使用java8 java7 java6 解密微信小程序encryptedData可以回遇到一些错误
1.java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/CBC/PKCS7Padding
2.Java.security.NoSuchProviderException: no such provider: BC
解决方法:
1.下载bcprov-jdk15on:jar,下再地址:http://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on/1.58
2.解密代码
static{
Security.addProvider(new BouncyCastleProvider());
}
// 被加密的数据
byte[] dataByte = Base64.decodeBase64(encryptedData);
// 加密秘钥
byte[] keyByte = Base64.decodeBase64(sessionkey);
// 偏移量
byte[] ivByte = Base64.decodeBase64(iv);
try {
// Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
parameters.init(new IvParameterSpec(ivByte));
cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
byte[] resultByte = cipher.doFinal(dataByte);
if (null != resultByte && resultByte.length > 0) {
String result = new String(resultByte, "UTF-8");
}
result 就是解密的数据
分享一个朋友的人工智能教程。零基础!通俗易懂!风趣幽默!大家可以看看是否对自己有帮助,点击查看教程。
如果对你有帮助,请不吝打赏