javascript - Encrypt only json key value and get response of whole json object with keyvalue encrypted -
iam trying encrypt key value of json object using nodejs app.iam using crypto node module.i pass json object(it can basic or complexi.e.,inside value can again have key value pair) response should same json in same format give initially,but key value should enctyped. in code have encrypt function encrypt data.here should pass keyvalue function,which iam able , encrypted data.iam using each i.e for(var exkey in jsondata) , passing each key value function.and again framing in json format using 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 fine if using basic json.but if using complex(eg.,inside keyvalue other key value pair) not work since need see whether having such , iterate function.so made changes 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 function able iterate loop , encrypt keyvalue , iam consoling key , value adding ",:,{. example if passing json as:
{"brokerlimit":"50","tradertype": {"insurer":"john","cover":"basic" }, "issplitpayment":"yes"} on console i'll
"brokerlimit":"51de" "tradertype":{ "insurer":"0e81dc9e" "cover":"068fc79922" "issplitpayment":"1d8bc7"
so iam able go inside loop , encrypt key value.. thing want response in json format i.e
{"brokerlimit":"51de","tradertype": {"insurer":"0e81dc9e","cover":"068fc79922" }, "issplitpayment":"1d8bc7"}
so need store encrypted key value in variable ,also need add ",:,} make in json format.here iam facing issue.if key has property in json key value goes function.at time think need save in temporary variable , need add global variable , keep on adding global variable...but couldn't fix logic..can me in this..hope question clear.if not reply me.
the easiest way create new object encrypted data, , use json.stringify(obj)
generate json string.
Comments
Post a Comment