Sample code for 30+ languages & platforms
PowerBuilder

Convert a PFX to a Java KeyStore

See more PFX/P12 Examples

Demonstrates Pfx.ToJksObj, which populates a JavaKeyStore with a JKS representation of the PFX, then writes it to a file.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. One private-key entry is created for each private key, with certificate chains built from the PFX certificates. The password protects the generated Java KeyStore.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Pfx
string ls_Password
string ls_JksPassword
oleobject loo_Jks

li_Success = 0

loo_Pfx = create oleobject
li_rc = loo_Pfx.ConnectToNewObject("Chilkat.Pfx")
if li_rc < 0 then
    destroy loo_Pfx
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  The file path is relative to the application's current working directory.  An absolute path
//  may also be used.  Supply the path appropriate to your own environment.
//  The PFX password should come from a secure source rather than being hard-coded.
ls_Password = "myPfxPassword"
li_Success = loo_Pfx.LoadPfxFile("qa_data/identity.pfx",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Pfx.LastErrorText
    destroy loo_Pfx
    return
end if

//  Populate a Java KeyStore (JKS) representation of this PFX.  One private-key entry is created for
//  each private key, and the PFX certificates are used to build the certificate chains.  The 1st
//  argument is the alias for the first private-key entry, and the 2nd protects the generated JKS.
//  The JKS password should come from a secure source rather than being hard-coded.
ls_JksPassword = "myJksPassword"
loo_Jks = create oleobject
li_rc = loo_Jks.ConnectToNewObject("Chilkat.JavaKeyStore")

li_Success = loo_Pfx.ToJksObj("myalias",ls_JksPassword,loo_Jks)
if li_Success = 0 then
    Write-Debug loo_Pfx.LastErrorText
    destroy loo_Pfx
    destroy loo_Jks
    return
end if

//  The Java KeyStore object can then be written to a .jks file.
li_Success = loo_Jks.ToFile(ls_JksPassword,"qa_output/identity.jks")
if li_Success = 0 then
    Write-Debug loo_Jks.LastErrorText
    destroy loo_Pfx
    destroy loo_Jks
    return
end if

Write-Debug "Wrote a Java KeyStore with " + string(loo_Jks.NumPrivateKeys) + " private key(s)."


destroy loo_Pfx
destroy loo_Jks