Search code examples
.netcryptographyencryption-asymmetric

Fileformat for saving encryption keys


Is there a easy to use (from .net) and commonly accepted file format for saving encryption keys (mainly for asymmetric keys, public and public/private). Does exists an API to create such files in .net (any version)?

I have looked at certificates, but this seems to be a overkill for only saving keys and as far as I have seen, not very easy to create.


Solution

  • One of the most common formats used to store the combination of a chain of certificate (or just one certificate) and its associated private key is PKCS#12: it tends to use the extension .p12 or .pfx. Most browsers use this.

    Alternatively, some tools use the PKCS#1 format (openssl rsa does, for example), if you just want the private key. (OpenSSL also supports PKCS#8.)

    Both can be encrypted and protected by a password.

    You should be able to use BouncyCastle to export them. (You might be interested in this question.)

    EDIT : To be more specific, you could use Org.BouncyCastle.OpenSsl.PemWriter (WriteObject with a password can be used to protect the private key). It will also let you export a plain public key (without having to rely on certificates).