Created
December 13, 2014 23:54
-
-
Save KazChe/a75a7d165649846fd50e to your computer and use it in GitHub Desktop.
nodejs encrypt / decrypt
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 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