Sample code for 30+ languages & platforms
Java

Convert PKCS12 / PFX to Java KeyStore

See more Java KeyStore (JKS) Examples

Converts a PKCS12 / PFX file to a Java keystore (JKS) file.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JavaKeyStore;
import com.chilkatsoft.chilkat2.Pfx;

public class ChilkatExample {

    public static void main(String[] args) {
        // This requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        JavaKeyStore jks = new JavaKeyStore();

        Pfx pfx = new Pfx();

        String pfxPassword = "secret";

        // Load a PKCS12 from a file.
        try {
            pfx.loadPfxFile("/someDir/my.p12", pfxPassword);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        String alias = "someAlias";
        String jksPassword = "jksSecret";

        // Add the PKCS12 to the empty Java keystore object:
        try {
            jks.addPfx(pfx, alias, jksPassword);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Write the Java keystore to a file:
        try {
            jks.toFile(jksPassword, "/jksFiles/my.jks");
            System.out.println("Successfully converted PKCS12 to JKS");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
        }
    }
}