java 7:Caused 中的填充异常:javax.crypto.BadPaddingException:给定的最终块未正确填充
Padding exception in java 7:Caused by: javax.crypto.BadPaddingException: Given final block not properly padded
您好,我正在使用 tripedes 键从输入流读取并写入输出流。
在 java7/8 中获取此 execption:原因:javax.crypto.BadPaddingException:给定的最终块未正确填充
byte[] passwd = Base64Util.decode(pwd);
bais = new ByteArrayInputStream(passwd);
baos = new ByteArrayOutputStream();
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, key);
// Read bytes, decrypt, and write them out.
byte[] buffer = new byte[2048];
int bytesRead;
while ((bytesRead = bais.read(buffer)) != -1) {
out.write(cipher.update(buffer, 0, bytesRead));
}
// Write out the final bunch of decrypted bytes
out.write(cipher.doFinal());
out.flush();
谁能告诉我 cipher.doFinal 中可能有什么错误?
更新,加密代码复制自评论:
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
// Create a special output stream to do the work for us
CipherOutputStream cos = new CipherOutputStream(out, cipher);
// Read from the input and write to the encrypting output stream
byte[] buffer = new byte[2048];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
cos.write(buffer, 0, bytesRead);
}
cos.close();
如果输入数据不总是块大小的倍数,则必须向输入数据添加填充;指定填充,PKCS#5 是 DES/3DES.
的首选填充
getInstance
调用未完全指定,缺少模式和填充选项。添加完整规范,例如:"DESede/ECB/PKCS5Padding (168)"。参见 Class Cipher documentation。
不要在新代码中使用 3DES,它是不安全的,也不要使用 ECB 模式,它也是不安全的,请参阅 ECB mode,向下滚动到 Penguin。
您好,我正在使用 tripedes 键从输入流读取并写入输出流。 在 java7/8 中获取此 execption:原因:javax.crypto.BadPaddingException:给定的最终块未正确填充
byte[] passwd = Base64Util.decode(pwd);
bais = new ByteArrayInputStream(passwd);
baos = new ByteArrayOutputStream();
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, key);
// Read bytes, decrypt, and write them out.
byte[] buffer = new byte[2048];
int bytesRead;
while ((bytesRead = bais.read(buffer)) != -1) {
out.write(cipher.update(buffer, 0, bytesRead));
}
// Write out the final bunch of decrypted bytes
out.write(cipher.doFinal());
out.flush();
谁能告诉我 cipher.doFinal 中可能有什么错误?
更新,加密代码复制自评论:
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
// Create a special output stream to do the work for us
CipherOutputStream cos = new CipherOutputStream(out, cipher);
// Read from the input and write to the encrypting output stream
byte[] buffer = new byte[2048];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
cos.write(buffer, 0, bytesRead);
}
cos.close();
如果输入数据不总是块大小的倍数,则必须向输入数据添加填充;指定填充,PKCS#5 是 DES/3DES.
的首选填充getInstance
调用未完全指定,缺少模式和填充选项。添加完整规范,例如:"DESede/ECB/PKCS5Padding (168)"。参见 Class Cipher documentation。
不要在新代码中使用 3DES,它是不安全的,也不要使用 ECB 模式,它也是不安全的,请参阅 ECB mode,向下滚动到 Penguin。