For any security protocol it's always best to stick to standards. Never try to invent your own protocol.. You may end up doing more harm than good. If this data is over HTTP, then feel free to use SSL (HTTPS) to guard against MiTM attacks, e.g. eavesdropping and tampering. However, I think you are looking for something different.
While SSL should ensure that the data is not tampered with on travel, you may be performing this operation yourself, e.g., someone hands you some data and you are unsure that it has not been tampered with. To do this operation free to leverage a digital signature of that data, where the private key of the digital signature is held by you.
Something like this:
- Generate key pair (private, public).
- Sign the json with the private key
Sign(json, private) -> signedDocument
.
- Now, later when you want to verify that no one has tampered with it:
Verify(public, signedDocument, json) = True
if the document has not been tampered with (with extremely high probability) and False
if the document has been tampered with (again with probability being extremely high).
Here is great first read on digital signatures for the reasonably technically inclined: https://en.wikipedia.org/wiki/Digital_signature
I believe this might be a good place to get started for JS: http://www-cs-students.stanford.edu/~tjw/jsbn/ (I expect Stanford to do a decent job at implementing RSA, but feel free to look around for something more standard.. if this was Java, then it might be easier... Maybe write a CryptoWebServer to support this operation?)
For further reference, I believe this library is now maintained here: https://github.com/kjur/jsrsasign/wiki/Tutorial-for-Signature-class
I've taken an example of signing+verifying directly from the Library's tutorial. Please note this only solves the problem of signing and verifying, you still need to generate public/private key pairs.
Signing message 'aaa' (you would use a .json payload here):
// initialize
var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"});
// initialize for signature generation
sig.init(rsaPrivateKey); // rsaPrivateKey of RSAKey object
// update data
sig.updateString('aaa')
// calculate signature
var sigValueHex = sig.sign()
Here is a example for signature validation:
// initialize
var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"});
// initialize for signature validation
sig.init("-----BEGIN CERTIFICATE-----(snip)"); // signer's certificate
// update data
sig.updateString('aaa')
// verify signature
var isValid = sig.verify(sigValueHex)