Java Requires Chilkat v11.0.0+
Chilkat2 Java
Convert PKCS12 / PFX to Java Keystore (JKS)
See more PFX/P12 Examples
Loads a PKCS12 / PFX file and saves it to a Java keystore (JKS) file.Chilkat Java Downloads
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 example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Pfx pfx = new Pfx();
// Load the PKCS12 from a file
try {
pfx.loadPfxFile("/someDir/my.p12", "myPfxPassword");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
String jksPassword = "myJksPassword";
String alias = "firstPrivateKeyAlias";
JavaKeyStore jks = new JavaKeyStore();
// Convert to a Java keystore object.
// The jksPassword is the password to be used for the JKS private key entries.
// It may be the same as the PFX password, but can also be different if desired.
try {
pfx.toJksObj(alias, jksPassword, jks);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Save the Java keystore to a file.
try {
jks.toFile(jksPassword, "/myKeystores/my.jks");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
jks.close();
return;
}
System.out.println("Successfully converted PFX to JKS.");
}
}