-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryptionModule.js
More file actions
42 lines (33 loc) · 995 Bytes
/
encryptionModule.js
File metadata and controls
42 lines (33 loc) · 995 Bytes
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
var NodeRSA = require('node-rsa');
var CryptoJS = require('crypto-js');
module.exports = {
encryptAES: function(data, key) {
var enc = CryptoJS.AES.encrypt(JSON.stringify(data), key);
return enc.toString();
},
decryptAES: function(data, key) {
var dec = CryptoJS.AES.decrypt(data.toString(), key)
var text = dec.toString(CryptoJS.enc.Utf8);
return text.substring(1, text.length - 1)
},
generateKeyPair: function() {
var key = new NodeRSA({
b: 512
});
var publicKey = key.exportKey("public");
var privateKey = key.exportKey("private");
return [privateKey, publicKey];
},
encryptRSA: function(data, key) {
var publicKey = new NodeRSA();
publicKey.importKey(key, "public");
var enc = publicKey.encrypt(data);
return enc;
},
decryptRSA: function(data, key) {
var privateKey = new NodeRSA();
privateKey.importKey(key, "private");
var dec = privateKey.decrypt(data);
return dec.toString();
}
}