Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.BinData,
  Chilkat.StringBuilder,
  Chilkat.Zip,
  Chilkat.Cert,
  Chilkat.CertStore;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  certStore: TCertStore;
  readOnly: Boolean;
  pfxPassword: string;
  allSuccess: Boolean;
  numSuccess: Integer;
  zip: TZip;
  certData: TBinData;
  sbFilename: TStringBuilder;
  cert: TCert;
  numCerts: Integer;
  i: Integer;
  bHasPrivateKey: Boolean;

begin
  success := False;

  certStore := TCertStore.Create;

  readOnly := True;
  success := certStore.OpenCurrentUserStore(readOnly);
  if (not success) then
    begin
      WriteLn(certStore.LastErrorText);
      Exit;
    end;

  pfxPassword := 'secret';

  allSuccess := True;
  numSuccess := 0;

  zip := TZip.Create;
  zip.NewZip('qa_output/personalCerts.zip');

  certData := TBinData.Create;
  sbFilename := TStringBuilder.Create;

  //  Iterate over the certificates in the Current User store.
  cert := TCert.Create;
  numCerts := certStore.NumCertificates;
  i := 0;
  while i < numCerts do
    begin
      certStore.GetCert(i,cert);
      WriteLn('DN = ' + cert.SubjectDN);

      sbFilename.SetString('cert');
      sbFilename.AppendInt(i + 1);

      bHasPrivateKey := cert.HasPrivateKey();
      if ((bHasPrivateKey = True) and (cert.PrivateKeyExportable = True)) then
        begin
          //  Export to a .pfx
          success := cert.ExportToPfxBd(pfxPassword,True,certData);
          if (success = True) then
            begin
              sbFilename.Append('.pfx');
              zip.AddBd(sbFilename.GetAsString(),certData);
            end;
        end
      else
        begin
          //  Export to a .cer
          success := cert.ExportCertDerBd(certData);
          if (success = True) then
            begin
              sbFilename.Append('.cer');
              zip.AddBd(sbFilename.GetAsString(),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 := zip.WriteZipAndClose();
      if (success <> True) then
        begin
          WriteLn(zip.LastErrorText);
          allSuccess := False;
        end;
    end;

  WriteLn('All success = ' + allSuccess);


  certStore.Free;
  zip.Free;
  certData.Free;
  sbFilename.Free;
  cert.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.