Sample code for 30+ languages & platforms
PowerBuilder

Load Identities from PEM Text

See more PFX/P12 Examples

Demonstrates Pfx.LoadPem, which loads one or more certificate/private-key identities from PEM-formatted text and builds the contents of the Pfx object.

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. The password decrypts any encrypted private keys in the PEM. This builds a PFX in memory from PEM material, which can then be serialized to PKCS#12.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Pfx
oleobject loo_SbPem
integer li_BLoaded
string ls_PemText
string ls_Password

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.
//  Load the PEM text (certificates and private keys) into a string.
loo_SbPem = create oleobject
li_rc = loo_SbPem.ConnectToNewObject("Chilkat.StringBuilder")

li_BLoaded = loo_SbPem.LoadFile("qa_data/identity.pem")
if li_BLoaded <> 1 then
    Write-Debug "Failed to load the PEM file."
    destroy loo_Pfx
    destroy loo_SbPem
    return
end if

ls_PemText = loo_SbPem.GetAsString()
if loo_SbPem.LastMethodSuccess = 0 then
    Write-Debug loo_SbPem.LastErrorText
    destroy loo_Pfx
    destroy loo_SbPem
    return
end if

//  The password decrypts any encrypted private keys in the PEM (use an empty string if none are
//  encrypted).  A password should come from a secure source rather than being hard-coded.
ls_Password = "myPemPassword"
li_Success = loo_Pfx.LoadPem(ls_PemText,ls_Password)
if li_Success = 0 then
    Write-Debug loo_Pfx.LastErrorText
    destroy loo_Pfx
    destroy loo_SbPem
    return
end if

Write-Debug "Loaded " + string(loo_Pfx.NumPrivateKeys) + " private key(s)."


destroy loo_Pfx
destroy loo_SbPem