Rijndael加密技术不能在Windows Server 2012上工作

问题描述:

我有一个Windows应用程序(x64),它一直在Winodws 7,8和现在10上正常工作。今天,我们失败了运行该程序的Windows 2012服务器下的。当我们查看事件日志时,发现来自System.Security.Cryptography.RijndaelManaged..ctor()的错误(遗憾的是日志没有给我们提供完整的路径)。Rijndael加密技术不能在Windows Server 2012上工作

我已经使用了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 Framework版本:v4.0.30319说明: 由于未处理的异常导致进程终止。异常 信息:System.InvalidOperationException在 System.Security.Cryptography.RijndaelManaged..ctor()at Common.StringCipher.Decrypt(System.String,System.String)at Common.Conf..cctor()异常信息:在Common.Conf.get_DataProvider() 在Postbag.FormMain..ctor()在Postbag.Program.Main( System.TypeInitializationException)

新的服务器也有.NET Framework的版本相同。

+0

既然你没有改变块的大小,你应该用'Aes.Create()'替换'new RijndaelManaged()'。它会产生相同的输出,但不会产生FIPS异常。 – bartonjs

RijndaelManaged类不符合FIPS,您的服务器似乎具有安全策略系统加密:使用符合FIPS标准的算法进行加密,散列和签名集。

在ARTICAL "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认证算法的实现是140符合FIPS标准。具体而言,唯一可以实例化的加密算法类是那些实现FIPS兼容算法的类。这些类的名字以“CryptoServiceProvider”或“Cng”结尾。任何尝试创建其他的加密算法类,如在结尾的名称类的实例“管理”,导致出现

InvalidOperationException异常因此,无论disable the security policy(从SecPol.msc工具)或者使用FIPS遵从执行。不幸的是,Rijndael没有这样的实现,因此您可能想要看看AesCngAesCryptoServiceProvider是否符合您的需求,因为AES是以Rijndael开始的正式实施。根据the blog Is RijndaelManaged class FIPS compliant? from Prateek Kr Dubey我得出结论:用RijdaelManaged加密的数据可以用AesCngAesCryptoServiceProvider来解密。

是完整的,我创建与RijnDaelManaged类的加密方法和适应在此行的代码示例:

using (RijndaelManaged symmetricKey = new RijndaelManaged()) 

阅读

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

,并确实能够解密的字符串。

+0

感谢您的信息。但是,我怎样才能禁用安全策略? – Disasterkid

+1

我已经链接下*禁用安全策略*,但这里也是:https://i.stack.imgur.com/bSsH0.png – rene

+0

感谢您的回复。让我尝试从服务器上删除策略,然后重试。我不认为在程序上做出改变目前是可行的,因为这是一个内部部署解决方案,应用这一改变将需要大量更新。但只要客户取消政策,我会回到这里。 – Disasterkid