1

I'm trying to write a simple Encryption-Decryption program using Openssl-RSA. When a data is encrypted, a keypair is generated. If I want to decrypt the data, I need the exact keypair that was generated while encrypting.

So where and how do I store this keypair so that I can decrypt the encrypted content whenever possible?

I'm using public key for encryption and private key for decryption.

1

You can technically store the keypair wherever you want, though there are some common ways that it is done.

If the data is particularly sensitive and is not going to be needed regularly/constantly, the most secure method of storing your private key is on a flash drive or printed on a piece of paper in a safe or a bank vault. (Note that you can store the public key anywhere you want - it is, after all, a "public" piece of information.

If you need the data to be readily accessible, it is not usually practical to store the private key offline in a safe or vault due to the hassle of retrieving it every time. A common location that is often used for key storage is a folder named .ssh inside your home directory. (This is the default in OpenSSL on Linux, and is often used on other systems too.) The common practice is to store the key as two files with names that describe them in the following way: purpose_type for the private key and purpose_type.pub for the public key. For example, if you use the OpenSSL command line to create and RSA key that identifies you, it will be stored by default in the files id_rsa and id_rsa.pub in the .ssh directory. In your case, you could name the key files something like mydatastorage_rsa and mydatastorage_rsa.pub.

IMPORTANT: If you store a private key in this way, it is highly recommended that you secure it with a passphrase. This prevents anyone who gets access to your files from being able to use your private key. You may have been prompted to provide a key passphrase when you originally created the keypair; if not, you can add a passphrase to the private key using the OpenSSL command line tools.

If you will be doing a lot of sensitive encryption work, you can buy a hardware key-store, called a "Hardware Security Module", abbreviated "HSM", which is designed for secure storage of private keys. Unless you are actually working in the security/encryption industry, this is almost definitely overkill. There are also other hardware solutions, including "Smart Cards" and "USB Crypto Tokens", which are cheaper than a full HSM, but still require some investment in hardware that is likely more than you need.


Despite the recommendation to secure a private key with a password, you may run into issues if you need to do this decryption inside a service, like a webserver. In that case, you have three options:

  1. Provide the passphrase every time the server application starts up.
  2. Provide the passphrase in a configuration file. (Only supported by some servers)
  3. Remove the passphrase (or don't set one in the first place).

While the first one is most secure, it can only be used if you start the service manually every time. If you need to do either of the other options, make sure that you properly secure the config file (option 2) or the private key (option 3) to prevent access to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.