![]() |
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
Export Certificate and Private Key to PFXDemonstrates how to export a digital certificate, it's private key, and potentially all certificate's in the chain of authentication to a PFX file. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <% ' This object is used to create a certificate store object. set ccs = Server.CreateObject("Chilkat_9_5_0.CreateCS") ' Open the local machine certificate store read-only. ccs.ReadOnly = 1 ' certStore is a Chilkat_9_5_0.CertStore Set certStore = ccs.OpenLocalSystemStore() ' Can we find a certificate by email address? ' cert is a Chilkat_9_5_0.Cert Set cert = certStore.FindCertBySubjectE("admin@chilkatsoft.com") If (cert Is Nothing ) Then ' Open the current-user certificate store and check it instead. Response.Write "<pre>" & Server.HTMLEncode( "Checking current-user certificate store...") & "</pre>" ' certStore is a Chilkat_9_5_0.CertStore Set certStore = ccs.OpenCurrentUserStore() ' cert is a Chilkat_9_5_0.Cert Set cert = certStore.FindCertBySubjectE("admin@chilkatsoft.com") If (cert Is Nothing ) Then Response.Write "<pre>" & Server.HTMLEncode( "Failed to find certificate!") & "</pre>" End If End If ' Does this certificate have a private key accessible ' to the calling process? Private keys are *not* stored ' within the certificate store. Private keys are stored ' in a key container in a Windows protected store. It ' can be one of two protected stores: the protected store for ' the current logged-in user account, or the "machine-key" ' protected store. The private key must both exist in a ' protected store, and the process must have permission to ' access it... ' You can only export to a PFX if the private key exists ' and is accessible. If (cert.HasPrivateKey() = 1) Then ' Export to a PFX. ' Provide a password that will be required whenever the PFX is opened. ' Also, include all certs in the chain of authentication. bIncludeChain = 1 success = cert.ExportToPfxFile("myCert.pfx","myPassword",bIncludeChain) If (success <> 1) Then Response.Write "<pre>" & Server.HTMLEncode( cert.LastErrorText) & "</pre>" Else Response.Write "<pre>" & Server.HTMLEncode( "Exported to PFX!") & "</pre>" End If Else Response.Write "<pre>" & Server.HTMLEncode( "Certificate does not have a private key available") & "</pre>" End If ' The Chilkat Certificate, Certificate Store, Private Key, ' Public Key, and Key Container classes / objects are freeware. ' They are used by and included with the Chilkat Email, ' Crypt, S/MIME, and other commercial Chilkat components. %> </body> </html> |
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.