前面说过接口数据在后端如何使用RSA加解密
这里遇到了使用RSA前端加密后端解密的需求。
实现方式如下:
1、后端的RSA工具
package com.ieslab.interactivequery.util;
import org.springframework.stereotype.Service;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
/**
* @description: RSA加密工具
* @author: yiwenli
* @create: 2021-04-29 09:52
**/
@Service
public class RSAUtil {
// 私钥字符串
private static String privateKeyStr;
// 公钥字符串
private static String publicKeyStr;
public static String getPublicKeyStr() {
return publicKeyStr;
}
/**
* 获取公钥私钥
*/
public static void initKey() {
try {
Base64.Encoder encoder = Base64.getEncoder();
// KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
// 初始化密钥对生成器,密钥大小为96-1024位
keyPairGen.initialize(1024, new SecureRandom());
// 生成一个密钥对,保存在keyPair中
KeyPair keyPair = keyPairGen.generateKeyPair();
// 得到私钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
// 得到公钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
privateKeyStr = new String(encoder.encode((privateKey.getEncoded())));
publicKeyStr = new String(encoder.encode(publicKey.getEncoded()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
/**
* RSA公钥加密
*
* @param str 加密字符串
* @param publicKey 公钥
*/
public static String encrypt(String str, String publicKey) {
try {
Base64.Decoder decoder = Base64.getDecoder();
Base64.Encoder encoder = Base64.getEncoder();
//base64编码的公钥
byte[] decoded = decoder.decode(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
String outStr = encoder.encodeToString(cipher.doFinal(str.getBytes("UTF-8")));
return outStr;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* RSA私钥解密
*
* @param str 加密字符串
*/
public static String decrypt(String str) {
try {
Base64.Decoder decoder = Base64.getDecoder();
//64位解码加密后的字符串
byte[] inputByte = decoder.decode(str.getBytes("UTF-8"));
//base64编码的私钥
byte[] decoded = decoder.decode(privateKeyStr);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
String outStr = new String(cipher.doFinal(inputByte));
return outStr;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
2、项目启动后调用初始化公私钥方法
public class XXXApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(XXXApplication .class);
springApplication.run(args);
RSAUtil.initKey();
}
3、写一个给前台提供公钥的接口
/**
* 获取数据加密公钥
*/
@RequestMapping(value = "/getPublicKeyStr", produces = "application/json;charset=utf-8")
public String getPublicKeyStr() {
return Result.success(RSAUtil.getPublicKeyStr()).toJsonString();
}
4、前端html引用jsencrypt工具包
<script src="/XXX/plugins/jsencrypt.min.js"></script>
5、JS中创建JSEncrypt对象并设置公钥值
new Vue({
el: "#app",
data() {
encrypt: new JSEncrypt(), // JSEncrypt对象
},
created() {
this.getPublicKey();
},
methods: {
/**
* 获取数据加密公钥给encrypt赋值
*/
getPublicKey() {
utils.getData(`${config.baseUrl}/transManage/getPublicKeyStr`, {}).then(res => {
this.encrypt.setPublicKey(res);
}).catch(e => {
})
},
},
})
6、前端加密时使用
xxx = this.encrypt.encrypt(xxx)
7、后端解析
RSAUtil.decrypt(xxx)