Export Private Key From a Java Keytool Keystore
|Since Java verion 6, you can import/export private keys into PKCS#12 (.p12) files using keytool, using -importkeystore (not available in previous versions).
1 | keytool -importkeystore -srckeystore existing-store.jks -destkeystore new-store.p12 -deststoretype PKCS12 |
The PKCS12 keystore type is also supported as a standard keystore type in the default Oracle/Sun security provider.
Once you have the p12 file, you can export the keys using openssl.
Export certificate using openssl:
1 | openssl pkcs12 -in keystore.p12 -nokeys -out cert.pem |
Export unencrypted private key:
1 | openssl pkcs12 -in keystore.p12 -nodes -nocerts -out key.pem |