title: Encrypt only json key value and get response of whole json object with keyvalue encrypted
body: <p>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.</p>
  
  <pre><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+"}";
  </code></pre>
  
  <p>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.</p>
  
  <pre><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;
    } 
  </code></pre>
  
  <p>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:</p>
  
  <p>{"brokerLimit":"50","traderType": 
  {"insurer":"john","cover":"basic" }, "isSplitPayment":"yes"}
  Then on console i'll get </p>
  
  <p>"brokerLimit":"51de"
  "traderType":{
  "insurer":"0e81dc9e"
  "cover":"068fc79922"
  "isSplitPayment":"1d8bc7"</p>
  
  <p>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 </p>
  
  <p>{"brokerLimit":"51de","traderType": 
  {"insurer":"0e81dc9e","cover":"068fc79922" }, "isSplitPayment":"1d8bc7"}</p>
  
  <p>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.     </p>