Skip to content

Instantly share code, notes, and snippets.

@bbengfort
Created June 18, 2013 19:37
Show Gist options
  • Save bbengfort/5808559 to your computer and use it in GitHub Desktop.
Save bbengfort/5808559 to your computer and use it in GitHub Desktop.
Current workings for the encode/decode for AES-256.
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