仅加密json键值并获得加密的keyvalue的整个json对象的响应 [英] Encrypt only json key value and get response of whole json object with keyvalue encrypted

查看:169
本文介绍了仅加密json键值并获得加密的keyvalue的整个json对象的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用nodejs app.Iam来加密json对象的键值,使用crypto node module.I将传递json对象(可以是基本的或复杂的,在一个值内,我们可以再次具有键值对)然后作为响应,我们应该得到与我们最初给出的相同格式相同的json,但是键值应该被enctyped。
在我的代码中,我有一个加密函数,它将加密数据。我应该只传递键值到该函数,我可以做,并获取加密的数据。我使用每个ie for(var exKey in JsonData)并将每个键值传递给function.And再次使用此代码将其重新设置为json格式。

  var JsonData = JSON.parse(req.headers.jsondata); 
var enc = null;
for(var exKey in JsonData){
var encryptData = encrypt(JsonData [exKey]);
if(enc!= null)
enc = enc +,+''+ exKey +''+:+ encryptData;
else
enc ={+''+ exKey +''+:+ encryptData;
}
enc = enc +};

如果我们使用基本的json,这很好。但是如果我们使用复杂的(例如,里面keyvalue其他键值对)这不会工作,因为我们需要看看我们是否有这样的和迭代的功能。所以我修改了代码。

 code> function iterate(JsonData)
{
for(var exKey in JsonData){
if(JsonData.hasOwnProperty(exKey)){
if(typeof JsonData [ exKey] ==object){
console.log(''+ exKey +''+:+{);
iterate(JsonData [exKey]);
}
else
{
var encrypted = encrypt(JsonData [exKey]);
console.log('''+ exKey +'+:+''+ encrypted +'');
}

}
}
函数加密(文本){
var cipher = crypto.createCipher(algorithm,password);
var crypted = cipher.update(text,'utf8','hex');
crypted + = cipher.final('hex');
返回加密;
}

现在使用我可以迭代循环并加密键值的功能例如,如果我将json传递为:



{brokerLimit:50,那么我可以通过添加 traderType:
{insurer:john,cover:basic},isSplitPayment:yes}
然后在控制台上我会得到



brokerLimit:51de
traderType:{
insurer:0e81dc9e
cover:068fc79922
isSplitPayment:1d8bc7



所以我可以进入循环和加密的关键值..
但现在的事情是我想以json格式作为响应,即



{brokerLimit:51de,traderType:
{insurer:0e81dc9e,覆盖:068fc79922},isSplitPayment:1d8bc7}



所以我需要将加密的密钥值存储在变量中,我也需要添加,,,}使其成为json格式。Iam面临问题。如果键在json中具有属性,那么键值回到函数。那时候我想我们需要保存临时变量,然后我们需要添加到另一个全局变量,然后继续添加到全局变量...但我无法修复逻辑。可以有人帮我这个。希望我的问题很清楚,如果没有回复我。

解决方案

最简单的方法可能是使用加密数据创建一个新对象,然后使用 JSON.stringify(obj)来生成JSON字符串。


Iam trying to encrypt only the key value of json object using nodejs app.Iam using crypto node module.I will pass json object(it can be basic or complexi.e.,inside a value we can again have key value pair) then as response we should get the same json in same format which we give initially,but the key value should be enctyped. In my code I have an encrypt function which will encrypt the data.Here I should only pass the keyvalue to the function,which iam able to do and get back encrypted data.Iam using for each i.e for(var exKey in JsonData) and passing each key value to function.And again framing it back in to json format using this code.

var JsonData=JSON.parse(req.headers.jsondata);
var enc=null;  
for(var exKey in JsonData) {
var encryptData=encrypt(JsonData[exKey]);
if(enc!= null)
enc= enc+","+ '"'+ exKey+'"'+":"+encryptData;
else
enc="{"+'"'+exKey+'"' +":"+encryptData; 
}
enc=enc+"}";

This is fine if we are using basic json.But if we are using complex(eg.,inside keyvalue other key value pair) this will not work since we need to see whether we are having such and iterate function.So i made changes to code.

function iterate(JsonData)
{
 for(var exKey in JsonData) {
 if (JsonData.hasOwnProperty(exKey)) {
    if(typeof JsonData[exKey]=="object"){
     console.log('"'+exKey+'"'+":"+"{");
      iterate(JsonData[exKey]);
 }
  else
 {
  var encrypted=encrypt(JsonData[exKey]);
  console.log('"'+exKey+'"'+":"+ '"'+encrypted+'"');
 }

 }
 }
  function encrypt(text){
  var cipher = crypto.createCipher(algorithm,password);
  var crypted = cipher.update(text,'utf8','hex');
  crypted +=cipher.final('hex'); 
  return crypted;
  } 

Now using the function i able to iterate the loop and encrypt the keyvalue and then iam consoling the key and value by adding ",:,{. For example if am passing json as:

{"brokerLimit":"50","traderType": {"insurer":"john","cover":"basic" }, "isSplitPayment":"yes"} Then on console i'll get

"brokerLimit":"51de" "traderType":{ "insurer":"0e81dc9e" "cover":"068fc79922" "isSplitPayment":"1d8bc7"

So Iam able to go inside loop and encrypt the key value.. But now the thing is I want to get this as response in json format i.e

{"brokerLimit":"51de","traderType": {"insurer":"0e81dc9e","cover":"068fc79922" }, "isSplitPayment":"1d8bc7"}

So I need to store the encrypted key value in variable ,also to that i need to add ",:,} to make it in json format.Here Iam facing the issue.If the key has property in json then the key value goes back to function.At that time I think we need to save in temporary variable and then we need to add to another global variable and then keep on adding to global variable...But I couldn't fix the logic..Can someone help me in this..Hope my question is clear.If not reply me.

解决方案

The easiest way is probably to just create a new object with the encrypted data, and then use JSON.stringify(obj) to generate the JSON string.

这篇关于仅加密json键值并获得加密的keyvalue的整个json对象的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆