Search code examples
c#cryptographybouncycastleprivate-key

Encrypt and store a private+public key with Bouncy Castle


I created a AsymmetricCipherKeyPair with the ECKeyPairGenerator and want to store the private and public key.

I found the following classes and methods to archive this:

PrivateKeyFactory.EncryptKey(...)  
EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo(...)

What is the recommend API to use?

But what algorithm should be used? I tested "BCObjectIdentifiers.bc_pbe_sha1_pkcs12_aes128_cbc" and got an exception "attempt to use non-PBE algorithm with PBE EncryptedPrivateKeyInfo generation"

I also tested "PBEWITHSHAAND128BITAES-CBC-BC" and it took ages to complete.

So is there a recommend way to store (and read in later) a private and public key with Bouncy Castle C# ?


Solution

  • You were on the right track, those are the right classes. However BC had bugs in the bindings for the AES-based PBE encryption algorithms, which I've only just now fixed in CVS.