当我尝试解密以下文件时,我会在以下行获得此BadPaddingException
。我相信文件已正确加密。
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:810)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:675)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
at javax.crypto.Cipher.doFinal(Cipher.java:1970)
**at MyProgram.decryptFile(MyProgram.java:1437)**
at MyProgram.decrypt(MyProgram.java:865)
at MyProgram.access$900(MyProgram.java:68)
at MyProgram$1.messagesAdded(MyProgram.java:353)
at javax.mail.event.MessageCountEvent.dispatch(MessageCountEvent.java:150)
at javax.mail.EventQueue.run(EventQueue.java:135)
at java.lang.Thread.run(Thread.java:722)
这是解密代码,我也可以在加密部分上发送代码:
private static void decryptFile(String file, String password, byte[] Salt, byte[] IV, String attachmentunecryptedhash, String attachmentoriginalsizeString, String attachmentcreated)
{
long attachmentSize = 0;
String attachmentCreated = null;
String attachmentModified = null;
String attachmentHash = null;
byte[] buffers = new byte[16];
byte[] endOfFile = new byte[16];
int counterForFile = 0;
int attachmentoriginalsize = 0;
int noBytes = 0;
try
{
if(Paths.get(file.trim()).toFile().exists() == true) //If the File Exists
{
//Creates Secret Key For Decryption -- Passes Password, Salt, Iterations, and Key Length
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), Salt, 65536, 256);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
SecretKey secretKey = factory.generateSecret(keySpec);
SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
//Initalizes Cipher For Decrypt Mode -- Passes Password and Salt
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(IV));
//Prepares To Write New Buffer Containing Decrypted Information to Output File
String unencryptedFile = file.trim().replaceAll(".aes", "");
FileInputStream fileInputStream = new FileInputStream(file.trim());
FileOutputStream fileOutputStream = new FileOutputStream(unencryptedFile);
attachmentoriginalsize = Integer.parseInt(attachmentoriginalsizeString);
//Writes Encrypted File to Disk Using Secure Cipher Output Stream
while((noBytes = fileInputStream.read(buffers)) != -1)
{
//Writes 1 encrypted byte at a time
fileOutputStream.write(cipher.update(buffers,0,noBytes));
//counterForFile += 16;
}
buffers = cipher.doFinal(); //Line 1437 Where the Error Exists
fileOutputStream.write(buffers);
fileOutputStream.flush();
//Close Files, Cleanup
fileInputStream.close();
fileOutputStream.close();
System.exit(1);
}
答案 0 :(得分:1)
有许多事情可能导致“不良填充”#34;错误。基本上任何导致最后一个块结束不匹配预期填充的东西都会引发错误。可能的原因包括:填充设置不正确,密钥错误,密文错误等。
要尝试诊断问题,请将解密端设置为NoPadding
。这将接受任何内容,并允许您检查输出:
完全垃圾:您可能在密钥或不同模式设置中出错。
第一次阻止垃圾:您可能遇到密钥错误或IV错误。
最后一个块垃圾:可能是cyphertext文件的损坏结束。
正确的解密,最后有一些奇怪的字节:奇怪的字节是填充。
如果它只是填充,那么设置解密函数以期望这种填充。否则,检查加密和解密的密钥/ IV / cyphertext byte-to-byte 是否相同。
至关重要您在诊断后设置了填充模式。 NoPadding
是不安全的。