Generate Public Key From Pem File Java

Generate Public Key From Pem File Java

Requirement : Create JKS keystore and truststore out of certificate and private key files given in pem format.

Try to open the certificate and key files and it contains ASCII text that starts with —–BEGIN CERTIFICATE—–, then it is in PEM format.

Generate Pem File Openssl

Concatenate all.pem files into one pem file, like all.pem Then create keystore in p12 format with private key + all.pem. Openssl pkcs12 -export -inkey private.key -in all.pem -name test -out test.p12 Then export p12 into jks. Keytool -importkeystore -srckeystore test.p12 -srcstoretype pkcs12 -destkeystore test.jks. Public keys will be returned as well formed SubjectPublicKeyInfo objects, private keys will be returned as well formed PrivateKeyInfo objects. In the case of a private key a PEMKeyPair will normally be returned if the encoding contains both the private and public key definition. Aug 29, 2016  Part 4: Converting the key files for usage in Java (Public Key) Plain Java is able to understand the public key format. However, it can’t read the PEM file directly, but it can understand the DER encoding. The solution is, to decode the file first using Base64 and then let it parse by Java. Here’s a snippet that does this.

/key-generator-vmware-workstation-15.html. Using keytool in java, when a keystore is created it already has the private key in it. Keytool does not allow us to import a private key into a keystore. Thus we need to use OpenSSL for this but OpenSSL creates the keystore in pkcs12 format. So we use jetty to convert our pkcs12 into jks format.

OpenSSL for Windows is available from http://www.slproweb.com/products/Win32OpenSSL.html.

Keystore to be created : keystore.pkcs12, Certificate File : test.cert.pem, PrivateKey File : test.key.pem.
openssl pkcs12 -export -out keystore.pkcs12 -in test.cert.pem -inkey test.key.pem
Enter the appropriate password. Now using jetty we can convert the pkcs12 keystore into jks keystore (keystore.jks).
java -cp c:jettylibjetty-6.1.1.jar org.mortbay.jetty.security.PKCS12Import keystore.pkcs12 keystore.jks

Generate Public Key From Pem File Java Mac

Now to create truststore file.
keytool -import -alias test -file test.cert.pem -keystore truststore

Linux Generate Pem File

where truststore is the new TrustStore in jks format. You can import as many other certificates as you need to trust into the truststore. Give the password and type y when asked trust the certificate.