-
Notifications
You must be signed in to change notification settings - Fork 41
/
encrypt.js
197 lines (181 loc) · 4.62 KB
/
encrypt.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const crypto = require('crypto');
const { merge_sort } = require('./merge_sort');
const int32Max = Math.pow(2, 32);
const cachedTables = {}; // password: [encryptTable, decryptTable]
const getTable = function(key) {
if (cachedTables[key]) {
return cachedTables[key];
}
console.log('calculating ciphers');
let table = new Array(256);
const decrypt_table = new Array(256);
const md5sum = crypto.createHash('md5');
md5sum.update(key);
const hash = new Buffer(md5sum.digest(), 'binary');
const al = hash.readUInt32LE(0);
const ah = hash.readUInt32LE(4);
let i = 0;
while (i < 256) {
table[i] = i;
i++;
}
i = 1;
while (i < 1024) {
table = merge_sort(
table,
(x, y) =>
((ah % (x + i)) * int32Max + al) % (x + i) -
((ah % (y + i)) * int32Max + al) % (y + i)
);
i++;
}
i = 0;
while (i < 256) {
decrypt_table[table[i]] = i;
++i;
}
const result = [table, decrypt_table];
cachedTables[key] = result;
return result;
};
const substitute = function(table, buf) {
let i = 0;
while (i < buf.length) {
buf[i] = table[buf[i]];
i++;
}
return buf;
};
const bytes_to_key_results = {};
const EVP_BytesToKey = function(password, key_len, iv_len) {
if (bytes_to_key_results[`${password}:${key_len}:${iv_len}`]) {
return bytes_to_key_results[`${password}:${key_len}:${iv_len}`];
}
const m = [];
let i = 0;
let count = 0;
while (count < key_len + iv_len) {
const md5 = crypto.createHash('md5');
let data = password;
if (i > 0) {
data = Buffer.concat([m[i - 1], password]);
}
md5.update(data);
const d = md5.digest();
m.push(d);
count += d.length;
i += 1;
}
const ms = Buffer.concat(m);
const key = ms.slice(0, key_len);
const iv = ms.slice(key_len, key_len + iv_len);
bytes_to_key_results[password] = [key, iv];
return [key, iv];
};
const method_supported = {
'aes-128-cfb': [16, 16],
'aes-192-cfb': [24, 16],
'aes-256-cfb': [32, 16],
'bf-cfb': [16, 8],
'camellia-128-cfb': [16, 16],
'camellia-192-cfb': [24, 16],
'camellia-256-cfb': [32, 16],
'cast5-cfb': [16, 8],
'des-cfb': [8, 8],
'idea-cfb': [16, 8],
'rc2-cfb': [16, 8],
rc4: [16, 0],
'rc4-md5': [16, 16],
'seed-cfb': [16, 16]
};
const create_rc4_md5_cipher = function(key, iv, op) {
const md5 = crypto.createHash('md5');
md5.update(key);
md5.update(iv);
const rc4_key = md5.digest();
if (op === 1) {
return crypto.createCipheriv('rc4', rc4_key, '');
} else {
return crypto.createDecipheriv('rc4', rc4_key, '');
}
};
class Encryptor {
constructor(key, method) {
this.key = key;
this.method = method;
this.iv_sent = false;
if (this.method === 'table') {
this.method = null;
}
if (this.method) {
this.cipher = this.get_cipher(
this.key,
this.method,
1,
crypto.randomBytes(32)
);
} else {
[this.encryptTable, this.decryptTable] = getTable(this.key);
}
}
get_cipher_len(method) {
method = method.toLowerCase();
return method_supported[method];
}
get_cipher(password, method, op, iv) {
method = method.toLowerCase();
password = new Buffer(password, 'binary');
const m = this.get_cipher_len(method);
if (m) {
const [key, iv_] = EVP_BytesToKey(password, m[0], m[1]);
if (!iv) {
iv = iv_;
}
if (op === 1) {
this.cipher_iv = iv.slice(0, m[1]);
}
iv = iv.slice(0, m[1]);
if (method === 'rc4-md5') {
return create_rc4_md5_cipher(key, iv, op);
} else {
if (op === 1) {
return crypto.createCipheriv(method, key, iv);
} else {
return crypto.createDecipheriv(method, key, iv);
}
}
}
}
encrypt(buf) {
if (this.method) {
const result = this.cipher.update(buf);
if (this.iv_sent) {
return result;
} else {
this.iv_sent = true;
return Buffer.concat([this.cipher_iv, result]);
}
} else {
return substitute(this.encryptTable, buf);
}
}
decrypt(buf) {
if (this.method) {
let result;
if (!this.decipher) {
const decipher_iv_len = this.get_cipher_len(this.method)[1];
const decipher_iv = buf.slice(0, decipher_iv_len);
this.decipher = this.get_cipher(this.key, this.method, 0, decipher_iv);
result = this.decipher.update(buf.slice(decipher_iv_len));
return result;
} else {
result = this.decipher.update(buf);
return result;
}
} else {
return substitute(this.decryptTable, buf);
}
}
}
exports.Encryptor = Encryptor;
exports.getTable = getTable;