Skip to content

Instantly share code, notes, and snippets.

@prahaladbelavadi
Forked from chris-rock/crypto-buffer.js
Created July 15, 2018 13:33
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 prahaladbelavadi/a9f27764ca1caf5e4d8b098be3f73cab to your computer and use it in GitHub Desktop.
Save prahaladbelavadi/a9f27764ca1caf5e4d8b098be3f73cab to your computer and use it in GitHub Desktop.
Encrypt and decrypt buffers in nodejs
// Part of https://github.com/chris-rock/node-crypto-examples
var crypto = require('crypto'),
algorithm = 'aes-256-ctr',
password = 'd6F3Efeq';
function encrypt(buffer){
var cipher = crypto.createCipher(algorithm,password)
var crypted = Buffer.concat([cipher.update(buffer),cipher.final()]);
return crypted;
}
function decrypt(buffer){
var decipher = crypto.createDecipher(algorithm,password)
var dec = Buffer.concat([decipher.update(buffer) , decipher.final()]);
return dec;
}
var hw = encrypt(new Buffer("hello world", "utf8"))
// outputs hello world
console.log(decrypt(hw).toString('utf8'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment