-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextractAES.js
49 lines (41 loc) · 1.47 KB
/
extractAES.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
var mcrypt = require('js-rijndael');
function extractAESKey(buffer, rsaPrivateKey) {
var rsaEncryptedAesKeySize = buffer.readInt32LE(0);
var rsaEncryptedAesKey = new Buffer(rsaEncryptedAesKeySize);
buffer.copy(rsaEncryptedAesKey, 0, 4, rsaEncryptedAesKeySize + 4);
var ivKeySize = buffer.readInt32LE(4+rsaEncryptedAesKeySize);
var ivKey = new Buffer(ivKeySize);
buffer.copy(ivKey, 0, rsaEncryptedAesKeySize+8, ivKeySize + rsaEncryptedAesKeySize+8);
var aesKey = rsaPrivateKey.decrypt(rsaEncryptedAesKey);
return {key: aesKey, iv: ivKey};
}
function insertAESKey(payload, aesInfo, rsaPublicKey) {
var encryptedPublicKey = rsaPublicKey.encrypt(aesInfo.key);
var result = Buffer.concat([
new Buffer("01", "hex"),
new Buffer("00010000", "hex"),
encryptedPublicKey,
new Buffer("20000000", "hex"),
aesInfo.iv,
payload
])
return result;
}
function decrypt(encrypted, aesInfo) {
var encryptedArray = encrypted.toJSON().data;
var iv = aesInfo.iv.toJSON().data;
var key = aesInfo.key.toJSON().data;
return new Buffer(mcrypt.decrypt(encryptedArray, iv, key, 'rijndael-256', 'cbc'));
}
function encrypt(clearText, aesInfo) {
var clearTextArray = clearText.toJSON().data;
var iv = aesInfo.iv.toJSON().data;
var key = aesInfo.key.toJSON().data;
return new Buffer(mcrypt.encrypt(clearTextArray, iv, key, 'rijndael-256', 'cbc'));
}
module.exports = {
extractAESKey: extractAESKey,
insertAESKey: insertAESKey,
decrypt: decrypt,
encrypt: encrypt
}