1

I have a server with endpoint that sends data which gets received by a different server, both running NodeJS - and I wanted it so that the JSON data which gets sent is encrypted, so I looked into node-rsa, but as I am new to Node in general, it felt a little cryptic for me.

I tried the initial functionality of encrypting the data being sent, but after it reaches my second server, I would like it to only be decrypted there so it can be parsed properly, but I don't quite get how to decrypt it.

The code for my encryption is supposedly like this:

const encrypted = key.encrypt(data, 'base64');
res.json({
            status: 200,
            message: "Done",
            data: encrypted;
        });

How do I exactly go on making a key-pair using node-rsa to decrypt the output of the data sent? And I suppose the best practice would be to make the actual key in a .env file instead hard-coding it into the code?

1
  • what do you want to achieve? I guess you don't want to send data in clear text through the network. The better approach is to use ssl (https). Handshake, Certificate Verification, Encryption and Decryption is then handled properly. Commented Jul 18, 2019 at 13:29

1 Answer 1

7

Like I mentioned in the comment: Question is what you want to achieve? If you don't want to send data in clear text through the network then using ssl (https) is the better option.

If you need to encrypt it manually then have a look at the following code snippets which I copied together from the node-rsa documentation:

First of all you should generate a key pair upfront:

const NodeRSA = require('node-rsa');

const key = new NodeRSA({b: 512});

console.log('\nPUBLIC:');
console.log(key.exportKey('pkcs8-public-pem'));
console.log('\nPRIVATE:');
console.log(key.exportKey('pkcs1-pem'));

// ----------- OUTPUT -----------
PUBLIC:
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKAHpm1YbYK1KrD409LUWulw1tdxKbGm
oyJH5pcLDrHuZXLnPPjCGSTbjgzCZKo9urkuxPISRZCGJ82lASZ8cI8CAwEAAQ==
-----END PUBLIC KEY-----

PRIVATE:
-----BEGIN RSA PRIVATE KEY-----
MIIBOQIBAAJBAKAHpm1YbYK1KrD409LUWulw1tdxKbGmoyJH5pcLDrHuZXLnPPjC
GSTbjgzCZKo9urkuxPISRZCGJ82lASZ8cI8CAwEAAQJAfqhPYq/gTNlSpqrqDC2i
nqx+fhnEpCR9bT53FQjdYplEXgbeyfkdWkdvJLWnv3lhgWM0tNJRCc/5oxy0Jybu
MQIhAPTYy3hN533kztAaupesT51NOIiMekNJ5czyD/3koNkTAiEAp1HJ5UG4Xiwn
+nuG9uNVIcflMEP1dOzJdbN3IKIxFhUCICKKFqHg83/58AbBToMo5o49H3V7w5+M
ZCN2HjVltB8LAiApblMz9YYVq5FqsNPGppwZ9UmHHgzVmxx3jKze48qL0QIgV0I+
Hk6ZCCdobuGBzxERj1vPfCUvaH124yYKNwaUryE=
-----END RSA PRIVATE KEY-----

For encryption you need the public key:

const key = new NodeRSA();
key.importKey('-----BEGIN PUBLIC KEY-----MFww...wEAAQ==-----END PUBLIC KEY-----', 'pkcs8-public-pem');

const data = { hello: 'world' };
const encrypted = key.encrypt(JSON.stringify(data), 'base64');
console.log('ENCRYPTED:');
console.log(encrypted);
res.json({
  status: 'OK',
  data: encrypted
});

// -------- OUTPUT ------------
ENCRYPTED:
NBsQMQycoww3wLATGMSkNk57zQVtWw5Dz0/uP+1rlzqKH1pXG6DDXsE9sf5k7TH0T09V9MNNtImCfoEgVDetWg==

For decryption you need the private key

const key = new NodeRSA();

// TODO: read private key from file and keep it secret and secure. Do not put this private key into code!
const privatePem = '-----BEGIN RSA PRIVATE KEY-----MII...UryE=-----END RSA PRIVATE KEY-----';
key.importKey(privatePem, 'pkcs1-pem');

// read the encrypted data from service call
const encrypted = 'NBsQMQycoww3wLATGMSkNk57zQVtWw5Dz0/uP+1rlzqKH1pXG6DDXsE9sf5k7TH0T09V9MNNtImCfoEgVDetWg==';
const decryptedString = key.decrypt(encrypted, 'utf8');

console.log('\nDECRYPTED string: ');
console.log(decryptedString);
const decrypedObject = JSON.parse(decryptedString);

// --------- OUTPUT --------
DECRYPTED: 
{"hello":"world"}

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that's exactly what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.