NoSuchAlgorithmException: Cannot find any provider supporting AES/CBC/PKCS7Padding
AES加密解密遇到的问题,当时jdk是1.8,就是执行报这个错;
解决方式:
添加这个静态代码块即可,但是需要引入jar包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.7</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.68</version>
</dependency>
static {
Security.addProvider(new BouncyCastleProvider());
}
例如AES解密加密代码:
// 引入依赖
import cn.hutool.crypto.symmetric.AES;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.Security;
// 代码
final static String Mode="加密模式";
final static String padding="填充模式";
final static byte[] Key ="密钥 KEY".getBytes();
final static byte[] Pwd ="密钥 KEY".getBytes();
static {
Security.addProvider(new BouncyCastleProvider());
}
/**
* 加密
* @param data
* @return
*/
public static String encryption(String data){
//AES(加密模式,填充方式,加密密钥不可更改 (16位),iv向量)
AES aes = new AES(Mode, padding,Key,Pwd);
// 加密为16进制表示
String encryptHex = aes.encryptHex(data);
return encryptHex;
}
/**
* 解密
* @param data
* @return
*/
public static String decryption(String data){
AES aes = new AES(Mode, padding,Key,Pwd);
// 加密为16进制表示
String decryptStr = aes.decryptStr(data);
return decryptStr;
}