Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi ActiveX) 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.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB; ... procedure TForm1.Button1Click(Sender: TObject); var certStore: TChilkatCertStore; readOnly: Integer; success: Integer; pfxPassword: WideString; allSuccess: Integer; numSuccess: Integer; entry: IChilkatZipEntry; zip: TChilkatZip; certData: TChilkatBinData; sbFilename: TChilkatStringBuilder; numCerts: Integer; i: Integer; cert: IChilkatCert; bHasPrivateKey: Integer; begin certStore := TChilkatCertStore.Create(Self); readOnly := 1; success := certStore.OpenCurrentUserStore(readOnly); if (not success) then begin Memo1.Lines.Add(certStore.LastErrorText); Exit; end; pfxPassword := 'secret'; allSuccess := 1; numSuccess := 0; zip := TChilkatZip.Create(Self); zip.NewZip('qa_output/personalCerts.zip'); certData := TChilkatBinData.Create(Self); sbFilename := TChilkatStringBuilder.Create(Self); // Iterate over the certificates in the Current User store. numCerts := certStore.NumCertificates; i := 0; while i < numCerts do begin cert := certStore.GetCertificate(i); Memo1.Lines.Add('DN = ' + cert.SubjectDN); sbFilename.SetString('cert'); sbFilename.AppendInt(i + 1); bHasPrivateKey := cert.HasPrivateKey(); if ((bHasPrivateKey = 1) and (cert.PrivateKeyExportable = 1)) then begin // Export to a .pfx success := cert.ExportToPfxBd(pfxPassword,1,certData.ControlInterface); if (success = 1) then begin sbFilename.Append('.pfx'); entry := zip.AppendBd(sbFilename.GetAsString(),certData.ControlInterface); end; end else begin // Export to a .cer success := cert.ExportCertDerBd(certData.ControlInterface); if (success = 1) then begin sbFilename.Append('.cer'); entry := zip.AppendBd(sbFilename.GetAsString(),certData.ControlInterface); end; end; if (success <> 1) then begin allSuccess := 0; end else begin numSuccess := numSuccess + 1; end; i := i + 1; end; if (numSuccess > 0) then begin success := zip.WriteZipAndClose(); if (success <> 1) then begin Memo1.Lines.Add(zip.LastErrorText); allSuccess := 0; end; end; Memo1.Lines.Add('All success = ' + IntToStr(Ord(allSuccess))); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.