2
votes

I was writing a java program that supposed to generate private rsa dsa ec key pairs and export them, also the program could import keys from pem files..
importing the keys works well since I've used bouncycastle PEMParser to read the object from the file which would be PEMEncryptedKeyPair and somehow using PEMDecryptorProvider.build() I could read the encrypted key..
The problem now is that I'm trying to write out the private key to a file and it should be encrypted, my question is :
is there a way of doing that using java built in classes or using bouncycastle or I have to encrypt the encoded private key and add the header myself? help is very appreciated ps: if there is a good place to start with bouncycastle APIs please tell me

1

1 Answers

3
votes

You can use the org.bouncycastle.openssl.PEMWriter class. The javadoc is missing but according to the source code the writeObject method accepts PrivateKey objects. If you want to protect (i.e. encrypt) the private key you need to use a PEMEncryptor instance which can be built from the org.bouncycastle.openssl.jcajce.JcePEMEncryptorBuilder class.