Skip to content

Instantly share code, notes, and snippets.

@tgoldenberg
Last active March 24, 2018 02:57
Show Gist options
  • Save tgoldenberg/9fca82fcacddf5030d8d3dd676e7bc80 to your computer and use it in GitHub Desktop.
Save tgoldenberg/9fca82fcacddf5030d8d3dd676e7bc80 to your computer and use it in GitHub Desktop.
Encrypt messages with ECIES
import decode from 'jwt-decode';
import jwt from 'njwt';
import { ec } from 'elliptic';
var eccrypto = require('eccrypto');
var crypto = require('crypto');
// sender
var privKey1 = crypto.randomBytes(32);
var pubKey1 = eccrypto.getPublic(privKey1);
// recipient
var privKey2 = crypto.randomBytes(32);
var pubKey2 = eccrypto.getPublic(privKey2);
// create web token of JSON content
var message = "Hello world";
var token = njwt.create({ message }, 'HS512').compact();
// encrypto token and send to recipient
eccrypto.encrypt(pubKey2, Buffer(token)).then(function(encrypted) {
console.log('> Encrypted message: ', encrypted);
// decrypt from recipient end
eccrypto.decrypt(privKey2, encrypted).then(function(content) {
console.log('> Decrypted message: ', content);
// decode web token
var decoded = decode(content);
console.log('> Decoded token: ', decoded);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment