Last active
December 26, 2015 12:49
-
-
Save sdeering/7153714 to your computer and use it in GitHub Desktop.
Basic Node.js Crypto Cipher Encryption Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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