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;
}
주세요 나 내가 어디로 가고 있는지 안다. 잘못된.
암호문은 텍스트가 아니며 '.txt' 파일에 저장하면 안됩니다. 여기서 정확히'os'를 닫고 있습니까? – EJP
질문은 스트림 종료 코드를 추가하여 수정되었습니다. –
코드가 이식 가능하지 않으므로 다른 플랫폼에서 문제가 될 수있는 암호화 및 암호 해독 및 해독 조각을 실행중인 경우 코드를 이식성있게 만들려면 플랫폼 기본값에 대한 의존도를 제거하십시오. 귀하의 코드에서 나는 그것을 두 영역에서 보았습니다. 첫째,'String.getBytes()'를 사용하지 마십시오. 대신에,'String.getBytes (StandardCharsets.UTF-8)'를 사용하십시오. 둘째,'Cipher.getInstance ("AES")'를 사용하지 말라. 항상 "알고리즘/모드/패딩"문자열을 지정하십시오. –