【问题标题】:Rijndael cryptography not working on Windows Server 2012Rijndael 加密在 Windows Server 2012 上不起作用
【发布时间】: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


【解决方案1】:

RijndaelManaged 类不符合 FIPS,并且您的服务器似乎设置了安全策略系统加密:使用符合 FIPS 的算法进行加密、散列和签名

在知识库文章"System cryptography: Use FIPS compliant algorithms for encryption, hashing, and signing" security setting effects in Windows XP and in later versions of Windows 中说:

Microsoft .NET Framework 应用程序(例如 Microsoft ASP.NET)仅允许使用经 NIST 认证为符合 FIPS 140 的算法实现。具体来说,唯一可以实例化的加密算法类是那些实现符合 FIPS 的算法的类。这些类的名称以“CryptoServiceProvider”或“Cng”结尾。任何尝试创建其他加密算法类的实例,例如名称以“Managed”结尾的类,都会导致发生 InvalidOperationException 异常

所以要么disable the security policy(来自 SecPol.msc 工具)要么使用符合 FIPS 的实现。不幸的是,Rijndael 没有这样的实现,所以您可能想看看 AesCngAesCryptoServiceProvider 是否符合您的需求,因为 AES 是从 Rijndael 开始的正式实现。基于Prateek Kr Dubey中的the blog Is RijndaelManaged class FIPS compliant?,我得出结论,用RijdaelManaged加密的数据可以用AesCngAesCryptoServiceProvider解密。

为了完整起见,我使用 RijnDaelManaged 类创建了一个 Encrypt 方法,并在这一行修改了您的代码示例:

using (RijndaelManaged symmetricKey = new RijndaelManaged())

阅读

using (var symmetricKey = new AesCryptoServiceProvider()) // or new AesCng()

并且确实能够解密字符串。

【讨论】:

  • 感谢您的信息。但是如何禁用安全策略?
  • 我已在 禁用安全策略 下链接它,但这里也是:i.stack.imgur.com/bSsH0.png
  • 感谢您的回复。让我尝试从服务器中删除该策略,然后重试。我认为目前对程序进行更改是不可行的,因为这是一个本地解决方案,并且应用更改将需要大量更新。但是一旦客户取消政策,我就会回到这里。
  • 我们最终禁用了安全策略。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
  • 2018-04-04
  • 2016-07-07
  • 2016-12-30
  • 2019-03-10
  • 1970-01-01
相关资源
最近更新 更多