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

 

 

 

 

 

 

 

Extract Public/Private Keys and Certs from PFX into String Variables

Demonstrates how to export certificates and public/private keys from a PFX file into in-memory strings.

Dim success As Long
Dim certStore As New ChilkatCertStore

'  Load the PFX file into a certificate store object
Dim password As String
password = "*myPassword2*"
success = certStore.LoadPfxFile("chilkat.pfx",password)
If (success <> 1) Then
    MsgBox certStore.LastErrorText
    Exit Sub
End If

Dim i As Long
Dim numCerts As Long
numCerts = certStore.NumCertificates

'  Loop over each certificate in the PFX.
Dim cert As ChilkatCert
Dim fname As String
For i = 0 To numCerts - 1

    Set cert = certStore.GetCertificate(i)

    Text1.Text = Text1.Text & cert.SubjectDN & vbCrLf
    Text1.Text = Text1.Text & "---" & vbCrLf

    Dim encodedCert As String
    encodedCert = cert.GetEncoded()

    '  This string may now be stored in a relational database string field.
    '  To re-create the cert, do this:
    Dim cert2 As New ChilkatCert
    cert2.SetFromEncoded encodedCert

    '  Does this cert have a private key?
    If (cert.HasPrivateKey() = 1) Then

        '  Get the private key.
        Dim pvkey As PrivateKey
        Set pvkey = cert.ExportPrivateKey()

        '  The private key can be exported into
        '  a string in PKCS8, RSA PEM, or XML format:
        Dim pemPvKey As String
        Dim pkcs8PvKey As String
        Dim xmlPvKey As String

        pemPvKey = pvkey.GetRsaPem()
        pkcs8PvKey = pvkey.GetPkcs8Pem()
        xmlPvKey = pvkey.GetXml()

        Text1.Text = Text1.Text & pemPvKey & vbCrLf
        Text1.Text = Text1.Text & pkcs8PvKey & vbCrLf
        Text1.Text = Text1.Text & xmlPvKey & vbCrLf

        '  Any of these formatted strings may
        '  be stored in a relational database field.
        '  to restore, call LoadPem or LoadXml
        '  LoadPem accepts either RSA PEM or
        '  PKCS8 PEM:
        Dim pvKey2 As New PrivateKey

        pvKey2.LoadPem pemPvKey
        pvKey2.LoadPem pkcs8PvKey
        pvKey2.LoadXml xmlPvKey

    End If

    '  Now for the public key:
    Dim pubkey As PublicKey
    Set pubkey = cert.ExportPublicKey()

    '  It can be exported to a string as OpenSSL PEM
    '  or XML:
    Dim pubKeyPem As String
    Dim pubKeyXml As String

    pubKeyPem = pubkey.GetOpenSslPem()
    pubKeyXml = pubkey.GetXml()

    Text1.Text = Text1.Text & pubKeyPem & vbCrLf
    Text1.Text = Text1.Text & pubKeyXml & vbCrLf

    '  To re-load a PublicKey object, call LoadXml
    '  or LoadOpenSslPem:
    Dim pubKey2 As New PublicKey

    pubKey2.LoadOpenSslPem pubKeyPem
    pubKey2.LoadXml pubKeyXml
    fname = "pubkey" & CStr(i) & "_openSsl.der"
    pubkey.SaveOpenSslDerFile fname

Next

'  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.