Skip to content

Instantly share code, notes, and snippets.

@KazChe
Created December 13, 2014 23:54
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 KazChe/a75a7d165649846fd50e to your computer and use it in GitHub Desktop.
Save KazChe/a75a7d165649846fd50e to your computer and use it in GitHub Desktop.
nodejs encrypt / decrypt
var fs = require('fs'),
crypto = require('crypto'),
algorithm = 'aes-256-ctr',
password = 'd6F3Efeq',
payload = {
"id": "12345",
"description": "description",
"location": "123 street",
"userIdentity": {
"phoneNumber": "5550000000",
"pin": "0000000"
},
"amount": {
"amount": ".001",
"currency": ""
}
},
clientkey = fs.readFileSync('./key.pem');
var text = crypto.createHash('sha1').update(payload + clientkey, 'utf8').digest('hex')
function encrypt(text){
var cipher = crypto.createCipher(algorithm,password)
var crypted = cipher.update(JSON.stringify(payload)+clientkey,'utf8','hex')
crypted += cipher.final('hex');
console.log('<encrypted to> \n\n', crypted +'\n\n');
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 encryptedValue = encrypt(text)
console.log('<decrypted to> \n\n',decrypt(encryptedValue));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment