Created
June 18, 2013 19:37
-
-
Save bbengfort/5808559 to your computer and use it in GitHub Desktop.
Current workings for the encode/decode for AES-256.
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 secret = crypto.randomBytes(24); | |
function encrypt(plaintext) { | |
var encoded = new Buffer(plaintext, 'utf8', 'hex'); | |
var cipher = crypto.createCipher('aes-256-cbc', secret); | |
// cipher.setAutoPadding(false); | |
var ciphertext = cipher.update(encoded, 'hex'); | |
return ciphertext.toString('base64'); | |
} | |
function decrypt(ciphertext) { | |
var encoded = new Buffer(ciphertext, 'base64', 'hex') | |
var decipher = crypto.createDecipher('aes-256-cbc', secret); | |
// decipher.setAutoPadding(false); | |
var plaintext = decipher.update(encoded, 'hex'); | |
return plaintext.toString('utf8'); | |
} | |
var ciphertext = encrypt(new Buffer("The secret crow ate the pie of the bear.").toString('utf8')); | |
console.log("Cipher text is: " + ciphertext); | |
console.log("Plain text is: " + decrypt(ciphertext)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment