Skip to content

Instantly share code, notes, and snippets.

@xardit
Created November 4, 2018 10:21
Show Gist options
  • Save xardit/b8ed341cba8b5c87516b8b67a84d1401 to your computer and use it in GitHub Desktop.
Save xardit/b8ed341cba8b5c87516b8b67a84d1401 to your computer and use it in GitHub Desktop.
node.js crypto simple aes 256 encryption
const crypto = require('crypto')
var sharedSecret = crypto.randomBytes(32); // should be 128 (or 256) bits
var initializationVector = crypto.randomBytes(16); // IV is always 16-bytes
// var initializationVector = '0123456789abcdef'
var plaintext = "Everything's gonna be 200 OK!";
var encrypted;
cipher = crypto.Cipheriv('aes-256-cbc', sharedSecret, initializationVector);
encrypted += cipher.update(plaintext, 'utf8', 'base64');
encrypted += cipher.final('base64');
// I would need to send both the IV and the Encrypted text to my friend
// { iv: initializationVector.toString('base64')
// , cipherText: encrypted
// }
console.log(encrypted)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment