首先引入hutool依赖
代码语言:javascript复制 <dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.1</version>
</dependency>
先通过网上随机生成一组密钥对(公钥与私钥),分别定义为PUBLIC_KEY、PRIVATE_KEY,随后进行如下操作(以下操作是公钥加密,私钥解密,如果想反过来同理):
代码语言:javascript复制private static final RSA rsa_en = new RSA(null,PUBLIC_KEY);
private static final RSA rsa_de = new RSA(PRIVATE_KEY,null);
//加密
byte[] encrypt = rsa_en.encrypt(data,KeyType.PublicKey);
//解密
byte[] decrypt = rsa_de.decrypt(encrypt, KeyType.PrivateKey);