2009-05-20 81 views
0

同時使用rsa提供程序對字符串進行加密和描述時出現此錯誤。RSA數據解密錯誤。要解密的數據超過64字節的這個模數的最大值

RSA數據解密錯誤。要解密的數據超過此模數64字節的最大值。

任何人都有想法如何解決這個錯誤嗎?

 

    internal sealed class RSAProvider 
    { 
     #region key store class 

     [Serializable] 
      private struct rsaKey 
     { 
      public rsaKey(RSAParameters rsaKeyInfo) 
      { 
       D = rsaKeyInfo.D; 
       DP = rsaKeyInfo.DP; 
       DQ = rsaKeyInfo.DQ; 
       Exponent = rsaKeyInfo.Exponent; 
       InverseQ = rsaKeyInfo.InverseQ; 
       Modulus = rsaKeyInfo.Modulus; 
       P = rsaKeyInfo.P; 
       Q = rsaKeyInfo.Q; 
      } 

      public RSAParameters CreateRSAKey() 
      { 
       RSAParameters rsaKeyInfo = new RSAParameters(); 

       rsaKeyInfo.D = D; 
       rsaKeyInfo.DP = DP; 
       rsaKeyInfo.DQ = DQ; 
       rsaKeyInfo.Exponent = Exponent; 
       rsaKeyInfo.InverseQ = InverseQ; 
       rsaKeyInfo.Modulus = Modulus; 
       rsaKeyInfo.P = P; 
       rsaKeyInfo.Q = Q; 

       return rsaKeyInfo; 
      } 

      public byte[] D; 
      public byte[] DP; 
      public byte[] DQ; 
      public byte[] Exponent; 
      public byte[] InverseQ; 
      public byte[] Modulus; 
      public byte[] P; 
      public byte[] Q; 
     } 

     #endregion 

     private static RSAParameters rsaKeyParameters; 

     static RSAProvider() 
     { 
      string rsaKeyString = System.Configuration.ConfigurationSettings.AppSettings["RSAKey"]; 
      if(rsaKeyString != null) 
      { 
       rsaKeyParameters = GetKeyByString(rsaKeyString); 
      } 
     } 

     private RSAProvider() 
     { 
     } 

     private static RSAParameters RSAKeyInfo 
     { 
      get 
      { 
       return rsaKeyParameters; 
      } 
     } 

     private static bool DoOAEPPadding 
     { 
      get 
      { 
       return false; 
      } 
     } 

     public static string GenerateKey(int keySize) 
     { 
      //Create a new instance of RSACryptoServiceProvider to generate 
      //public and private key data. 
      RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(keySize); 

      RSAParameters rsaKeyInfo = RSA.ExportParameters(true); 

      return GetKeyString(rsaKeyInfo); 
     } 


     #region Encrypt 

     public static byte[] Encrypt(byte[] dataToEncrypt, string rsaKeyString) 
     { 
      RSAParameters rsaKeyInfo = GetKeyByString(rsaKeyString); 

      return Encrypt(dataToEncrypt, rsaKeyInfo); 
     } 

     public static byte[] Encrypt(byte[] dataToEncrypt, RSAParameters rsaKeyInfo) 
     { 
      try 
      { 
       //Create a new instance of RSACryptoServiceProvider. 
       // Common.Identity.ImpersonateValidUser("prana", "eetplpvt", "Avdhoota1985"); 
       RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); 

       //Import the RSA Key information. This only needs 
       //toinclude the public key information. 
       RSA.ImportParameters(rsaKeyInfo); 

       //Encrypt the passed byte array and specify OAEP padding. 
       //OAEP padding is only available on Microsoft Windows XP or 
       //later. 
       //return RSA.Encrypt(dataToEncrypt, DoOAEPPadding); 
       byte[] data = RSA.Encrypt(dataToEncrypt, DoOAEPPadding); 
       RSA.Clear(); 
       //Common.Identity.UndoImpersonation(); 
       return data; 
      } 
       //Catch and display a CryptographicException 
       //to the console. 
      catch(CryptographicException e) 
      { 
       // Updated By Divya Bhalodia on 27th June 2008 for Localization task 
       //throw new Exception("Data encryption error.", e); 
       Common.EnumLocalization.EnumLocalization loc = new Common.EnumLocalization.EnumLocalization(ASP.BL.ApplicationUsers.ApplicationUserController.CurrentUserCulture.Code, ASP.BL.Applications.ApplicationController.CurrentApplicationInfo.ItemId); 
       throw new Exception(loc.LocalizeString("RSA Data encryption error.") + e.Message, e); 
       // end Updated - Divya 
      } 
     } 

     public static byte[] Encrypt(byte[] dataToEncrypt) 
     { 
      return Encrypt(dataToEncrypt, RSAKeyInfo); 
     } 

     #endregion 

     #region Decrypt 

     public static byte[] Decrypt(byte[] dataToDecrypt, string rsaKeyString, bool doOAEPPadding) 
     { 
      RSAParameters rsaKeyInfo = GetKeyByString(rsaKeyString); 
      return Decrypt(dataToDecrypt, rsaKeyInfo, doOAEPPadding); 
     } 

     public static byte[] Decrypt(byte[] dataToDecrypt, RSAParameters rsaKeyInfo, bool doOAEPPadding) 
     { 
      try 
      { 
       //Create a new instance of RSACryptoServiceProvider. 
       Common.Identity.ImpersonateValidUser(); 
       RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); 

       //Import the RSA Key information. This needs 
       //to include the private key information. 
       RSA.ImportParameters(rsaKeyInfo); 

       //Decrypt the passed byte array and specify OAEP padding. 
       //OAEP padding is only available on Microsoft Windows XP or 
       //later. 
       //return RSA.Decrypt(dataToDecrypt, doOAEPPadding); 
       byte[] data = RSA.Decrypt(dataToDecrypt, doOAEPPadding); 
       RSA.Clear(); 
       Common.Identity.UndoImpersonation(); 
       return data; 
      } 
       //Catch and display a CryptographicException 
       //to the console. 
      catch(CryptographicException e) 
      { 

       // Updated By Divya Bhalodia on 27th June 2008 for Localization task 
       //throw new Exception("Data decryption error.", e); 
       Common.EnumLocalization.EnumLocalization loc = new Common.EnumLocalization.EnumLocalization(ASP.BL.ApplicationUsers.ApplicationUserController.CurrentUserCulture.Code, ASP.BL.Applications.ApplicationController.CurrentApplicationInfo.ItemId); 
       throw new Exception(loc.LocalizeString("RSA Data decryption error.") + e.Message, e); 
       // end Updated - Divya 
      } 
     } 

     public static byte[] Decrypt(byte[] dataToDecrypt) 
     { 
      return Decrypt(dataToDecrypt, RSAKeyInfo, DoOAEPPadding); 
     } 
     #endregion 

     #region Additional functions 

     private static string GetKeyString(RSAParameters rsaKeyInfo) 
     { 
      byte[] tmp; 
      rsaKey k = new rsaKey(rsaKeyInfo); 
      BinaryFormatter formater = new BinaryFormatter(); 

      using(MemoryStream stream = new MemoryStream()) 
      { 
       formater.Serialize(stream, k); 
       tmp = stream.ToArray(); 
      } 

      Code(tmp); 

      return Convert.ToBase64String(tmp); 
     } 


     private static RSAParameters GetKeyByString(string rsaKeyString) 
     { 
      rsaKey k; 

      byte[] tmp = Convert.FromBase64String(rsaKeyString); 
      Code(tmp); 

      BinaryFormatter formater = new BinaryFormatter(); 

      using(MemoryStream stream = new MemoryStream(tmp)) 
      { 
       k = (rsaKey)formater.Deserialize(stream); 
      } 
      return k.CreateRSAKey(); 
     } 


     private static void Code(byte[] tmp) 
     { 
      byte mask1 = 0x55; 
      byte mask3 = 0xB9; 
      byte mask4 = 0xCF; 

      for(int i = 0; i 
+1

它意味着它說什麼。您嘗試解密的數據已損壞,或者您提供的設置不正確。發佈代碼以獲取更多幫助。 – 2009-05-20 10:22:45

回答

0

我encoutered類似的問題,但你可以做兩兩件事來幫助自己克服這些困難。

  1. 您需要確保您正在加密的hte數據比您正在使用的密鑰短。所以如果你的密鑰是1024位,那麼確保你只有1000比特的低音。要做到這一點,你需要將你的字節數組塊化爲更小的塊,加密每個塊,然後將加密後的值存儲在數組或字符串中。所以不是加密1個字符串,而是加密說5個字符串。

當存儲該信息作爲一個字符串確保所有的數字都是相同的長度,因此,如果格式化返回15你存儲與015繩子,讓你只除以3後得到的字節,然後把進入陣列。

要解密您的數據,您只需簡單地讀取字符串的長度並確定要解密的塊數。一個一個地破壞這些數據,然後可以使用已破壞的字節數組重新創建對象。

如果你想要實際的代碼,請親自聯繫我,我將能夠幫助你更好地使用一些可以爲你做到這一點的腳本。