Storing a node-rsa encryption/decryption key on a Heroku app

Viewed 108

I have a Postgres database attached to my Heroku application, which is a Node.js file. In the database, I need to encrypt phone numbers in the Node.js API and then put the encrypted numbers into the database. The phone numbers need to be able to be decrypted and sent back to the various clients. I thought node-rsa (https://www.npmjs.com/package/node-rsa) was a good module to use. The only problem is that when you create a key using:

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

That key will be regenerated at every server restart/deployment. If I use this key to encrypt phone numbers using:

const encrypted = key.encrypt('phoneNumberHere', 'base64');

and then post the encrypted number to the database, when I try to decrypt it, it might not work (because I may be using a new key). I was referred to Keybase.io because that would store backups of my keys in case the server fails or the key is removed somehow, but I'm not sure if that's the way to go. I was thinking if I can somehow store a node-rsa key inside of a Heroku config var it should be secure but I'm 1) not quite sure how to do that 2) not sure if that's a secure/smart way of key storage.

Any insight would be much appreciated, thank you!

0 Answers
Related