Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

Post encrypted data

node v14.17.6
version: 2.0.0
endpointsharetweet
Encrypt & POST flow: 1. Data in JSON format 2. Convert to buffer 3. Encryption with public key (pem) 4. Convert to base64 5. POST data to the server via application/json format (field key: encrypted) The detailed encryption process can refer to the code below
const crypto = require("crypto"); const axios = require('axios'); const publicKey = `-----BEGIN PUBLIC KEY----- MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgG7M+Xq8zOounLGpom4Gicq7TQkd KJcqB8gaauqDhS1PF7qrsRBMm1ubvLSrj7q8fdEkevPU+k8GJpS4UGewLuVRGdgz TbppLnhZip3YauwhurogJSVwTqHZSDYWtxYHLqjtWFJudnSwH9cClAx7CWB8zSAB 7mvOnM9GRnE8djv7AgMBAAE= -----END PUBLIC KEY-----`; // client side encrypt let plainBody = { "LineID": "U12345678901234567890123456789012", "SFDCID": "1234" };
let json = JSON.stringify(plainBody);
let buffer = new Buffer.from(json);
let encryptedBuffer = crypto.publicEncrypt(publicKey, buffer);
let encrypted = encryptedBuffer.toString("base64");
axios.post( "https://example.com", { encrypted: encrypted, }, { headers: { "content-type": "application/json", }, } );
Decrypt flow: 1. Convert data from base64 to buffer 2. Decrypt with private key 3. Convert buffer to string 4. Parse the string back to the data in JSON format
// service side decrypt const privateKey = `-----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgG7M+Xq8zOounLGpom4Gicq7TQkdKJcqB8gaauqDhS1PF7qrsRBM m1ubvLSrj7q8fdEkevPU+k8GJpS4UGewLuVRGdgzTbppLnhZip3YauwhurogJSVw TqHZSDYWtxYHLqjtWFJudnSwH9cClAx7CWB8zSAB7mvOnM9GRnE8djv7AgMBAAEC gYAtRAQwBm3Yxk4xrtGzDWzBrB9et9DbYMppD5HaWPzRc9yH4JinxgQ6OrevP9+q NFNaXAL3Fm1kV2jvnRUMJ0ggzJamfqEfohb2Ea23tg9e+mOYs3OMtNSGipIfCWu7 WEA//Gb9XztR4Pf6q8B4Asnf3XWSDcNA+b1l1wV8PdzoYQJBANxCPWVa3QvzoZr/ NP1L/t7rLLF6+yMmk/ScuKoM+Y1v9dYJDYDPwukLO6CqbHXFcx6DsXpcJirdFkSX rqus3KMCQQCAx73JkrsQouyJ1/IfiFx7mcShZU08TBtw5ImmAdI9eRboM0Z5pOys gDc39wNyw9DX1pVf9H/+XpAFm/z+HwDJAkEAqFc0J4f31A+j3xvYuxNfO68QnKE6 2lW81UMG3GlIPjJo+Oo4WM/xzz2XmCdI+AYuAw9yEOvm+/G3+DrZ9DoM8wJAZ7l6 ActvZHz1rCkE2aUJaHx3xc9EIFbkm09YFx/MaLsJWklCcGQIRQCYeTk5y8wwgEhT 1A+osAcWVY5IfZ4XEQJBAMRrhV5nKMHMDWWwxxl+qF5hI2pnVeRcl8zw776+p9FQ VOyLrWE1yv1tRH9z+i7y8++b8m4iUHJVW6/rNWI6uRc= -----END RSA PRIVATE KEY-----`; encryptedBuffer = new Buffer.from(encrypted, "base64");
buffer = crypto.privateDecrypt(privateKey, encryptedBuffer);
json = buffer.toString()
data = JSON.parse(json)
Loading…

no comments

    sign in to comment