2010-08-30 61 views
1

我使用像這樣的RSACryptoServiceProvider ...RSACryptoServiceProvider KeyContainer似乎超時?

private byte[] RSAEncrypt(byte[] DataToEncrypt, string ContainerName, bool DoOAEPPadding) 
    { 
     try 
     { 
      byte[] encryptedData; 

      // Create a new instance of CspParameters. Pass 
      // 13 to specify a DSA container or 1 to specify 
      // an RSA container. The default is 1. 
      CspParameters cspParams = new CspParameters(); 

      // Specify the container name using the passed variable. 
      cspParams.KeyContainerName = ContainerName; 

      cspParams.Flags = CspProviderFlags.UseDefaultKeyContainer; 

      //Create a new instance of RSACryptoServiceProvider. 
      using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(cspParams)) 
      { 
       //Encrypt the passed byte array and specify OAEP padding. 
       //OAEP padding is only available on Microsoft Windows XP or 
       //later. 

       encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding); 
      } 
      return encryptedData; 
     } 
     //Catch and display a CryptographicException 
     //to the console. 
     catch (CryptographicException ex) 
     { 
      sl.Write(ex, MessageType.Error); 
      throw;     
     } 
    } 

然後我嘗試關閉我的Outlook插件的Windows窗體應用程序,並把它背在其上使用這個代碼peice的是什麼後,對數據進行解密。解密代碼看起來像這樣...

private byte[] RSAEncrypt(byte[] DataToEncrypt, string ContainerName, bool DoOAEPPadding) 
    { 
     try 
     { 
      byte[] encryptedData; 

      // Create a new instance of CspParameters. Pass 
      // 13 to specify a DSA container or 1 to specify 
      // an RSA container. The default is 1. 
      CspParameters cspParams = new CspParameters(); 

      // Specify the container name using the passed variable. 
      cspParams.KeyContainerName = ContainerName; 

      cspParams.Flags = CspProviderFlags.UseDefaultKeyContainer; 

      //Create a new instance of RSACryptoServiceProvider. 
      using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(cspParams)) 
      { 
       //Encrypt the passed byte array and specify OAEP padding. 
       //OAEP padding is only available on Microsoft Windows XP or 
       //later. 

       encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding); 
      } 
      return encryptedData; 
     } 
     //Catch and display a CryptographicException 
     //to the console. 
     catch (CryptographicException ex) 
     { 
      sl.Write(ex, MessageType.Error); 
      throw;     
     } 
    }  
直到東西來了,我不能把我的手指上

的偉大工程。我不知道它是否像日期變化或什麼。會發生什麼是我試圖解密數據,並得到「錯誤的數據」錯誤。現在它再次運行良好,直到某段時間,或關閉應用程序或用戶註銷。我只是不知道,也不能確定是什麼原因造成的。當我吹走來自文本文件的加密數據並重新創建並解密時,我沒有問題。即使我在加密/保存到文件和從文件讀取/解密它之間重新啓動應用程序也會很好!發生了一些事情,我只是不知道KeyContainers是否足夠了解可能會使CspParameters過期的最佳猜測?

回答

0

我結束了使用CspParameters標誌和,而不是使用用戶KeyContainer商店,我使用的機器KeyContainer商店。

0

是的,如果你設置: cspParams.Flags = CspProviderFlags.UseDefaultKeyContainer;

然後將密鑰容器存儲在用戶的密鑰容器存儲中,然後以另一個用戶的身份登錄,並使用RSA提供完全不同的KeyContainer存儲。

改爲使用: cspParams.Flags = CspProviderFlags.UseMachineKeyStore = true;

將使用本地計算機的KeyContainer存儲區,該存儲區對於計算機是全局的,並且將爲您提供相同的KeyContainer存儲,無論哪個用戶登錄。但是,這僅適用於Windows安裝。在不同的Windows安裝或計算機下運行程序將爲您提供不同的KeyContainer存儲。如果您希望在多臺計算機上解密相同的數據,則需要將密鑰保存到硬盤上的文件中。堅持一個純文本文件的密鑰是一個巨大的安全風險,所以請在將密鑰保存到文件之前對其進行加密,或者將其保存在受密碼保護的.rar文件中。

如果仍有問題,請嘗試設置: RSA.PersistKeyInCsp = true;

這將確保您的密鑰保存在KeyContainer存儲中。如果使用CspParameters構造函數,則在KeyContainer中保留該文件應該是默認行爲,如下所示:

CspParameters cspParams = new CspParameters();

用微軟自己的話說: 「這種形式的CspParameters初始化ProviderType字段的值爲24,它指定PROV_RSA_AES提供程序。「 來源:http://msdn.microsoft.com/en-us/library/xw9ywed4.aspx

所以,你在你的代碼中的註釋是不正確的,我會誤導你,我會建議你改正

我不確定關於堅持在重點其它ProviderTypes和它們的默認設置。在KeyContainer店,所以設置PersistKeyInCsp爲true,如果你仍然有問題,可能需要

希望這有助於

〜亞當的WhiteHat();