2009-05-20 1 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

それはそれが言うことを意味します。解読しようとしているデータが破損しているか、不正な設定を行っています。詳細については、ポストコードを参照してください。 –

+0

これは私が今行っているコードです。あなたは何を問題にしてください? –

答えて

0

私は同様の問題を引き起こしましたが、あなたはそれを克服するために2つのことを行うことができます。

  1. 暗号化するデータが使用しているキーよりも短いことを確認する必要があります。あなたの鍵が1024ビットの場合は、1000ビットという言葉でのみベースされていることを確認してください。これを行うには、バイト配列を小さなチャンクにチャンクし、各チャンクを暗号化してから、配列または文字列に暗号化された値を格納する必要があります。したがって、1文字列を暗号化する代わりに、5文字列を暗号化します。文字列としてこの情報を格納すると、すべての数字が同じ長さなので、フォーマッタは15を返した場合、あなたはちょうどその置くためにバイトを取得するために、後で3で割るように、あなたは015で文字列を格納していることを確認してください

配列に挿入します。

データを復号化するには、文字列の長さを読み取り、復号するチャンクの数を判別するだけで済みます。これらを1つずつデカップし、デコードされたバイト配列でオブジェクトを再作成することができます。

実際のコードをご希望の場合は、私に個人的にお問い合わせください。私はあなたのためにこれを行うことができるいくつかのスクリプトでよりうまくお手伝いできるようになります。

関連する問題