Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loCertStore
LOCAL lnReadOnly
LOCAL lcPfxPassword
LOCAL lnAllSuccess
LOCAL lnNumSuccess
LOCAL loZip
LOCAL loCertData
LOCAL loSbFilename
LOCAL loCert
LOCAL lnNumCerts
LOCAL i
LOCAL lnBHasPrivateKey

lnSuccess = 0

loCertStore = CreateObject('Chilkat.CertStore')

lnReadOnly = 1
lnSuccess = loCertStore.OpenCurrentUserStore(lnReadOnly)
IF (NOT lnSuccess) THEN
    ? loCertStore.LastErrorText
    RELEASE loCertStore
    CANCEL
ENDIF

lcPfxPassword = "secret"

lnAllSuccess = 1
lnNumSuccess = 0

loZip = CreateObject('Chilkat.Zip')
loZip.NewZip("qa_output/personalCerts.zip")

loCertData = CreateObject('Chilkat.BinData')
loSbFilename = CreateObject('Chilkat.StringBuilder')

* Iterate over the certificates in the Current User store.
loCert = CreateObject('Chilkat.Cert')
lnNumCerts = loCertStore.NumCertificates
i = 0
DO WHILE i < lnNumCerts
    loCertStore.GetCert(i,loCert)
    ? "DN = " + loCert.SubjectDN

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

    lnBHasPrivateKey = loCert.HasPrivateKey()
    IF ((lnBHasPrivateKey = 1) AND (loCert.PrivateKeyExportable = 1)) THEN
        * Export to a .pfx
        lnSuccess = loCert.ExportToPfxBd(lcPfxPassword,1,loCertData)
        IF (lnSuccess = 1) THEN
            loSbFilename.Append(".pfx")
            loZip.AddBd(loSbFilename.GetAsString(),loCertData)
        ENDIF

    ELSE
        * Export to a .cer
        lnSuccess = loCert.ExportCertDerBd(loCertData)
        IF (lnSuccess = 1) THEN
            loSbFilename.Append(".cer")
            loZip.AddBd(loSbFilename.GetAsString(),loCertData)
        ENDIF

    ENDIF

    IF (lnSuccess <> 1) THEN
        lnAllSuccess = 0
    ELSE
        lnNumSuccess = lnNumSuccess + 1
    ENDIF

    i = i + 1
ENDDO

IF (lnNumSuccess > 0) THEN
    lnSuccess = loZip.WriteZipAndClose()
    IF (lnSuccess <> 1) THEN
        ? loZip.LastErrorText
        lnAllSuccess = 0
    ENDIF

ENDIF

? "All success = " + STR(lnAllSuccess)

RELEASE loCertStore
RELEASE loZip
RELEASE loCertData
RELEASE loSbFilename
RELEASE loCert