I have created a self-signed certificate with Java code and added into KeyStore. Now I want to export Private key and Certificate created, into a file in PEM format. Is it possible to achieve this without any third party library ? Below is the code I use for creating self-singed certificate.
public void createSelfSignedSSLCertificate() { try { final CertAndKeyGen keypair = new CertAndKeyGen("RSA", "SHA1WithRSA", null); final X500Name x500Name = new X500Name(commonName, organizationalUnit, organization, city, state, country); keypair.generate(keysize); final PrivateKey privKey = keypair.getPrivateKey(); final X509Certificate[] chain = new X509Certificate[1]; chain[0] = keypair.getSelfCertificate(x500Name, new Date(), validity * 24 * 60 * 60); final String alias = JettySSLConfiguration.SSL_CERTIFICATE_ALIAS; keyStore.setKeyEntry(alias, privKey, keyStorePassword.toCharArray(), chain); } catch (final Exception e) { // Handle Exception } } Any suggestion of how to export the key and certificate into file with PEM format will be really helpful.
-----BEGIN <xyz>and-----END <xyz>, are described as "PEM format". You'll need to be specific as to which one you want.sun.*packages, which are not part of the J2SE API, you should probably consider using a 3rd party library.