Skip to content

Instantly share code, notes, and snippets.

@jasonsilberman
Created April 18, 2014 23:46
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 jasonsilberman/11068778 to your computer and use it in GitHub Desktop.
Save jasonsilberman/11068778 to your computer and use it in GitHub Desktop.
var crypto = require('crypto');
var assert = require('assert');
var algorithm = 'aes256'; // or any other algorithm supported by OpenSSL
var key = 'password';
var text = 'I love kittens';
var cipher = crypto.createCipher(algorithm, key);
var encrypted = cipher.update(text, 'utf8', 'hex') + cipher.final('hex');
var decipher = crypto.createDecipher(algorithm, key);
var decrypted = decipher.update(encrypted, 'hex', 'utf8') + decipher.final('utf8');
assert.equal(decrypted, text);
// credit: http://stackoverflow.com/a/6953606/1740273
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment