![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi DLL) Backup Windows Current User / Personal Certificates to a .zipDemonstrates 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. Note: This example requires Chilkat v10.1.2 or greater.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Zip, CertStore, BinData, StringBuilder, ZipEntry, Cert; ... procedure TForm1.Button1Click(Sender: TObject); var certStore: HCkCertStore; readOnly: Boolean; success: Boolean; pfxPassword: PWideChar; allSuccess: Boolean; numSuccess: Integer; entry: HCkZipEntry; zip: HCkZip; certData: HCkBinData; sbFilename: HCkStringBuilder; cert: HCkCert; numCerts: Integer; i: Integer; bHasPrivateKey: Boolean; begin 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'); entry := CkZip_AppendBd(zip,CkStringBuilder__getAsString(sbFilename),certData); CkZipEntry_Dispose(entry); end; end else begin // Export to a .cer success := CkCert_ExportCertDerBd(cert,certData); if (success = True) then begin CkStringBuilder_Append(sbFilename,'.cer'); entry := CkZip_AppendBd(zip,CkStringBuilder__getAsString(sbFilename),certData); CkZipEntry_Dispose(entry); 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; |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.