2017-09-19 38 views
1

我有一個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

回答

4

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