-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes.js
43 lines (39 loc) · 1.34 KB
/
aes.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
38
39
40
41
42
43
var crypto = require('crypto');
var aesutil = module.exports = {};
/**
* aes加密
* @param data 待加密内容
* @param key 必须为32位私钥
* @returns {string}
*/
aesutil.encryption = function (data, iv) {
iv = iv || "";
var clearEncoding = 'utf8';
var cipherEncoding = 'base64';
var cipherChunks = [];
var cipher = crypto.createCipheriv('aes-256-ctr', crypto.pbkdf2Sync(iv, '', 100000, 32, 'sha512'), crypto.createHash('sha1').update(iv).digest().slice(0, 16));
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, iv) {
if (!data) {
return "";
}
iv = iv || "";
var clearEncoding = 'utf8';
var cipherEncoding = 'base64';
var cipherChunks = [];
var decipher = crypto.createDecipheriv('aes-256-ctr', crypto.pbkdf2Sync(iv, '', 100000, 32, 'sha512'), crypto.createHash('sha1').update(iv).digest().slice(0, 16));
decipher.setAutoPadding(true);
cipherChunks.push(decipher.update(data, cipherEncoding, clearEncoding));
cipherChunks.push(decipher.final(clearEncoding));
return cipherChunks.join('');
};