Tuesday, 14 March 2017

encrypt and decrypt the json file in nodejs

can anyone say how to encrypt and decrypt the jsonfile in node.js please upload compiled program. I tried This program .It shows an error as File password tty.setrawmode is not a function.can anyone give the solution.thanks in advance....

JSON FILE:

{
  "production" : {
    "db"    : {
      "database" : "mysql",
      "user"     : "root",
      "password" : "bdwb ve13hb3"
    },
    "app"   : {
      "port"     : 8086
    }
  }
}

ENCRYPTION FILE:

var SecureConf = require('secure-conf');
var sconf      = new SecureConf();
sconf.encryptFile(
    "./test.json",
    "./test.json.enc",
    function(err, f, ef, ec) {
        if (err) {
            consoel.log("failed to encrypt %s, error is %s", f, err);
        } else {
            console.log("encrypt %s to %s complete.", f, ef);
            console.log("encrypted contents are %s", ec);
        }
    }
);

DECRYPTION:


var SecureConf = require('secure-conf');
var sconf      = new SecureConf();
var ef         = "./test.json.enc";
var express    = require('express');
var app        = express();

sconf.decryptFile(ef, function(err, file, content) {
    if (err) {
        console.log('Unable to retrieve the configuration contents.');
    } else {
        var config = JSON.parse(content);
        app.listen(config.production.app.port);
    }
});


via vigneshRavi

No comments:

Post a Comment