encrypt-util.js
1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import CryptoJS from "crypto-js";
import JsEncrypt from "jsencrypt";
let rsa = new JsEncrypt();
/*
* 创建加密
* @param data
*
* 设置项
* key 公钥
* iv 偏移量
*/
export function makeEncrypt(param) {
let key = process.env.VUE_APP_RSA_PUBLIC_KEY; // process.env.AES_KEY
let iv = process.env.VUE_APP_AES_IV; // process.env.AES_IV
rsa.setPublicKey(key);
let chars = "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".split(",");
function randomAesKey() {
let res = "";
for (let i = 0; i < 16; i++) {
let id = Math.ceil(Math.random() * (chars.length - 1));
res += chars[id];
}
return res;
};
function encryptContent(obj) {
let aesKey = randomAesKey();
let key = CryptoJS.enc.Utf8.parse(aesKey);
let ivR = CryptoJS.enc.Utf8.parse(iv);
var encrypted = CryptoJS.AES.encrypt(JSON.stringify(obj), key, {
iv: ivR,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
let content = encrypted.toString(); //返回的是base64格式的密文
let securityKey = rsa.encrypt(aesKey);
let result = {
k: securityKey,
v: content
};
return result;
};
let data = encryptContent(param)
return data;
}