Visual Basic Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

VB Examples

Bounced Mail
Character Encoding
Digital Certificates
Digital Signatures
Email
FTP
HTML-to-XML
HTTP
IMAP
Encryption
MHT / HTML Email
RSA Encryption
S/MIME
Socket
Spider
String
Tar
Unicode
Upload
XML
XMP
Zip Compression

More Examples...
Email Object
POP3
SMTP
RSS
Atom
Self-Extractor


VB Strings
VB Byte Array

Unreleased...
Service
PPMD
Deflate
Bzip2
LZW
Bz2
DH Key Exchange
DSA
Icon

 

 

 

 

 

 

 

Export Certificate and Private Key to PFX

Demonstrates how to export a digital certificate, it's private key, and potentially all certificates in the chain of authentication to a PFX file.

Dim success As Long

'  This object is used to create a certificate store object.
Dim ccs As New ChilkatCreateCS

Dim certStore As ChilkatCertStore
'  Open the local machine certificate store read-only.
ccs.ReadOnly = 1
Set certStore = ccs.OpenLocalSystemStore()

'  Can we find a certificate by email address?
Dim cert As ChilkatCert
Set cert = certStore.FindCertBySubjectE("admin@chilkatsoft.com")
If (cert Is Nothing ) Then
    '  Open the current-user certificate store and check it instead.

    Text1.Text = Text1.Text & "Checking current-user certificate store..." & vbCrLf
    Text1.Refresh

    Set certStore = ccs.OpenCurrentUserStore()

    Set cert = certStore.FindCertBySubjectE("admin@chilkatsoft.com")
    If (cert Is Nothing ) Then
        Text1.Text = Text1.Text & "Failed to find certificate!" & vbCrLf
        Text1.Refresh
        Exit Sub
    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.
    Dim bIncludeChain As Long
    bIncludeChain = 1
    success = cert.ExportToPfxFile("myCert.pfx","myPassword",bIncludeChain)
    If (success <> 1) Then
        Text1.Text = Text1.Text & cert.LastErrorText & vbCrLf
        Text1.Refresh
    Else
        Text1.Text = Text1.Text & "Exported to PFX!" & vbCrLf
        Text1.Refresh
    End If

Else
    Text1.Text = Text1.Text & "Certificate does not have a private key available" & vbCrLf
    Text1.Refresh
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.

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2008 Chilkat Software, Inc. All Rights Reserved.