Sample code for 30+ languages & platforms
PowerBuilder

Backup Windows Current User / Personal Certificates to a .zip

See more Certificates Examples

Demonstrates how to backup the certificates in the Windows registry-based Current User certificate store (in the "Personal" Logical Store as seen in certmgr.msc), to a zip archive. Certificates having an exportable private key are exported to .pfx files. Certificates with no private key, or with a non-exportable private key, are exported to .cer files.

Obviously, this example only runs on Windows computers.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_CertStore
integer li_ReadOnly
string ls_PfxPassword
integer li_AllSuccess
integer li_NumSuccess
oleobject loo_Zip
oleobject loo_CertData
oleobject loo_SbFilename
oleobject loo_Cert
integer li_NumCerts
integer i
integer li_BHasPrivateKey

li_Success = 0

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

li_ReadOnly = 1
li_Success = loo_CertStore.OpenCurrentUserStore(li_ReadOnly)
if not li_Success then
    Write-Debug loo_CertStore.LastErrorText
    destroy loo_CertStore
    return
end if

ls_PfxPassword = "secret"

li_AllSuccess = 1
li_NumSuccess = 0

loo_Zip = create oleobject
li_rc = loo_Zip.ConnectToNewObject("Chilkat.Zip")

loo_Zip.NewZip("qa_output/personalCerts.zip")

loo_CertData = create oleobject
li_rc = loo_CertData.ConnectToNewObject("Chilkat.BinData")

loo_SbFilename = create oleobject
li_rc = loo_SbFilename.ConnectToNewObject("Chilkat.StringBuilder")

// Iterate over the certificates in the Current User store.
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_NumCerts = loo_CertStore.NumCertificates
i = 0
do while i < li_NumCerts
    loo_CertStore.GetCert(i,loo_Cert)
    Write-Debug "DN = " + loo_Cert.SubjectDN

    loo_SbFilename.SetString("cert")
    loo_SbFilename.AppendInt(i + 1)

    li_BHasPrivateKey = loo_Cert.HasPrivateKey()
    if (li_BHasPrivateKey = 1) AND (loo_Cert.PrivateKeyExportable = 1) then
        // Export to a .pfx
        li_Success = loo_Cert.ExportToPfxBd(ls_PfxPassword,1,loo_CertData)
        if li_Success = 1 then
            loo_SbFilename.Append(".pfx")
            loo_Zip.AddBd(loo_SbFilename.GetAsString(),loo_CertData)
        end if

    else
        // Export to a .cer
        li_Success = loo_Cert.ExportCertDerBd(loo_CertData)
        if li_Success = 1 then
            loo_SbFilename.Append(".cer")
            loo_Zip.AddBd(loo_SbFilename.GetAsString(),loo_CertData)
        end if

    end if

    if li_Success <> 1 then
        li_AllSuccess = 0
    else
        li_NumSuccess = li_NumSuccess + 1
    end if

    i = i + 1
loop

if li_NumSuccess > 0 then
    li_Success = loo_Zip.WriteZipAndClose()
    if li_Success <> 1 then
        Write-Debug loo_Zip.LastErrorText
        li_AllSuccess = 0
    end if

end if

Write-Debug "All success = " + string(li_AllSuccess)


destroy loo_CertStore
destroy loo_Zip
destroy loo_CertData
destroy loo_SbFilename
destroy loo_Cert