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
(Unicode C) 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.
#include <C_CkCertStoreW.h> #include <C_CkZipEntryW.h> #include <C_CkZipW.h> #include <C_CkBinDataW.h> #include <C_CkStringBuilderW.h> #include <C_CkCertW.h> void ChilkatSample(void) { HCkCertStoreW certStore; BOOL readOnly; BOOL success; const wchar_t *pfxPassword; BOOL allSuccess; int numSuccess; HCkZipEntryW entry; HCkZipW zip; HCkBinDataW certData; HCkStringBuilderW sbFilename; int numCerts; int i; HCkCertW cert; BOOL bHasPrivateKey; certStore = CkCertStoreW_Create(); readOnly = TRUE; success = CkCertStoreW_OpenCurrentUserStore(certStore,readOnly); if (!success) { wprintf(L"%s\n",CkCertStoreW_lastErrorText(certStore)); CkCertStoreW_Dispose(certStore); return; } pfxPassword = L"secret"; allSuccess = TRUE; numSuccess = 0; zip = CkZipW_Create(); CkZipW_NewZip(zip,L"qa_output/personalCerts.zip"); certData = CkBinDataW_Create(); sbFilename = CkStringBuilderW_Create(); // Iterate over the certificates in the Current User store. numCerts = CkCertStoreW_getNumCertificates(certStore); i = 0; while (i < numCerts) { cert = CkCertStoreW_GetCertificate(certStore,i); wprintf(L"DN = %s\n",CkCertW_subjectDN(cert)); CkStringBuilderW_SetString(sbFilename,L"cert"); CkStringBuilderW_AppendInt(sbFilename,i + 1); bHasPrivateKey = CkCertW_HasPrivateKey(cert); if ((bHasPrivateKey == TRUE) && (CkCertW_getPrivateKeyExportable(cert) == TRUE)) { // Export to a .pfx success = CkCertW_ExportToPfxBd(cert,pfxPassword,TRUE,certData); if (success == TRUE) { CkStringBuilderW_Append(sbFilename,L".pfx"); entry = CkZipW_AppendBd(zip,CkStringBuilderW_getAsString(sbFilename),certData); CkZipEntryW_Dispose(entry); } } else { // Export to a .cer success = CkCertW_ExportCertDerBd(cert,certData); if (success == TRUE) { CkStringBuilderW_Append(sbFilename,L".cer"); entry = CkZipW_AppendBd(zip,CkStringBuilderW_getAsString(sbFilename),certData); CkZipEntryW_Dispose(entry); } } if (success != TRUE) { allSuccess = FALSE; } else { numSuccess = numSuccess + 1; } i = i + 1; } if (numSuccess > 0) { success = CkZipW_WriteZipAndClose(zip); if (success != TRUE) { wprintf(L"%s\n",CkZipW_lastErrorText(zip)); allSuccess = FALSE; } } wprintf(L"All success = %d\n",allSuccess); CkCertStoreW_Dispose(certStore); CkZipW_Dispose(zip); CkBinDataW_Dispose(certData); CkStringBuilderW_Dispose(sbFilename); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.