Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPfx
LOCAL cJksPassword
LOCAL cAlias
LOCAL oJks

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oPfx := CreateObject("Chilkat.Pfx")

//  Load the PKCS12 from a file
nSuccess := oPfx:LoadPfxFile("/someDir/my.p12", "myPfxPassword")
IF (nSuccess == 0)
    ? oPfx:LastErrorText
    oPfx:destroy()
    RETURN
ENDIF

cJksPassword := "myJksPassword"
cAlias := "firstPrivateKeyAlias"

oJks := CreateObject("Chilkat.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.
nSuccess := oPfx:ToJksObj(cAlias, cJksPassword, oJks)
IF (nSuccess == 0)
    ? oPfx:LastErrorText
    oPfx:destroy()
    oJks:destroy()
    RETURN
ENDIF

//  Save the Java keystore to a file.
nSuccess := oJks:ToFile(cJksPassword, "/myKeystores/my.jks")
IF (nSuccess != 1)
    ? oJks:LastErrorText
    oJks:destroy()
    oPfx:destroy()
    oJks:destroy()
    RETURN
ENDIF

? "Successfully converted PFX to JKS."

oPfx:destroy()
oJks:destroy()