Skip to content

Instantly share code, notes, and snippets.

@Terkea
Created March 30, 2021 22:02
Show Gist options
  • Save Terkea/4dff33ba9fa2fe7b44bbb41d94b81fb8 to your computer and use it in GitHub Desktop.
Save Terkea/4dff33ba9fa2fe7b44bbb41d94b81fb8 to your computer and use it in GitHub Desktop.
ECDH and AES 256 in NODE JS
const crypto = require('crypto');
const assert = require('assert')
// THIS SECTION COVERS THE KEY GENERATION
// AND PROOFS THAT THE SHARES KEYS ARE IDENTIAL
// SOURCE: https://asecuritysite.com/encryption/js_ecdh
// DOCS: https://nodejs.org/api/crypto.html#crypto_crypto_createecdh_curvename
type = 'secp256k1';
// a list with all the curves that can be used
// idk yet what those are but investigate
// console.log(crypto.getCurves())
console.log("Type:\t",type);
// Generate Veronica's keys...
const veronica = crypto.createECDH(type);
const veronicaKey = veronica.generateKeys();
// Generate Marian's keys...
const marian = crypto.createECDH(type);
const marianKey = marian.generateKeys();
console.log("\nVeronica private key:\t",veronica.getPrivateKey().toString('hex'));
console.log("Veronica public key:\t",veronicaKey.toString('hex'))
console.log("\nMarian private key:\t",marian.getPrivateKey().toString('hex'));
console.log("Marian public key:\t",marianKey.toString('hex'));
// Exchange and generate the secret...
const veronicaSecret = veronica.computeSecret(marianKey);
const marianSecret = marian.computeSecret(veronicaKey);
console.log("\nVeronica shared key:\t",veronicaSecret.toString('hex'))
console.log("Marian shared key:",marianSecret.toString('hex'));
try{
assert.deepStrictEqual(veronicaSecret, marianSecret, 'missmatch')
console.log("\n --- KEYS MATCH ---\n")
}catch(e){
console.error("\n --- KEYS MISSMATCH ---\n")
}
// THIS SECTION COVERS THE ENCRYPTION OF
// A GIVEN MESSAGE USING THE NEWLY GENERATED KEYS
// SOURCE: https://gist.github.com/siwalikm/8311cf0a287b98ef67c73c1b03b47154
// DOCS: https://nodejs.org/api/crypto.html#crypto_crypto_createcipheriv_algorithm_key_iv_options
// openssl list -cipher-algorithms
// will display the available cipher algorithms.
const phrase = "ciao bella";
const IV = crypto.randomBytes(16); // Initialization vector.
//https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_(CBC)
const algorithm = 'aes-256-cbc'
console.log('ALGORITHM ', algorithm)
var encrypt = ((val, key) => {
let cipher = crypto.createCipheriv(algorithm, key, IV);
let encrypted = cipher.update(val, 'utf8', 'base64');
encrypted += cipher.final('base64');
return encrypted;
});
var decrypt = ((encrypted, key) => {
let decipher = crypto.createDecipheriv(algorithm, key, IV);
let decrypted = decipher.update(encrypted, 'base64', 'utf8');
return (decrypted + decipher.final('utf8'));
});
encrypted_text = encrypt(phrase, marianSecret);
original_phrase = decrypt(encrypted_text, marianSecret);
console.log('ENCRYPTED: ', encrypted_text);
console.log('DECRYPTED: ', original_phrase);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment