admin管理员组

文章数量:1389754

I was wondering if anyone knew of a quick library or some solution which basically does this.

code({ json: 1 }) -> codedsecret

decode(codedSecret) -> { json: 1 }

Basically, I'm looking for a way to translate JSON to an encoded string that most folks won't be able to understand for security reasons.

I was wondering if anyone knew of a quick library or some solution which basically does this.

code({ json: 1 }) -> codedsecret

decode(codedSecret) -> { json: 1 }

Basically, I'm looking for a way to translate JSON to an encoded string that most folks won't be able to understand for security reasons.

Share Improve this question asked Apr 3, 2013 at 23:26 adrianadrian 2,3864 gold badges33 silver badges49 bronze badges 3
  • 1 Perhaps Crypto JS? (with all the usual caveats about client-side security) – Tim M. Commented Apr 3, 2013 at 23:30
  • Yeah I just saw that, might work well thanks – adrian Commented Apr 3, 2013 at 23:30
  • You can reverse your string :) – Ivan Kuckir Commented Apr 3, 2013 at 23:41
Add a ment  | 

1 Answer 1

Reset to default 6

If by "most folks" you mean non-sneaky developers, then base64 is probably sufficient. Modern browsers should implement btoa and atob (both directions), but there are libraries out there in case you need more patibility.

var secret = btoa(JSON.stringify({json: 1}));
JSON.parse(atob(secret)); // {json: 1}

This offers no cryptographic security at all and is easily cracked by developers who can recognize and even decode base64 strings visually. If you need to cover those two circumstances, then you need to look into encrypting on the server side before municating with the client and vice versa. There are OpenSSL implementations for JavaScript, but I'm not sure how much I'd trust them.

本文标签: javascriptJSON to hashed valueStack Overflow