Skip to content

Instantly share code, notes, and snippets.

@davidgaya
Last active April 13, 2016 15:38
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 davidgaya/0158808b0c3711b0bdd80193e95d0a44 to your computer and use it in GitHub Desktop.
Save davidgaya/0158808b0c3711b0bdd80193e95d0a44 to your computer and use it in GitHub Desktop.

Object encryptor decryptor using aes192. It converts any object to a base64 encrypted string, and the oposite from an encrypted base64 string to an object. Note that objects are first converted to JSON, don't expect functions to be converted.

Example:

c=require('object_cryptor');
c1=c(''abcdefghijklmnopqrstuvxyz');
var obj = {
  a: 'a',
  'one': 1,
  'sub': {
    'two': 2,
    'three': 'three'
  }
}
c1.encrypt(obj);
// 'VZE/AoEbgIzaOAKeBTp5523SfXKEGchfw1dLHTzs2dJRz9u4DxfICGfpxqaRwgc/WsBZj4igPHdxF1BTlsS+Vg=='

c1.decrypt('VZE/AoEbgIzaOAKeBTp5523SfXKEGchfw1dLHTzs2dJRz9u4DxfICGfpxqaRwgc/WsBZj4igPHdxF1BTlsS+Vg==')
// { a: 'a', one: 1, sub: { two: 2, three: 'three' } }
var crypto = require('crypto');
module.exports = function (secret) {
secret = secret || crypto.pseudoRandomBytes(128).toString('hex');
return {
encrypt: function(obj) {
var str = JSON.stringify(obj) || '';
var buf = Buffer(str, 'utf8');
var c = crypto.createCipher('aes192', secret);
var b1 = c.update(buf);
var b2 = c.final();
return Buffer.concat([ b1, b2 ]).toString('base64');
},
decrypt: function(encoded_str) {
var buf = Buffer(encoded_str, 'base64');
var c = crypto.createDecipher('aes192', secret);
var b1 = c.update(buf);
var b2 = c.final();
var str = Buffer.concat([ b1, b2 ]).toString('utf8');
try {
return JSON.parse(str);
}
catch(e) {
return str;
}
}
};
};
{
"name": "object_cryptor",
"version": "0.0.2",
"description": "Object encryptor decryptor using aes192. It converts any object to a base64 encrypted string, and the oposite. Objects are first converted to JSON, don't expect functions to be converted.",
"main": "index.js",
"scripts": {
"test": "node test.js"
},
"repository": {
"type": "git",
"url": "git+https://gist.github.com/0158808b0c3711b0bdd80193e95d0a44.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://gist.github.com/0158808b0c3711b0bdd80193e95d0a44"
},
"homepage": "https://gist.github.com/0158808b0c3711b0bdd80193e95d0a44"
}
var assert = require('assert');
var c=require('./index');
var c1, c2;
c1=c();
// Supports i18n
var str='Amb accents: á, é , í';
assert(str === c1.decrypt(c1.encrypt(str)));
// works with long strings (longer than aes block)
str='hola, com va totes les coses que passen per on estem la majoria de vegades ?, això no hi ha manera de que creixi, sempre té la mateixa longitud, carai ja no sé que més ficar per a que el xifrat es fagi molt més llarg del que ha sigut fins ara. Em tornaré voig.';
assert(str === c1.decrypt(c1.encrypt(str)));
// two crypts using same password give same results
c1 = c('abcdefghijklmnopqrstuvxyz');
c2 = c('abcdefghijklmnopqrstuvxyz');
assert(c1 !== c2);
assert(str === c1.decrypt(c2.encrypt(str)));
assert(str === c2.decrypt(c1.encrypt(str)));
// objects
var obj = {
a: 'a',
'one': 1,
'sub': {
'two': 2,
'three': 'three'
}
}
var decoded_obj = c1.decrypt(c1.encrypt(obj));
assert(obj !== decoded_obj);
assert(obj.a === decoded_obj.a);
assert(obj.one === decoded_obj.one);
assert(obj.sub.two === decoded_obj.sub.two);
assert(obj.sub.three === decoded_obj.sub.three);
// arrays
obj = ['a', 'b'];
decoded_obj = c1.decrypt(c1.encrypt(obj));
assert(obj !== decoded_obj);
assert(obj[0] === decoded_obj[0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment