Encrypt only json key value and get response of whole json object with keyvalue encrypted

Deadly 提交于 2019-12-23 06:23:16

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/39108697/encrypt-only-json-key-value-and-get-response-of-whole-json-object-with-keyvalue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!