[英]Getting BadPaddingException when decrypting with AES algorithm
使用AES算法解密時出現以下錯誤。 我不明白我要去哪里錯了。 幾天前,這段代碼對我來說還算不錯,但是卻沒有拋出這樣的錯誤。
我得到的錯誤:-
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;
}
請讓我知道我要去哪里了。
您應該在加密部分中使用CipherOutputStream
,並確保對其調用close
,以便寫入最終的填充數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.