1 配置文件引入 加密包 package.json
"jsencrypt": "^3.0.0-rc.1",
2 加密公钥配置
javascript
import { JSEncrypt } from 'jsencrypt'
import request from "@/utils/request";
const RSA_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJVol0cJoJKA36lRJoCOe7IZNhWm/BY+zOumtVQ/uhw4+oIJQqbJ+xuAYED5VjUWGsKjqJWKAS1QuBpQNij+nUXUV1v7AB9r0ihBTwvvvVJSLOfEau6eeKEqnAW6X4bZvaexFKTxlAb7IumnlBoo8uP5bCyRt93BuseuHynoYrmwIDAQAB";
// 加密
export function encryptedData(data) {
// 新建JSEncrypt对象
let encryptor = new JSEncrypt();
// 设置公钥
encryptor.setPublicKey(RSA_PUBLIC_KEY);
// 加密数据
return encryptor.encrypt(data);
};
3 前端请求 加密传输敏感数据
javascript
import { encryptedData } from "@/utils/auth";
handleSubmit(e) {
const { $store } = this;
e.preventDefault();
this.form.validateFields(async (err, values) => {
if (!err) {
await $store.dispatch({
type: "login/sumbitLogin",
params: {
userInfo: encryptedData(values.userName),
password: encryptedData(values.password)
}
});
this.$router.push({ path: "/home" });
}
});
}
4 后端接口 解密敏感数据
java
final String loginName = RSAUtil.decrypt(userInfo, RSAUtil.DEFAULT_PRIVATE_KEY);
后端解密工具包
java
package c;
import org.apache.commons.codec.binary.Base64;
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.HashMap;
import java.util.Map;
/**
* RSA 签名算法组件
*
*
*/
public class RSAUtil {
public static String DEFAULT_PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIlWiXRwmgkoDfqVEmgI57shk2Fab8Fj7M66a1VD+6HDj6gglCpsn7G4BgQPlWNRYawqOolYoBLVC4GlA2KP6dRdRXW/sAH2vSKEFPC++9UlIs58Rq7p54oSqcBbpfhtm9p7EUpPGUBvsi6aeUGijy4/lsLJG33cG6x64fKehiubAgMBAAECgYB1JrxZ9zNuXByQhFkPJYmGtz1g7iQmc3atYZftQ/CCLK9+W0wIZGBU2QVIsYJdFi6bDeqg/liNt+34ySn6CYkLrtEPhJ9WvTg81v1b2aMun91rL+HvnP3mx5xUqrzBZeY7YHaNAnIiX8XoYo1LgvpQlJqSeFZzMQIAKAwqsh7CAQJBAP8rO5O4ooGoyYLqj9d4GxVtj4/3Waf8ysNC7xc55jFJWFk2c/MU1f8ZXp3fduNSP7GMsDElIz3uAJGXBcbZwdMCQQCJyQ2r+LaEGq0MCSzHR+bjQI1CadvIhcQe4jf1EN6keaC6UVLyqT9sKbM6DAbArTxxtOb8TJyGfSmYDgbJkAoZAkEA3qd4ywOXOZBwlb3bWebBq0b4uGTbZMwemZd0TU4GYQdhAieTsY6C1J/sgdGWSfFhBqp+q8N+JvHgqSmgrs0gZQJAajhHxOrA8ciK3Tt5fMuucEUEYU2wdL7CubhVwSRXL17gjKJDGvVlAmKHW9mHnyQt886tP5VkGb4FwcYWb28OiQJAEPAasVoq+Cls2VFiFY3kZ8eEOYHxdamIIyNH9hFv07eZEyq63ZTXcbFhOwMeE5W2JowIbTVwmMkWT7pPSM9sXw==";
public static String DEFALUT_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJVol0cJoJKA36lRJoCOe7IZNhWm/BY+zOumtVQ/uhw4+oIJQqbJ+xuAYED5VjUWGsKjqJWKAS1QuBpQNij+nUXUV1v7AB9r0ihBTwvvvVJSLOfEau6eeKEqnAW6X4bZvaexFKTxlAb7IumnlBoo8uP5bCyRt93BuseuHynoYrmwIDAQAB";
/**
* 随机生成密钥对
* @throws NoSuchAlgorithmException
*/
public static Map<Integer, String> genKeyPair() throws NoSuchAlgorithmException {
// 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(); // 得到公钥
String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
// 得到私钥字符串
String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));
// 将公钥和私钥保存到Map
Map<Integer, String> keyMap = new HashMap<>(); //用于封装随机产生的公钥与私钥
keyMap.put(0,publicKeyString); //0表示公钥
keyMap.put(1,privateKeyString); //1表示私钥
return keyMap;
}
/**
* RSA公钥加密
*
* @param str
* 加密字符串
* @param publicKey
* 公钥
* @return 密文
* @throws Exception
* 加密过程中的异常信息
*/
public static String encrypt( String str, String publicKey ) throws Exception{
//base64编码的公钥
byte[] decoded = Base64.decodeBase64(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
}
/**
* RSA私钥解密
*
* @param str
* 加密字符串
* @param privateKey
* 私钥
* @return 铭文
* @throws Exception
* 解密过程中的异常信息
*/
public static String decrypt(String str, String privateKey) throws Exception {
//64位解码加密后的字符串
byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
//base64编码的私钥
byte[] decoded = Base64.decodeBase64(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
return new String(cipher.doFinal(inputByte));
}
}