对 C# 非常陌生 - 从 Powershell 和 Java 中的真正管理员迁移。我正在使用 MS 功能来解密文件:
static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//A 64 bit key and IV is required for this provider.
//Set secret key For DES algorithm.
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
//Set initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
//Create a file stream to read the encrypted file back.
FileStream fsread = new FileStream(sInputFilename,
FileMode.Open,
FileAccess.Read);
//Create a DES decryptor from the DES instance.
ICryptoTransform desdecrypt = DES.CreateDecryptor();
//Create crypto stream set to read and do a
//DES decryption transform on incoming bytes.
CryptoStream cryptostreamDecr = new CryptoStream(fsread,
desdecrypt,
CryptoStreamMode.Read);
//Print the contents of the decrypted file.
StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
fsDecrypted.Flush();
fsDecrypted.Close();
}
目前,我通过各种不同的形式在各种按钮中调用它:
string decryptionKey = File.ReadAllText(@"C:\ThickClient\secretKey.skey");
DecryptFile("C:\\ThickClient\\Encrypted.enc", "C:\\ThickClient\\tempPWDump.enc", decryptionKey);
string decryptedPW = File.ReadAllText(@"C:\ThickClient\tempPWDump.enc");
File.Delete(@"C:\ThickClient\tempPWDump.enc");
而且我必须在每个表单中定义static void DecryptFile {code}
,并在每个表单中将其调用为一个新变量以使用它。看起来很疯狂,我可以在 Windows 窗体的哪个位置定义它并设置一个全局变量,以便它可用于每个窗体?