前面说过接口数据在后端如何使用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;
@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 keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024, new SecureRandom());
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();
}
}
public static String encrypt(String str, String publicKey) {
try {
Base64.Decoder decoder = Base64.getDecoder();
Base64.Encoder encoder = Base64.getEncoder();
byte[] decoded = decoder.decode(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
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;
}
}
public static String decrypt(String str) {
try {
Base64.Decoder decoder = Base64.getDecoder();
byte[] inputByte = decoder.decode(str.getBytes("UTF-8"));
byte[] decoded = decoder.decode(privateKeyStr);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
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(),
},
created() {
this.getPublicKey();
},
methods: {
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)
|