-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes gcm.js
112 lines (94 loc) · 2.81 KB
/
aes gcm.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* @author eric
* @date 2022-3-1
* @description AES-GCM模式加密实现
* 直接作为一个类导入使用即可
* encodeParams为加密方法
* decodeParams为解密方法
*/
import crypto from 'crypto';
const aesKey = 'eric';
/**
* 生成对应长度的字符串
* @param { number } length 长度
* @returns { string } 生成的字符串
*/
function generateIv(length) {
const base64Charsets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
let str = '';
for (let i = 0; i < length; i++) {
const index = Math.floor(Math.random() * base64Charsets.length);
str += base64Charsets[index];
}
return str;
}
/**
* 加密方法
* @param { string } word 需要加密的字符串
* @returns { string } 加密后的字符串
*/
function encodeAes(word) {
/* 生成12位偏移量 */
const iv = generateIv(12);
/* 根据key和iv对字符串进行加密 */
const cipher = crypto.createCipheriv('aes-128-gcm', aesKey, iv);
const encrypted = cipher.update(word, 'utf8');
const finalstr = cipher.final();
const auth = cipher.getAuthTag();
let res = Buffer.concat([encrypted, finalstr, auth]);
/* 生成偏移量iv的buffer */
const ivbuffer = Buffer.from(iv, 'utf8');
/* 偏移量buffer拼接到加密buffer最前面 */
res = Buffer.concat([ivbuffer, res]);
return res.toString('base64');
}
/**
* 解密方法
* @param { string } word 需要解密的字符串
* @returns { string } 解密后的字符串
*/
function decodeAes(word) {
let decryptedStr;
try {
/* base64字符串转buffer */
const wordBuffer = Buffer.from(word, 'base64');
/* 截取前12位作为iv */
const iv = wordBuffer.slice(0, 12);
/* 截取剩余部分作为加密字符串 */
const buffer = wordBuffer.slice(12, wordBuffer.length);
/* 字符串解密 */
const decipher = crypto.createDecipheriv('aes-128-gcm', aesKey, iv);
const auth = buffer.subarray(buffer.length - 16);
decipher.setAuthTag(auth);
const str = decipher.update(Buffer.from(buffer.subarray(0, buffer.length - 16), 'hex'));
const fin = decipher.final();
decryptedStr = new TextDecoder('utf8').decode(Buffer.concat([str, fin]));
} catch (e) {
console.log('AES decodeAes error word-----------' + word);
return word.toString();
}
try {
decryptedStr = JSON.parse(decryptedStr);
} catch (e) {
console.log('AES JSON parse error word-----------' + word);
}
return decryptedStr.toString()
}
/* 加密之前的统一拦截 */
function encodeParams(origin) {
if (!origin) {
return '';
}
if (typeof origin != 'string') {
origin = JSON.stringify(origin);
}
return encodeAes(origin);
}
/* 解密之前的统一拦截 */
function decodeParams(params) {
if (!params) {
return '';
}
return decodeAes(params);
}
export { encodeParams, decodeParams };