-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
53 lines (51 loc) · 1.87 KB
/
node.js
File metadata and controls
53 lines (51 loc) · 1.87 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
49
50
51
52
53
var crypto = require('crypto');
var key = '12345678901234567890123456789012';
var data = "13245678901";
function test() {
var aesutil = {}; // module.exports = {};
/**
* aes加密
* @param data 待加密内容
* @param key 必须为32位私钥
* @returns {string}
*/
aesutil.encryption = function(data, key, iv) {
iv = iv || "";
var clearEncoding = 'utf8';
var cipherEncoding = 'base64';
var cipherChunks = [];
var cipher = crypto.createCipheriv('aes-256-ecb', key, iv);
cipher.setAutoPadding(true);
cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
cipherChunks.push(cipher.final(cipherEncoding));
return cipherChunks.join('');
}
/**
* aes解密
* @param data 待解密内容
* @param key 必须为32位私钥
* @returns {string}
*/
aesutil.decryption = function(data, key, iv) {
if (!data) {
return "";
}
iv = iv || "";
var clearEncoding = 'utf8';
var cipherEncoding = 'base64';
var cipherChunks = [];
var decipher = crypto.createDecipheriv('aes-256-ecb', key, iv);
decipher.setAutoPadding(true);
data = data.replace('*', '+'); //替换+ 特殊符号,与前端加密保持一致
cipherChunks.push(decipher.update(data, cipherEncoding, clearEncoding));
cipherChunks.push(decipher.final(clearEncoding));
return cipherChunks.join('');
}
var e = aesutil.encryption(data, key, '');
var de = aesutil.decryption(e, key, '');
var de2 = aesutil.decryption('B8yTZsiyPZdQZ+Qj1OhKQg==', key, '');
console.log('encode: ', e);
console.log('decode: ', de);
console.log('decode de2: ', de2);
}
test();