npm install crypto-js –save
npm i jsencrypt –save
npm install –save sm-crypto
import CryptoJS from “crypto-js”
import RSA from ‘jsencrypt/bin/jsencrypt’
import sm3 from “sm-crypto/src/sm3”
function generateMixed(n:any) {
var jschars = [‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’, ‘q’, ‘w’, ‘e’, ‘r’, ‘t’, ‘y’, ‘u’, ‘i’, ‘o’, ‘p’, ‘a’, ‘s’, ‘d’, ‘f’, ‘g’, ‘h’, ‘j’, ‘k’, ‘l’, ‘z’, ‘x’, ‘c’, ‘v’, ‘b’, ‘n’, ‘m’];
var res = “”;
for (var i = 0; i < n; i++) {
var id = Math.ceil(Math.random() * 61);
res += jschars[id];
}
return res;
}
let key=generateMixed(16)
let key2=””
// AES加密
function Encrypt(word:any) {
key2=key
var srcs = CryptoJS.enc.Utf8.parse(word),
key1 = CryptoJS.enc.Utf8.parse(key);
var encrypted = CryptoJS.AES.encrypt(srcs, key1, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
}
// AES解密
export function Decrypt(word:any) {
var encryptedHexStr = CryptoJS.enc.Hex.parse(word);
var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
var key1 = CryptoJS.enc.Utf8.parse(key2);
var decrypt = CryptoJS.AES.decrypt(word, key1, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
// rsa加密
function RSAEncrypt() {
var publicKey_pkcs1 =’MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsxxINczpneVLYbpbVCREy2o0IQKOPVk8676DcwDtH4bmewKxaTk013yC4QCJ4nzsoPrCirVQ0bTr/CMzZnN2+CaGBOxtSYiMkeyPTmapIj74QGohMiaeqa5f2d6DlGkJ9lTcjUQzt/ljuXqgFlPBna/ZRIl7639ZdcyCXNnqflQybW7RZJEI7Kg304KMvvO/x1cugn/sISa/1EAvYswSZV19dgbroBIKX4unRQmmcZbPNWqXYk4nwPZe5XQ4M95iqXJ4J0MZ9Lu+ZOFTfqgYz4vXrJKKvXcEdKq6p6Vt/oqmhU2D1ec3xBuzYconpX8kRqmPfpDcq+rHn17ZhyOzYQIDAQAB’ ; //rsa公钥
var encrypt_rsa = new RSA();
encrypt_rsa.setPublicKey(publicKey_pkcs1);
var encStr = encrypt_rsa.encrypt(key)
return encStr;
}
//请求加密
export function setInto(data:any) {
var obj = new Object();
obj[“sign”] = sm3(JSON.stringify(data)) //MD5.hexMD5(data); //对应换成SM3
obj[“encryptData”] = Encrypt(data); //aes加密data后的值
obj[“encryptKey”] = RSAEncrypt(); //rsa非对称加密
return obj;
}
form表单提交的参数为:setInto(finalParams) //finalParams为post请求的原来data
get获取接口的response:Decrypt(response.data)
本文来自 余 倩倩 投稿,不代表 PmTemple 立场,如若转载,请注明出处:
。如有涉及侵权行为,请发送相关证明材料至邮箱admin@pmtemple.com