Skip to content

Instantly share code, notes, and snippets.

@arizawan
Forked from chris-rock/crypto-ctr.js
Created June 29, 2016 15:18
Show Gist options
  • Save arizawan/cd0279ba0f1379fd675cf43c4e652f80 to your computer and use it in GitHub Desktop.
Save arizawan/cd0279ba0f1379fd675cf43c4e652f80 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';
function encrypt(text){
var cipher = crypto.createCipher(algorithm,password)
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text){
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