Skip to content

Instantly share code, notes, and snippets.

@cdimascio
Forked from chris-rock/crypto-ctr.js
Last active March 2, 2018 23:26
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 cdimascio/62a5d0b6ddceb2df6af30fb4542fb060 to your computer and use it in GitHub Desktop.
Save cdimascio/62a5d0b6ddceb2df6af30fb4542fb060 to your computer and use it in GitHub Desktop.
Encrypt and decrypt text in nodejs
// Part of https://github.com/chris-rock/node-crypto-examples
// Nodejs encryption with CTR
var crypto = require('crypto'),
algorithm = 'aes-256-ctr',
password = 'd6F3Efeq',
key = Buffer.from('5ebe2294ecd0e0f08eab7690d2a6ee69', 'hex'),
iv = Buffer.from('26ae5cc854e36b6bdfca366848dea6bb', 'hex');
function encrypt(text){
const cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
// var cipher = crypto.createCipheriv(algorithm,password)
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text){
var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
// var decipher = crypto.createDecipher(algorithm,password)
var dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
var hw = encrypt("hello world")
// outputs hello world
console.log(decrypt(hw));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment