Skip to content

Instantly share code, notes, and snippets.

@xk
Forked from virtuo/gist:757317
Created May 25, 2011 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xk/991013 to your computer and use it in GitHub Desktop.
Save xk/991013 to your computer and use it in GitHub Desktop.
Problem using crypto with base64 ouput format
var crypto = require('crypto');
var key = "some keys are better than others";
var msg = "Attack Troy from within a wooden horse!";
var CYPHER = "aes256";
var test = function(msg) {
var cypher = crypto.createCipher(CYPHER, key);
var enc_msg = cypher.update(msg, "utf8", 'binary');
enc_msg += cypher.final('binary');
enc_msg= new Buffer(enc_msg, 'binary').toString('base64');
var decypher = crypto.createDecipher(CYPHER, key);
var dec_msg = decypher.update(enc_msg, 'base64', "utf8");
dec_msg += decypher.final("utf8");
console.log('i: -> '+ msg, '\no: -> '+dec_msg, '\nc: -> "'+ enc_msg + '"\n');
};
var i= 0;
(function loop () {
var txt= msg.substr(0, i++);
test(txt, 'binary');
if (txt.length < msg.length) process.nextTick(loop);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment