-
Notifications
You must be signed in to change notification settings - Fork 1
/
aes-ecb.js
97 lines (79 loc) · 2.25 KB
/
aes-ecb.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
/**
*
* @authors Your Name (you@example.org)
* @date 2019-05-26 17:32:15
* @version $Id$
*/
var crypto = require('crypto');
function encryptTo128(key, text, outEncoding) {
if (key.length != 16) {
throw new Error('the key length must 16');
}
return encrypt('aes-128-ecb', text, key, outEncoding);
}
function encryptTo192(key, text, outEncoding) {
if (key.length != 24) {
throw new Error('the key length must 24');
}
return encrypt('aes-192-ecb', text, key, outEncoding);
}
function encryptTo256(key, text, outEncoding) {
if (key.length != 32) {
throw new Error('the key length must 32');
}
return encrypt('aes-256-ecb', text, key, outEncoding);
}
function encrypt(algorithm, text, key, outEncoding) {
var encoding = 'hex';
if (!outEncoding) {
encoding = outEncoding || encoding;
}
cipher = crypto.createCipheriv(algorithm, key, '');
result = cipher.update(text).toString(encoding);
result += cipher.final().toString(encoding);
return result;
}
function decrypt(algorithm, key, encryptText, encryptEncoding, outEncoding) {
var defEncryptEncoding = 'hex';
if (!encryptEncoding) {
encryptEncoding = encryptEncoding || defEncryptEncoding;
}
var defOutEncoding = 'utf8';
if (!outEncoding) {
outEncoding = outEncoding || defOutEncoding;
}
var deCipher = crypto.createDecipheriv(algorithm, key, '');
var result = deCipher.update(encryptText, encryptEncoding);
result += deCipher.final().toString(outEncoding);
return result;
}
function decrypt128(key, text, encryptEncoding, outEncoding) {
if (key.length != 16) {
throw new Error('the key length must 16');
}
return decrypt('aes-128-ecb', key, text, encryptEncoding, outEncoding);
}
function decrypt192(key, text, encryptEncoding, outEncoding) {
if (key.length != 24) {
throw new Error('the key length must 24');
}
return decrypt('aes-192-ecb', key, text, encryptEncoding, outEncoding);
}
function decrypt256(key, text, encryptEncoding, outEncoding) {
if (key.length != 32) {
throw new Error('the key length must 32');
}
return decrypt('aes-256-ecb', key, text, encryptEncoding, outEncoding);
}
module.exports = {
encrypt: {
bit128: encryptTo128,
bit192: encryptTo192,
bit256: encryptTo256
},
decrypt: {
bit128: decrypt128,
bit192: decrypt192,
bit256: decrypt256
}
}