Using the functions listed on this page: http://lollyrock.com/articles/nodejs-encryption/
I was able to build the following proof of concept for my own needs:
var crypto = require('crypto');
var algorithm = 'aes256';
var password = 'correcthorsestaplebattery';
var string = "Something I\'d like to encrypt, like maybe login credentials for a site I need to scrape.";
console.log('\n\nText: ' + string);
var encrypted = encrypt(new Buffer(string, "utf8"), algorithm, password);
console.log('\n\nEncrypted: ' + encrypted);
var decrypted = decrypt(encrypted, algorithm, password).toString('utf8');
console.log('\n\nDecrypted: ' + decrypted);
console.log('\n\nAre they the same before and after crypto? ');
console.log(decrypted == string);
function encrypt(buffer, algorithm, password){
var cipher = crypto.createCipher(algorithm,password)
var crypted = Buffer.concat([cipher.update(buffer),cipher.final()]);
return crypted;
}
function decrypt(buffer, algorithm, password){
var decipher = crypto.createDecipher(algorithm,password)
var dec = Buffer.concat([decipher.update(buffer) , decipher.final()]);
return dec;
}
This uses AES256, which should be as secure as two-way encryption, although I'm not advanced enough to comment on the implementation. This is better than plain text.
From this, you can easily write the output to a file instead of the console, as shown. While you are simply parsing a file containing JSON, you just need to add a decryption step before interpreting it.
Hope this helps.
source
share