Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkJavaKeyStore.pb"
IncludeFile "CkPfx.pb"

Procedure ChilkatExample()

    success.i = 0

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

    pfx.i = CkPfx::ckCreate()
    If pfx.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Load the PKCS12 from a file
    success = CkPfx::ckLoadPfxFile(pfx,"/someDir/my.p12","myPfxPassword")
    If success = 0
        Debug CkPfx::ckLastErrorText(pfx)
        CkPfx::ckDispose(pfx)
        ProcedureReturn
    EndIf

    jksPassword.s = "myJksPassword"
    alias.s = "firstPrivateKeyAlias"

    jks.i = CkJavaKeyStore::ckCreate()
    If jks.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; 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.
    success = CkPfx::ckToJksObj(pfx,alias,jksPassword,jks)
    If success = 0
        Debug CkPfx::ckLastErrorText(pfx)
        CkPfx::ckDispose(pfx)
        CkJavaKeyStore::ckDispose(jks)
        ProcedureReturn
    EndIf

    ; Save the Java keystore to a file.
    success = CkJavaKeyStore::ckToFile(jks,jksPassword,"/myKeystores/my.jks")
    If success <> 1
        Debug CkJavaKeyStore::ckLastErrorText(jks)
        CkJavaKeyStore::ckDispose(jks)

        CkPfx::ckDispose(pfx)
        CkJavaKeyStore::ckDispose(jks)
        ProcedureReturn
    EndIf

    Debug "Successfully converted PFX to JKS."


    CkPfx::ckDispose(pfx)
    CkJavaKeyStore::ckDispose(jks)


    ProcedureReturn
EndProcedure