Skip to content

Instantly share code, notes, and snippets.

@renatoargh
Created February 21, 2013 17:39
Show Gist options
  • Select an option

  • Save renatoargh/5006587 to your computer and use it in GitHub Desktop.

Select an option

Save renatoargh/5006587 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 = 'this is a secret';
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment