加密和解密node.js中的json文件
有人能说出如何在node.js中加密和解密jsonfile吗。我尝试了这个程序,它显示了一个错误,因为文件密码tty.setrawmode不是函数 纯文本文件:加密和解密node.js中的json文件,json,node.js,encryption,Json,Node.js,Encryption,有人能说出如何在node.js中加密和解密jsonfile吗。我尝试了这个程序,它显示了一个错误,因为文件密码tty.setrawmode不是函数 纯文本文件: { "production" : { "db" : { "database" : "mysql", "user" : "root", "password" : "bdwb ve13hb3" }, "app" : { "port" : 8
{
"production" : {
"db" : {
"database" : "mysql",
"user" : "root",
"password" : "bdwb ve13hb3"
},
"app" : {
"port" : 8086
}
}
}
加密:
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);
}
}
);
解密:
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);
}
});
如果使用三个参数调用
sconf.encryptFile
,它将从终端读取密码。如果您没有在能够接受node.js中用户字符的终端上运行它,则需要在脚本的第三个参数处添加密码:
sconf.encryptFile(
"./test.json",
"./test.json.enc",
"this is not such a good password",
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);
}
}
);
sconf.encryptFile(
“/test.json”,
“/test.json.enc”,
“这不是一个好密码”,
功能(err、f、ef、ec){
如果(错误){
consel.log(“加密%s失败,错误为%s”,f,err);
}否则{
console.log(“将%s加密到完成的%s。”,f,ef);
console.log(“加密内容为%s”,ec);
}
}
);代码>
解密时需要提供完全相同的密码
当然,将密码添加到代码中不是一个好主意,因为攻击者只需查看代码即可找到用于加密配置文件的密码。Bro,我无法获得输出。我按照您的指示操作。我添加了第三个参数作为文本。我以webstorm终端中的第一个参数运行加密程序。它显示相同的错误。您可以将加密和解密的确切代码发送给我吗。这是我的邮件id:vignesh。mack03@gmail.com.Thanks提前。我在等你的答复……很抱歉,但我不是你的兄弟。我已经浏览了sconf.encryptFile
的源代码,这就是应该发生的事情。我实际上还没有运行这个代码。