Skip to content

Instantly share code, notes, and snippets.

@riston
Created June 11, 2016 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riston/419668483d523ba68cf85787ebf52924 to your computer and use it in GitHub Desktop.
Save riston/419668483d523ba68cf85787ebf52924 to your computer and use it in GitHub Desktop.
Encrypt-decrypt AES256 Node.js with crypto module example
var crypto = require("crypto");
function encrypt(key, data) {
var ciph = crypto.createCipher("aes256", key);
ciph.end(data);
var encBuffer = ciph.read();
// Convert to base64
return encBuffer.toString("base64");
}
function decrypt(key, data) {
var ciph = crypto.createDecipher("aes256", key);
var encBuffer = new Buffer(data, "base64");
ciph.end(encBuffer);
return ciph.read().toString("utf8");
}
var enc = encrypt("kass", "hernesupp");
var dec = decrypt("kass", enc);
console.log(enc, dec);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment