Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@virtuo
Created December 28, 2010 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save virtuo/757317 to your computer and use it in GitHub Desktop.
Save virtuo/757317 to your computer and use it in GitHub Desktop.
Problem using crypto with base64 ouput format
var crypto = require('crypto');
var key = "some key";
var msg = "Attack Troy from within a wooden horse!";
var CYPHER = "aes256";
var test = function(output_encoding) {
var cypher = crypto.createCipher(CYPHER, key);
var enc_msg = cypher.update(msg, "utf8", output_encoding);
enc_msg += cypher.final(output_encoding);
console.log('Crypted message: ' + enc_msg);
var decypher = crypto.createDecipher(CYPHER, key);
var dec_msg = decypher.update(enc_msg, output_encoding, "utf8");
dec_msg += decypher.final("utf8");
console.log('Decrypted message: ' + dec_msg);
};
console.log('hex')
test('hex');
console.log('\nbase64');
test('base64');
/* Result :
node test_crypto.js
hex
Crypted message: 39b6b2dfdf9e0f74ecea8e3284689b5d283d63f1ef2212a4fbc85cecc4cd745b77ec21ebb40e864f63ce50f805e1c131
Decrypted message: Attack Troy from within a wooden horse!
base64
Crypted message: Obay39+eD3Ts6o4yhGibXSg9Y/HvIhKk+8hc7MTNd+wh67QOhk9jzlD4BeHBMQ==
Decrypted message: Attack Troy from�S����'#&���J
*/
@virtuo
Copy link
Author

virtuo commented Dec 28, 2010

when using "hex" as output_encoding I can crypt-decrypt and then I have the original string, but when using "base64" as output_encoding, the original string is altered (but have some similarities).
Related doc : http://nodejs.org/api.html#crypto-createcipher-258

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment