Skip to content

Instantly share code, notes, and snippets.

@sdeering
Last active December 26, 2015 12:49
Show Gist options
  • Save sdeering/7153714 to your computer and use it in GitHub Desktop.
Save sdeering/7153714 to your computer and use it in GitHub Desktop.
Basic Node.js Crypto Cipher Encryption Example
var cryptojs = require('crypto')
, salt = "<client app salt token>"
, keyIterations = 1000 //number of interations to generate your key
, keyLength
, key
, password
, cipher
, msg = "<what you want to encrypt>"
, encryptedMsg;
//generate random password
try {
password = this.password = cryptojs.randomBytes(256);
} catch (ex) {
password = Math.Random();
}
//generate secure salted key
key = cryptojs.pbkdf2Sync(password, salt, keyIterations, keyLength);
//create cypher
cipher = cryptojs.createCipher('aes128', key) //AES The Advanced Encryption Standard (AES) is a U.S. Federal Information Processing Standard (FIPS).
cipher.update(msg, 'utf8', 'base64');
encryptedMsg = cipher.final('base64');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment