-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecies.js
More file actions
52 lines (39 loc) · 1.77 KB
/
ecies.js
File metadata and controls
52 lines (39 loc) · 1.77 KB
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
var eccrypto = require("eccrypto");
var CryptoJS = require("crypto-js");
console.log('in ECC');
function eci (req, res, next) {
console.log('in ECC');
var privateKeyA = eccrypto.generatePrivate();
var publicKeyA = eccrypto.getPublic(privateKeyA);
var privateKeyB = eccrypto.generatePrivate();
var publicKeyB = eccrypto.getPublic(privateKeyB);
// Encrypting the message for B.
const msg = "John 22 4432323";
var result="";
eccrypto.encrypt(publicKeyB,msg).then(function(encrypted) {
// B decrypting the message.
console.log(encrypted);
eccrypto.decrypt(privateKeyB, encrypted).then(function(plaintext) {
console.log('inside decrypt');
result+="Message to part B:"+plaintext.toString();
// res.send("Message to part B:"+plaintext.toString()+"privB"+privateKeyB.toString('hex'));
}).catch(function() {
console.log("Decrypt is BAD")});
}).catch(function() {
console.log("Encrypt is BAD")});
// Encrypting the message for A.
eccrypto.encrypt(publicKeyA, Buffer.from(msg)).then(function(encrypted) {
// A decrypting the message.
eccrypto.decrypt(privateKeyA, encrypted).then(function(plaintext) {
result+="</h1></div><div><h1>Message to part A:"+plaintext.toString()+'</h1></div>';
res.send('<div><h1>hello</h1></div><div><h1>Eccrypto working!</h1></div><div><h1>'+result+'<div><h1>CryptoJS working!</h1></div><div><h1>'+JSON.stringify(decryptedData)+'</h1></div>');
});
});
var data = [{"id": "1","name":"john","card":"1234123412341234"}]
// Encrypt
var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data),"God is Great" );
// Decrypt
var bytes = CryptoJS.AES.decrypt(ciphertext.toString(), "God is Great");
var decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
}
module.exports = eci;