What is a reasonably safe way to store credentials at rest for use in a screen cleaning application?

By virtue of PhantomJS, CasperJS allows you to specify a JSON file to load when the application starts. I have my credentials stored in this file, which is slightly better than having hard code in the source file:

var json = require('testfile.json');

var username = json['username'];
var mykey = json['mykey'];

I still have my credentials stored in clear text on the server that I would like to get away from. This process will be automated, so I can’t pass the credentials using the command line arguments every time I start it, and I don’t want to store the arguments in the Windows Task Scheduler. What is a safe way to keep this information alone?

+4
source share
1 answer

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);

// check to prove 2-way encryption works
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.

+1
source

Source: https://habr.com/ru/post/1526060/


All Articles