Delphi ActiveX
Delphi ActiveX
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 ActiveX Downloads
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
success: Integer;
certStore: TChilkatCertStore;
readOnly: Integer;
pfxPassword: WideString;
allSuccess: Integer;
numSuccess: Integer;
zip: TChilkatZip;
certData: TChilkatBinData;
sbFilename: TChilkatStringBuilder;
cert: TChilkatCert;
numCerts: Integer;
i: Integer;
bHasPrivateKey: Integer;
begin
success := 0;
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.
cert := TChilkatCert.Create(Self);
numCerts := certStore.NumCertificates;
i := 0;
while i < numCerts do
begin
certStore.GetCert(i,cert.ControlInterface);
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');
zip.AddBd(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');
zip.AddBd(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;