AES 去除初始化向量 解密报错?
这段代码调试了无数次,始终报错, 网上也找个很多方法都未能解决, 求大佬帮助~
完整测试代码如下:
public class Test { private static String encryptKey = "cf+WtR8yLvaX/v9NDsXcl7oWps8F3pom";
public static void main(String[] args) {
decrypt();
}
public static void encrypt() {
String encryptStr = "[{\"projectId\":\"AEROB\",\"tenantId\":\"LGE\",\"brand\":\"VW\",\"vin\":\"LAVTDMERZAA000010\",\"uid\":\"7f8b25ad-620a-4e42-a376-8a5a4f22372e\",\"language\":\"zh-cn\",\"deviceId\":\"LG8-LGA25.05.2101000045\",\"deviceType\":\"HU\",\"sendType\":0,\"province\":\"\",\"city\":\"\",\"area\":\"\",\"os\":\"LINUX\",\"osVer\":\"5.4.131\",\"eventId\":\"KL15Event\",\"appId\":\"SysPower\",\"msgId\":\"5271fd11-8267-4b5c-90a8-58033b3fbcca\",\"collectTime\":1671165022000,\"appVersion\":\"\",\"data\":{\"event\":\"KL15 OFF\",\"pageId\":\"sys\"}}]";
//1.数据转UTF-8编码bytes
byte[] dataBytes = encryptStr.getBytes(StandardCharsets.UTF_8);
//2.压缩
byte[] gzip = ZipUtil.gzip(dataBytes);
//3.Base64编码
byte[] encodeBase64 = Base64.getEncoder().encode(gzip);
//4.获取秘钥字符串UTF-8编码bytes
byte[] keyBytes = encryptKey.getBytes(StandardCharsets.UTF_8);
//5.随机生成初始化向量
IvParameterSpec ivParameterSpec = new IvParameterSpec(keyBytes, 0, 16);
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, "AES"));
byte[] encryptBytes = cipher.doFinal(encodeBase64);
// 6.组装
byte[] concatBytes = ArrayUtils.addAll(encryptBytes, ivParameterSpec.getIV());
// 7.base64 编码
String result = Base64.getEncoder().encodeToString(concatBytes);
System.out.println("加密后的字符串:" + result);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void decrypt() {
String encryptStr = "yc5dF47WNO9MZ4qUjBwZ46QWcQLxZBtxoepcyPHZB0XXiJxnOTD1Xy4r2AvWO0q+mbEfh2sbUkLer4Y4e2VV9a19HFlI821xvvgCx3HVMkN29KEPHFiwoDWf4X+aW4d3owpu/SRGoK8Q/1sbKMOmykrLQsqZqhasSKYGwTrkwXB0E59xTFJn0o6s5gliQYioSU+Th+VyK0t4AKGZfDyLX4Yujd81EEbZT4zJcjMB8jnJkNs1OyGMzhVgmtz6Jho1jAm2nShaRQrl78ey7qFZ+mK69qj5HbQvDp1OalnixU70R/blLOYZpKeoJWnAqa/HSENtk7O3ciSYo4t9fvYknV7dX1w+FPwywofthIO+OJit+CIPGAqfd7HTs0g+DlFbz0j9gDHbZHS4AKwExTl8EdfMdLCSpBM97CgangI/aRSUbQyXC7JXfrM6lkdfKql+CMdghRAT7HQx9Lzo8uoN3G5vXcpW/gAcGJYUm0s0bey12rOvtu8EP/i3wN7/tI0HeTxCpbl+dgdpcJHUD21FXe6Zyq9LVXiZibWE0sGs4l1xEO2kazvJ75f5QLF/J+VLl+MrFyQfXen1A9mRjhdNB2NmK1d0Ujh5THZhWC92OU4=";
//1.base64解码
byte[] decryptStr = Base64.getDecoder().decode(encryptStr);
byte[] keyBytes = encryptKey.getBytes(StandardCharsets.UTF_8);
//2.提取初始化向量
IvParameterSpec ivParameterSpec = new IvParameterSpec(keyBytes, 0, 16);
//3.去除初始化向量
byte[] removeBytes = Arrays.copyOf(decryptStr, decryptStr.length - 16);
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, "AES"));
byte[] byteData = cipher.doFinal(removeBytes);
//4.解码
byte[] decodeBytes = Base64.getDecoder().decode(byteData);
//4.解压缩
String result = ZipUtil.unGzip(decodeBytes, "utf-8");
System.out.println("-->解密后字符串: " + result);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
maven:
<dependency> <groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>
报错信息如下:
Connected to the target VM, address: '127.0.0.1:52297', transport: 'socket'Exception in thread "main" java.lang.RuntimeException: java.security.InvalidKeyException: Parameters missing
at com.github.yeecode.easyrpc.client.Test.decrypt(Test.java:74)
at com.github.yeecode.easyrpc.client.Test.main(Test.java:21)
Caused by: java.security.InvalidKeyException: Parameters missing
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:469)
at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:313)
at javax.crypto.Cipher.implInit(Cipher.java:801)
at javax.crypto.Cipher.chooseProvider(Cipher.java:863)
at javax.crypto.Cipher.init(Cipher.java:1248)
at javax.crypto.Cipher.init(Cipher.java:1185)
at com.github.yeecode.easyrpc.client.Test.decrypt(Test.java:66)
... 1 more
Disconnected from the target VM, address: '127.0.0.1:52297', transport: 'socket'
Process finished with exit code 1
回答:
cipher.init方法用的不对,应该为
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, "AES"), ivParameterSpec)
亲测有效
以上是 AES 去除初始化向量 解密报错? 的全部内容, 来源链接: utcz.com/p/944976.html