Created
November 12, 2016 16:19
-
-
Save perry-mitchell/0d4a1da2348eac2bbd9dcd5761c3877c to your computer and use it in GitHub Desktop.
ECDH key exchange with AES encryption using iocane
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To use this, you will need to `npm install iocane`! | |
const crypto = require("crypto"); | |
let aliceECDH = crypto.createECDH("secp256k1"); | |
aliceECDH.generateKeys(); | |
let alicePublicKey = aliceECDH.getPublicKey(null, "compressed"), | |
alicePrivateKey = aliceECDH.getPrivateKey(null, "compressed"); | |
console.log("Alice Public: ", alicePublicKey.length, alicePublicKey.toString("hex")); | |
console.log("Alice Private:", alicePrivateKey.length, alicePrivateKey.toString("hex")); | |
let bobECDH = crypto.createECDH("secp256k1"); | |
bobECDH.generateKeys(); | |
let bobPublicKey = bobECDH.getPublicKey(null, "compressed"), | |
bobPrivateKey = bobECDH.getPrivateKey(null, "compressed"); | |
console.log("Bob Public: ", bobPublicKey.length, bobPublicKey.toString("hex")); | |
console.log("Bob Private: ", bobPrivateKey.length, bobPrivateKey.toString("hex")); | |
// On Alice's side | |
let secret1 = aliceECDH.computeSecret(bobPublicKey); | |
console.log("Alice Secret: ", secret1.length, secret1.toString("hex")); | |
// On Bob's side | |
let secret2 = bobECDH.computeSecret(alicePublicKey); | |
console.log("Bob Secret: ", secret2.length, secret2.toString("hex")); | |
const iocane = require("iocane").crypto; | |
iocane | |
.encryptWithPassword("Hi there, Bob!", secret1) | |
.then(function(encrypted) { | |
console.log("Encrypted:", encrypted); | |
// send to Bob | |
iocane | |
.decryptWithPassword(encrypted, secret1) | |
.then(function(message) { | |
console.log("Decrypted:", message); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment