Sample code for 30+ languages & platforms
AutoIt

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

AutoIt
Local $bSuccess = False

$oCertStore = ObjCreate("Chilkat.CertStore")

Local $bReadOnly = True
$bSuccess = $oCertStore.OpenCurrentUserStore($bReadOnly)
If (Not $bSuccess) Then
    ConsoleWrite($oCertStore.LastErrorText & @CRLF)
    Exit
EndIf

Local $sPfxPassword = "secret"

Local $bAllSuccess = True
Local $iNumSuccess = 0

$oZip = ObjCreate("Chilkat.Zip")
$oZip.NewZip("qa_output/personalCerts.zip")

$oCertData = ObjCreate("Chilkat.BinData")
$oSbFilename = ObjCreate("Chilkat.StringBuilder")

; Iterate over the certificates in the Current User store.
$oCert = ObjCreate("Chilkat.Cert")
Local $iNumCerts = $oCertStore.NumCertificates
Local $i = 0
While $i < $iNumCerts
    $oCertStore.GetCert($i,$oCert)
    ConsoleWrite("DN = " & $oCert.SubjectDN & @CRLF)

    $oSbFilename.SetString("cert")
    $oSbFilename.AppendInt($i + 1)

Local $bHasPrivateKey = $oCert.HasPrivateKey()
    If (($bHasPrivateKey = True) And ($oCert.PrivateKeyExportable = True)) Then
        ; Export to a .pfx
        $bSuccess = $oCert.ExportToPfxBd($sPfxPassword,True,$oCertData)
        If ($bSuccess = True) Then
            $oSbFilename.Append(".pfx")
            $oZip.AddBd($oSbFilename.GetAsString(),$oCertData)
        EndIf

    Else
        ; Export to a .cer
        $bSuccess = $oCert.ExportCertDerBd($oCertData)
        If ($bSuccess = True) Then
            $oSbFilename.Append(".cer")
            $oZip.AddBd($oSbFilename.GetAsString(),$oCertData)
        EndIf

    EndIf

    If ($bSuccess <> True) Then
        $bAllSuccess = False
    Else
        $iNumSuccess = $iNumSuccess + 1
    EndIf

    $i = $i + 1
Wend

If ($iNumSuccess > 0) Then
    $bSuccess = $oZip.WriteZipAndClose()
    If ($bSuccess <> True) Then
        ConsoleWrite($oZip.LastErrorText & @CRLF)
        $bAllSuccess = False
    EndIf

EndIf

ConsoleWrite("All success = " & $bAllSuccess & @CRLF)