2017-03-19 22 views
0

AESアルゴリズムで解読すると次のエラーが発生します。私はどこが間違っているのか分かりません。このコードは私にとってはうまくいきましたが、そのようなエラーは発生しませんでした。AESアルゴリズムで解読するとBadPaddingExceptionが発生する

java.io.IOException: javax.crypto.BadPaddingException: Given final block not properly padded 
at javax.crypto.CipherInputStream.getMoreData(CipherInputStream.java:121) 
at javax.crypto.CipherInputStream.read(CipherInputStream.java:239) 
at javax.crypto.CipherInputStream.read(CipherInputStream.java:215) 
at userInterface.Decryption.decryption(Decryption.java:61) 
at userInterface.DecodingTab$DecodeButtonActionListener.actionPerformed(DecodingTab.java:295) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$500(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at ... 40 more 

暗号コード: - - :

public Encryption(String key2, FileInputStream fileInputStream, 
     FileOutputStream fileOutputStream) { 
    this.key = key2; 
    this.is = fileInputStream; 
    this.os = fileOutputStream; 
} 

public boolean encryption() throws Throwable { 
    MessageDigest md5 = MessageDigest.getInstance("MD5"); 
     md5.update(key.getBytes()); 

     SecretKeySpec key = new SecretKeySpec(md5.digest(), "AES"); 

     cipher = Cipher.getInstance("AES"); 
     cipher.init(Cipher.ENCRYPT_MODE, key); 
     cipherInputStream = new CipherInputStream(is, cipher); 

    byte[] bytes = new byte[64]; 
    int numBytes; 
    while ((numBytes = cipherInputStream.read(bytes)) != -1) { 
     os.write(bytes, 0, numBytes); 
    } 
    os.flush(); 
    os.close(); 
    is.close(); 
    cipherInputStream.close(); 
    return true; 
} 

**復号化コード: - **

public Decryption(String decodingKey, File fileOutput) { 
    this.key = decodingKey; 
    this.outputFile = fileOutput.getAbsolutePath(); 
} 

public boolean decryption() throws IOException, Throwable { 
    encryptedFile = new File("Crypto\\EncryptedFile.txt"); 
    is = new FileInputStream(encryptedFile); 
    File outputF = new File(outputFile); 
    os = new FileOutputStream(outputF); 

    MessageDigest md5 = MessageDigest.getInstance("MD5"); 
     md5.update(key.getBytes()); 

     SecretKeySpec key = new SecretKeySpec(md5.digest(), "AES"); 

     cipher = Cipher.getInstance("AES"); 
     //cipher = Cipher.getInstance("AES/CBC/NoPadding"); 
     cipher.init(Cipher.DECRYPT_MODE, key); 
    cipherInputStream = new CipherInputStream(is, cipher); 

    byte[] bytes = new byte[64]; 
    int numBytes; 
    while ((numBytes = cipherInputStream.read(bytes)) != -1) { 
     os.write(bytes, 0, numBytes); 
    } 
    os.flush(); 
    os.close(); 
    cipherInputStream.close(); 
    is.close(); 
    encryptedFile.delete(); 
    return true; 
} 

せてください、私は取得しています

エラー私はどこに行くのか知っています違う。

+0

暗号文はテキストではなく、 '.txt'という名前のファイルに保存しないでください。あなたはここで 'os'をどこで閉じていますか? – EJP

+0

質問は、ストリームの終了コードを追加して、同じのために編集されました。 –

+2

あなたのコードは移植性がありません。そのため、問題の可能性のある異なるプラットフォーム上で暗号化と復号化と復号化の部分を実行している場合です。コードを移植可能にするには、プラットフォームのデフォルトへの依存をなくします。あなたのコードでは、私は2つの領域でそれを参照してください。まず、 'String.getBytes()'を使わないでください。代わりに、 'String.getBytes(StandardCharsets.UTF-8)'を使用してください。次に、 'Cipher.getInstance(" AES ")'を使わないでください。完全な "アルゴリズム/モード/パディング"文字列を指定してください。 –

答えて

0

暗号化部分にCipherOutputStreamを使用し、最終的なパディングデータが書き込まれるようにcloseを呼び出してください。

+3

なぜですか?私は彼が現在持っている 'CipherInputStream'を使わないでください。また、彼は自分のストリームを適切に閉じているようだ。 –

関連する問題