What are the general ideas for storing keys in order to make sure no one else has access to it?

It's not a good approach to hash the keys, since you are going to need them later.

  • $\begingroup$ The best idea is to store the keys on a smart card $\endgroup$
    – user27950
    Oct 11, 2016 at 12:16

2 Answers 2


The requirements for storing public keys and private keys are quite different. Public keys need to be in a trust store in order to be able to trust them. Care should be taken that an attacker cannot replace them or trick the system into using a different trust store. Public keys - as the name implies - do not have to be confidential.

The requirements for private keys obviously do require confidentiality. Furthermore, the access to use the private keys should be restricted. Note that it is possible to open up access to use the private key while keeping them confidential; an example of this is storage of the private key in a smart card, which then offers a signing / decryption service.

The handling of public keys / certificates and private keys is known as key management. Entire books have been written about that subject so we won't be able to discuss all of them.

For storage in software a common method is to store the private keys in key stores. Examples of key stores are PKCS#12 key stores and PGP key containers. These containers are files for which the contents of the entire store and / or the key entries themselves are in turn protected by a passphrase.

For generic solutions I'd take a good look at PKCS#12 which is for instance used by OpenSSL. Mind that the default password handling may still require work (e.g. increasing the work factor for the decryption key that unlocks the PKCS#12 contents). Just choosing PKCS#12 is not key management on its own.

Symmetric (data or session) keys are often derived - using a Key Derivation Function or Key Agreement protocol - rather than stored.

  • 1
    $\begingroup$ To expand a little: OpenSSL library fully supports PKCS12, and also PKCS8 (privatekey only not cert(s), optional PBE) and 'legacy' per-algorithm formats (optional bad PBE); commandline supports it for import and export but not other operations. Java fully (since 6 IIRC, partially before that) supports PKCS12; NSS and Windows (CNG) support it for import and export but have their own stores for most operations. $\endgroup$ Oct 12, 2016 at 1:50
  • $\begingroup$ @dave_thompson_085 In Java 9 PKCS#12 will be the default key store format and it will support secret keys (before that the key stores were very much PKIX minded, requiring certificate chains for private keys etc.) $\endgroup$
    – Maarten Bodewes
    Oct 12, 2016 at 21:03

Regarding Public keys , there is no need to store them secretly as they are meant for public. As far as private keys are concerned there could be following ways to store them

1) Store them in a location inaccessible for others (not in a purview of OS so that no one can see other than specific privileged user) 2) No Direct access method , meaning keys are only accessible by program. 3) You can use HSM (Hardware Security modules) that are designed to store keys safely.

Of course hashing is one of the good methods as you suggested to store keys.

  • $\begingroup$ I conjecture that your last sentence above is not exactly what you wanted to express (or else the meaning is not very clear). $\endgroup$ Oct 15, 2016 at 9:07
  • $\begingroup$ perhaps it should be that hashing is also a good method for storing keys Scretly. $\endgroup$
    – Anurag
    Oct 18, 2016 at 17:46
  • $\begingroup$ Im confused. But how do you recover the keys from their hash values that you have stored? $\endgroup$ Oct 19, 2016 at 14:03

Your Answer

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

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