-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRSAAESUtils.js
37 lines (33 loc) · 1.17 KB
/
RSAAESUtils.js
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
/**
* Created by Administrator on 2018/4/8.
*/
const RSAClient = require('./lib/rsa-client');//客户端rsa加密
const base64 = require('./lib/base64');
const CryptoJS = require('crypto-js');
module.exports = {
//默认使用cbc,Pkcs7 (the default)
encode: (publickey, data,randmoKey) => {
if (typeof(data) === 'object') {
data = JSON.stringify(data);
} else {
data = data.toString();
}
var _publicKey = new RSAClient();
_publicKey.setPublic(publickey.n, publickey.e);
const enKey = base64.hex2b64(_publicKey.encrypt(randmoKey));
const key = CryptoJS.enc.Utf8.parse(randmoKey);
const encryptedData = CryptoJS.AES.encrypt(data, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return {data: encryptedData.toString(), key: enKey};
},
decode: (data,randmoKey) => {
const key = CryptoJS.enc.Utf8.parse(randmoKey);
const bytes = CryptoJS.AES.decrypt(data.toString(), key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return bytes.toString(CryptoJS.enc.Utf8);
}
}