2012-01-10 12 views
1

ここに私の暗号化設定は以下のとおりです。復号化時に「BadPaddingException」が表示されるのはなぜですか?

public void writeMessage (final byte[] message) throws Exception 
{ 
    try 
    { 
     // write iv 
     byte b[] = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV(); 
     System.out.println(b.length); 
     stream.write(b); 
    } 
    catch (final InvalidParameterSpecException e) 
    { 
     throw new Exception("Unable to write IV to stream due to invalid"+ 
          " parameter specification"); 
    } 
    catch (final IOException e) 
    { 
     throw new Exception("Unable to write IV to stream"); 
    } 

    try 
    { 
     // write cipher text 
     byte b[] = cipher.doFinal(message); 
     System.out.println(b.length); 
     stream.write(b); 
    } 
    catch (final IllegalBlockSizeException e) 
    { 
     throw new Exception("Unable to write cipher text to stream due to "+ 
          "illegal block size"); 
    } 
    catch (final BadPaddingException e) 
    { 
     throw new Exception("Unable to write cipher text to stream due to " + 
          "bad padding"); 
    } 
    catch (final IOException e) 
    { 
     throw new Exception("Unable to write cipher text to stream"); 
    } 
} 

エラー:Unable to decrypt message due to bad padding - null.

ここ

public byte[] readMessage() throws Exception 
{ 
    byte[] iv = new byte[16]; 
    byte[] message = new byte[EncryptionSettings.encryptionMessageLength]; 

    try 
    { 
     // read IV from stream 
     if (stream.read(iv) != 16) 
      throw new Exception("Problem receiving full IV from stream"); 
    } 
    catch (final IOException e) 
    { 
     throw new Exception("Unable to read IV from stream"); 
    } 

    try 
    { 
     cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv)); 
    } 
    catch (final InvalidKeyException e) 
    { 
     throw new Exception("Invalid key"); 
    } 
    catch (final InvalidAlgorithmParameterException e) 
    { 
     throw new Exception("Invalid algorithm parameter"); 
    } 

    try 
    { 
     //read message from stream 
     if (stream.read(message) != EncryptionSettings.encryptionMessageLength) 
      throw new Exception("Problem receiving full encrypted message from stream"); 
    } 
    catch (final IOException e) 
    { 
     throw new Exception("Unable to read message from stream"); 
    } 

    try 
    { 
     return cipher.doFinal(message); //decipher message and return it. 
    } 
    catch (IllegalBlockSizeException e) 
    { 
     throw new Exception("Unable to decrypt message due to illegal block size - " 
          + e.getMessage()); 
    } 
    catch (BadPaddingException e) 
    { 
     throw new Exception("Unable to decrypt message due to bad padding - " 
          + e.getMessage()); 
    } 
} 

を暗号化するために私のコードです:

ここ
public static String encryptionAlgorithm = "AES"; 
public static short encryptionBitCount = 256; 
public static int encryptionMessageLength = 176; 
public static String hashingAlgorithm = "PBEWITHSHAAND128BITAES-CBC-BC"; 
     //PBEWithSHA256And256BitAES-CBC-BC"PBEWithMD5AndDES";//"PBKDF2WithHmacSHA1"; 
public static short hashingCount = 512; 
public static String cipherTransformation = "AES/CBC/PKCS5Padding"; 

を解読するために私のコードですdecryptinを実行するとBadPaddingExceptionが発生するg、なぜ?私の最初のコメントからのメッセージ(16で割り切れる)パディング後176で正確に168文字です

+1

また、あなたの例外処理は実際には「処理していません」...あまり説明的ではない例外タイプと若干説明的なメッセージの代わりにスタックトレースの一部を投げ捨てているだけです。 –

+0

私はキーメソッドを選んだだけで、すべてが正しく開閉されていることを指摘しておきます。鍵は、サーバとクライアントの両方に組み込まれているので、異なることはできません(現時点では、明らかにこれが変わります)。 – Cheetah

+0

情報が少なすぎるため、コードがないためコンパイルできません。ストリームのために。すべてが問題ないと言っても私にはうまくいきますが、その間にコードは実行されません。それは正しい*ではありません。 –

答えて

4

:キーは、他の側で使用したものとは異なる場所

典型的なシナリオは、1です。これが最も考えられる原因ですが、実際には.close()およびおそらく.flush()ステートメントが不足しているため、ストリームの処理方法を確認することもできます。また、すべてのデータを常にバッファに読み込むことができると想定していますが、そうでない場合もあります。

キーが実際に正しく計算されませんでした。 enryption /復号

関連する問題