【发布时间】:2017-09-19 21:32:14
【问题描述】:
我有一个 Windows 应用程序 (x64),它在 Winodws 7、8 和现在的 10 上运行良好。今天我们在 Windows 2012 Server 下运行该程序失败。当我们查看事件日志时,我们发现了一个源自 System.Security.Cryptography.RijndaelManaged..ctor() 的错误(遗憾的是日志没有给我们完整的路径)。
我使用了 Rijndael 算法来加密我程序中的敏感数据。程序要做的第一件事是检索加密的配置文件并对其进行解密以获取所有设置。这就是我的程序没有开始的地方。
这是我程序中的解密方法:
public static string Decrypt(string cipherText, string passPhrase)
{
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
{
byte[] keyBytes = password.GetBytes(keysize / 8);
using (RijndaelManaged symmetricKey = new RijndaelManaged())
{
symmetricKey.Mode = CipherMode.CBC;
using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes))
{
using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
}
}
}
}
}
这是我在日志中收到的错误消息:
应用程序:Postbag.exe 框架版本:v4.0.30319 描述: 由于未处理的异常,该进程被终止。例外 信息:System.InvalidOperationException 在 System.Security.Cryptography.RijndaelManaged..ctor() 在 Common.StringCipher.Decrypt(System.String, System.String) 在 Common.Conf..cctor() 异常信息: Common.Conf.get_DataProvider() 处的 System.TypeInitializationException 在 Postbag.FormMain..ctor() 在 Postbag.Program.Main()
新服务器也有相同版本的 .Net 框架。
【问题讨论】:
-
由于您没有更改块大小,您应该将
new RijndaelManaged()替换为Aes.Create()。它将产生相同的输出,但没有 FIPS 异常。
标签: c# windows encryption rijndaelmanaged windows2012