Created
May 14, 2015 09:29
-
-
Save goodjob1114/f6c90f445f3ff0e2ebc7 to your computer and use it in GitHub Desktop.
encrypt data using crypto::aes-256-cbc on Node.js
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 crypto = require('crypto') | |
var cipher = function(key, plaintext){ | |
console.log('cipher::init') | |
var cipher = crypto.createCipher('aes-256-cbc', key) | |
var encriptedText = cipher.update(plaintext, 'utf8', 'hex') | |
encriptedText += cipher.final('hex') | |
console.log('cipher::key',key) | |
console.log('cipher::encriptedText',encriptedText) | |
return encriptedText | |
} | |
var decipher = function(key, encriptedText){ | |
console.log('decipher::init') | |
var decipher = crypto.createDecipher('aes-256-cbc', key) | |
console.log('encriptedText',encriptedText) | |
var plaintext = decipher.update(encriptedText, 'hex', 'utf8') | |
plaintext += decipher.final('utf8') | |
console.log('decipher::key',key) | |
console.log('decipher::plaintext',plaintext) | |
return plaintext | |
} | |
var key = 'helloNode' | |
var magic = cipher(key, 'Tim is a good guy') | |
console.log('magic',magic) | |
var demagic = decipher(key, magic) | |
console.log('demagic',demagic) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment