Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, BinData, StringBuilder, Zip, Cert, CertStore;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
certStore: HCkCertStore;
readOnly: Boolean;
pfxPassword: PWideChar;
allSuccess: Boolean;
numSuccess: Integer;
zip: HCkZip;
certData: HCkBinData;
sbFilename: HCkStringBuilder;
cert: HCkCert;
numCerts: Integer;
i: Integer;
bHasPrivateKey: Boolean;

begin
success := False;

certStore := CkCertStore_Create();

readOnly := True;
success := CkCertStore_OpenCurrentUserStore(certStore,readOnly);
if (not success) then
  begin
    Memo1.Lines.Add(CkCertStore__lastErrorText(certStore));
    Exit;
  end;

pfxPassword := 'secret';

allSuccess := True;
numSuccess := 0;

zip := CkZip_Create();
CkZip_NewZip(zip,'qa_output/personalCerts.zip');

certData := CkBinData_Create();
sbFilename := CkStringBuilder_Create();

// Iterate over the certificates in the Current User store.
cert := CkCert_Create();
numCerts := CkCertStore_getNumCertificates(certStore);
i := 0;
while i < numCerts do
  begin
    CkCertStore_GetCert(certStore,i,cert);
    Memo1.Lines.Add('DN = ' + CkCert__subjectDN(cert));

    CkStringBuilder_SetString(sbFilename,'cert');
    CkStringBuilder_AppendInt(sbFilename,i + 1);

    bHasPrivateKey := CkCert_HasPrivateKey(cert);
    if ((bHasPrivateKey = True) and (CkCert_getPrivateKeyExportable(cert) = True)) then
      begin
        // Export to a .pfx
        success := CkCert_ExportToPfxBd(cert,pfxPassword,True,certData);
        if (success = True) then
          begin
            CkStringBuilder_Append(sbFilename,'.pfx');
            CkZip_AddBd(zip,CkStringBuilder__getAsString(sbFilename),certData);
          end;
      end
    else
      begin
        // Export to a .cer
        success := CkCert_ExportCertDerBd(cert,certData);
        if (success = True) then
          begin
            CkStringBuilder_Append(sbFilename,'.cer');
            CkZip_AddBd(zip,CkStringBuilder__getAsString(sbFilename),certData);
          end;
      end;
    if (success <> True) then
      begin
        allSuccess := False;
      end
    else
      begin
        numSuccess := numSuccess + 1;
      end;
    i := i + 1;
  end;

if (numSuccess > 0) then
  begin
    success := CkZip_WriteZipAndClose(zip);
    if (success <> True) then
      begin
        Memo1.Lines.Add(CkZip__lastErrorText(zip));
        allSuccess := False;
      end;
  end;

Memo1.Lines.Add('All success = ' + IntToStr(Ord(allSuccess)));

CkCertStore_Dispose(certStore);
CkZip_Dispose(zip);
CkBinData_Dispose(certData);
CkStringBuilder_Dispose(sbFilename);
CkCert_Dispose(cert);

end;