2017-03-08 6 views
1

こんにちは私は暗号化してテキストを解読したいです。私の暗号化コードは正常に動作し、私が望む値にマッチしています。しかし、私はDecryptこれはエラーpadding is invalid and cannot be removedを与えて欲しい。以下のコードでは、私は両方のコードの暗号化と復号化を行っています。また、私はこのエラーを修正する必要がありますStack overflow linkStackoverlFlow Link 2しかし、それを修正しないでください。埋め込みが無効で、復号化の値を取り除くことができません

string getHashKey1 = EncryptText("10002:1486703720424", "hpIw4SgN)TxJdoQj=GKo)p83$uHePgoF"); 

結果= 1ltQFLRGNif73uCNzi0YEvBqLKiRgx6fWsk5e/GcTQc=

string reverseKey = DecryptText('1ltQFLRGNif73uCNzi0YEvBqLKiRgx6fWsk5e/GcTQc=', "hpIw4SgN)TxJdoQj=GKo)p83$uHePgoF"); 

私はAES_DECRYPT aes.Padding = PaddingMode.Zerosに追加します。私は結果より下になる。 結果: - 上記のコード����y�7�t���Ij���,���� Z��$�

public string EncryptText(string input, string password) 
    { 
     string result = ""; 
     try 
     { 
      // Get the bytes of the string 
      byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input); 
      byte[] passwordBytes = Encoding.UTF8.GetBytes(password); 

      byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes); 

      result = Convert.ToBase64String(bytesEncrypted); 
      return result; 
     } 
     catch (Exception ex) 
     { 

     } 

     return result; 
    } 

public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes) 
    { 
     byte[] encryptedBytes = null; 
     try 
     {  
      using (MemoryStream ms = new MemoryStream()) 
      { 
       using (Aes aes = Aes.Create()) 
       { 
        aes.Key = passwordBytes; 
        aes.Mode = CipherMode.ECB; 

        // "zero" IV 
        aes.IV = new byte[16]; 

        using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)) 
        { 
         cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length); 
         cs.Close(); 
        } 
        encryptedBytes = ms.ToArray(); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 

     }    
     return encryptedBytes; 
    } 

は、暗号化のために正常に動作しています。コードの下

passwordBytes = SHA256.Create().ComputeHash(passwordBytes); 

ではなく、ときに暗号化、エラー padding is invalid and cannot be removed

public string DecryptText(string input, string password) 
    { 
     // Get the bytes of the string 
     byte[] bytesToBeDecrypted = Convert.FromBase64String(input); 
     byte[] passwordBytes = Encoding.UTF8.GetBytes(password); 
     passwordBytes = SHA256.Create().ComputeHash(passwordBytes); 

     byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes); 

     string result = Encoding.UTF8.GetString(bytesDecrypted); 

     return result; 
    } 


public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes) 
    { 
     byte[] decryptedBytes = null; 

     using (MemoryStream ms = new MemoryStream()) 
     { 
      using (Aes aes = Aes.Create()) 
      { 
       aes.Key = passwordBytes; 
       aes.Mode = CipherMode.ECB; 
       aes.IV = new byte[16];     

       using (var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write)) 
       { 
        cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length); 
        cs.Close(); // here i am getting error 
       } 
       decryptedBytes = ms.ToArray(); 
      } 
     } 

     return decryptedBytes; 
    } 
+0

あなたは 'passwordBytes = SHA256.Create()ComputeHash(passwordBytes)を復号する際、パスワードをハッシュ化されている;'ではなく、とき私は私がその行を削除感謝@pedrofb – pedrofb

+0

今はそれが動作します。お返事ありがとうございます –

+0

を復号化するとき何をする必要があるか@pedrofb暗号 –

答えて

1

あなたが復号化するときに、パスワードをハッシュさを与えています。

1)(すでにpedrofbによって指摘さ):あなたが暗号でUTF8.GetBytesを使用していますが、復号化でSHA256(UTF8.GetBytes())これは、あなたが二つの問題を持っている別のパスワードに

+0

あなたの答えを投票していただきありがとうございます。私はすでにエラーを見つけるために私の3時間を与えている –

3

を使用していることを意味します。

これらのいずれの方法も使用しないでください。代わりに、PBKDF2などの適切なパスワードベースのキー導出関数を使用する必要があります。 .NETではPBKDF2はRfc2898DeriveBytesクラスから利用できます。

byte[] salt = 8 or more bytes that you always pass in as the same. 
// (salt could be fixed for your application, 
// but if you have users it should be unique per user and stored along with the output value) 
int iterations = 100000; 
// Or bigger. If you were making a user management system you 
// should write this number down, too, so you can increase it over time; 
// it should be whatever number makes it take 100ms or more on the fastest relevant computer) 
Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, salt, iterations); 
passwordBytes = pbkdf2.GetBytes(16); // 16 = AES128, 24 = AES192, 32 = AES256. 

2)暗号化でBase64エンコーディングを使用しますが、暗号化ではUTF8.GetBytesを使用します。

ボーナス問題:

3)あなたは、電子コードブック(ECB)チェーンを使用しています。暗号ブロック連鎖(CBC)はECBよりも推奨されます。

CBCを適切に使用するには、新しいAesオブジェクトを作成するときに自動的に行われるランダム初期化ベクトル(IV)を生成するか、オブジェクトを再利用する場合はGenerateIV()を暗号化で呼び出すことができます。次に、IV(常にAESの場合は16バイト)を暗号文に付加することができます。復号化では、最初の16バイトを切り取り、それをIV(データの残りの部分を復号化する)またはb)全体を復号化し、復号化された出力の最初の16バイトを無視することができます。

関連する問題