使用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;
}
请让我知道我哪里出错。
答案 0 :(得分:0)
您应该在加密部分使用CipherOutputStream
,并确保在其上调用close
,以便写入最终的填充数据。