I am new to BouncyCastle. I have a private key generated using the below code.
final CertAndKeyGen keypair = new CertAndKeyGen("RSA", "SHA1WithRSA", null);
keypair.generate(1024);
final PrivateKey privKey = keypair.getPrivateKey();
I would to encrypt it with a password using AES or some openssl supported algorithm using BouncyCastle. Can some one help me out how to start, where I am not able to find any good tutorial on this. Please help me out. Thanks in advance.
If you'd prefer to protect your private keys with AES-256 instead of one of the old DES variants supported by PKCS8, this will work:
You can verify the output with openssl. In my case the key is EC so this command is used:
Adapt as required for RSA keys.
If you just want to output your private key to a passphrase "12345" protected PEM formatted and file "privatekey.pem" you can use this BC code:
then afterwards you can get at the private key with openssl with
The "standard" use of PEMWriter will not passphrase protect your private key:(