Lianja
Lianja
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 Lianja Downloads
llSuccess = .F.
loCertStore = createobject("CkCertStore")
llReadOnly = .T.
llSuccess = loCertStore.OpenCurrentUserStore(llReadOnly)
if (not llSuccess) then
? loCertStore.LastErrorText
release loCertStore
return
endif
lcPfxPassword = "secret"
llAllSuccess = .T.
lnNumSuccess = 0
loZip = createobject("CkZip")
loZip.NewZip("qa_output/personalCerts.zip")
loCertData = createobject("CkBinData")
loSbFilename = createobject("CkStringBuilder")
// Iterate over the certificates in the Current User store.
loCert = createobject("CkCert")
lnNumCerts = loCertStore.NumCertificates
i = 0
do while i < lnNumCerts
loCertStore.GetCert(i,loCert)
? "DN = " + loCert.SubjectDN
loSbFilename.SetString("cert")
loSbFilename.AppendInt(i + 1)
llBHasPrivateKey = loCert.HasPrivateKey()
if ((llBHasPrivateKey = .T.) and (loCert.PrivateKeyExportable = .T.)) then
// Export to a .pfx
llSuccess = loCert.ExportToPfxBd(lcPfxPassword,.T.,loCertData)
if (llSuccess = .T.) then
loSbFilename.Append(".pfx")
loZip.AddBd(loSbFilename.GetAsString(),loCertData)
endif
else
// Export to a .cer
llSuccess = loCert.ExportCertDerBd(loCertData)
if (llSuccess = .T.) then
loSbFilename.Append(".cer")
loZip.AddBd(loSbFilename.GetAsString(),loCertData)
endif
endif
if (llSuccess <> .T.) then
llAllSuccess = .F.
else
lnNumSuccess = lnNumSuccess + 1
endif
i = i + 1
enddo
if (lnNumSuccess > 0) then
llSuccess = loZip.WriteZipAndClose()
if (llSuccess <> .T.) then
? loZip.LastErrorText
llAllSuccess = .F.
endif
endif
? "All success = " + str(llAllSuccess)
release loCertStore
release loZip
release loCertData
release loSbFilename
release loCert