AES Error: Given final block not properly padded(AES 错误:给定的最终块未正确填充)
问题描述
我需要有关此错误的帮助:给定最终块未正确填充.从标题可以看出,我正在使用 AES.
I need help with this error: Given final block not properly padded. As you can see from the title, I am working with AES.
这是错误所在行的代码:
Here is the code of line where is error:
byte[] decrypted = cipher.doFinal(bytes);
这里是完整的代码:
public class AESCrypt {
private final Cipher cipher;
private final SecretKeySpec key;
private String encryptedText, decryptedText;
public AESCrypt(String password) throws Exception {
// hash password with SHA-256 and crop the output to 128-bit for key
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(password.getBytes("UTF-8"));
byte[] keyBytes = new byte[16];
System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
key = new SecretKeySpec(keyBytes, "AES");
}
public String encrypt(String plainText) throws Exception {
byte[] iv = new byte[cipher.getBlockSize()];
new SecureRandom().nextBytes(iv);
AlgorithmParameterSpec spec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
byte[] encrypted = cipher.doFinal(plainText.getBytes());
encryptedText = asHex(encrypted);
return encryptedText;
}
public String decrypt(String cryptedText) throws Exception {
byte[] iv = new byte[cipher.getBlockSize()];
AlgorithmParameterSpec spec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, key, spec);
// decrypt the message
byte[] bytes = cryptedText.getBytes("UTF-8");
byte[] decrypted = cipher.doFinal(bytes);
decryptedText = asHex(decrypted);
System.out.println("Desifrovani tekst: " + decryptedText + "
");
return decryptedText;
}
public static String asHex(byte buf[]) {
StringBuilder strbuf = new StringBuilder(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10) {
strbuf.append("0");
}
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}
public static void main(String[] args) throws Exception {
System.out.print("....AES....
");
String message = "MESSAGE";
String password = "PASSWORD";
System.out.println("MSG:" + message);
AESCrypt aes = new AESCrypt(password);
String encryptedText = aes.encrypt(message).toString();
System.out.println("SIFROVANA PORUKA: " + encryptedText);
String decryptedText = aes.decrypt(encryptedText).toString();
System.out.print("DESIFROVANA PORUKA: " + decryptedText);
}
}
推荐答案
根据您的评论,您已经非常接近让加密货币工作了.
Per your comment, you are pretty close to getting the crypto working.
您需要将 IV 生成代码从您的加密/解密方法移到其他地方,就像这样
You need to move the IV generation code from your encryption/decryption methods to somewhere else, like so
public AlgorithmParameterSpec getIV() {
AlgorithmParameterSpec ivspec;
byte[] iv = new byte[cipher.getBlockSize()];
new SecureRandom().nextBytes(iv);
ivspec = new IvParameterSpec(iv);
}
然后将该 ivspec 传递给加密和解密方法(使它们看起来像 encrypt(String,AlgorithmParameterSpec)
),以便加密和解密具有相同的 iv.
then pass that ivspec into both the encrypt and decrypt methods (making them look like encrypt(String,AlgorithmParameterSpec)
), so that you have the same iv for both encryption and decryption.
另外,不要在decryptedByteArray上调用printBase64Binary
,而是调用new String(decryptedByteArray, "UTF-8")
Also, don't call printBase64Binary
on the decryptedByteArray, instead call new String(decryptedByteArray, "UTF-8")
这篇关于AES 错误:给定的最终块未正确填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:AES 错误:给定的最终块未正确填充
基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01