Created
November 24, 2013 15:48
-
-
Save chris-rock/7628605 to your computer and use it in GitHub Desktop.
encrypt and decrypt data with nodejs
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
"use strict"; | |
var assert = require('assert'); | |
var crypto = require('crypto'); | |
var Buffer = require('buffer').Buffer; | |
var SECRET_KEY = "ChuckN0rrisL1kesPur3D3PapaSuperKey"; | |
var ENCODING = 'hex'; | |
//var ALGORITHM = "des-ede3-cbc"; | |
var ALGORITHM = "aes256"; | |
//1h * 60 min * 60s * 1000 ms | |
var valid = Math.round(new Date().getTime() + 1*60*60*1000); | |
var token = { | |
"id" : "123092742930840ß28423853409756430952", | |
"data" : "1238758023750" | |
}; | |
var text = JSON.stringify(token); | |
var cipher = crypto.createCipher(ALGORITHM, SECRET_KEY); | |
var cryptedPassword = cipher.update(text, 'utf8',ENCODING); | |
cryptedPassword+= cipher.final(ENCODING); | |
var decipher = crypto.createDecipher(ALGORITHM, SECRET_KEY); | |
var decryptedPassword = decipher.update(cryptedPassword, ENCODING,'utf8'); | |
decryptedPassword += decipher.final('utf8'); | |
assert.equal(text, decryptedPassword); | |
console.log("Original Text", JSON.stringify(token)); | |
console.log("Crypted Text:", cryptedPassword); | |
console.log("Decrypted Text:", decryptedPassword); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment