Sample code for 30+ languages & platforms
VB.NET

Load PKCS12 / PFX and Access Contents

See more PFX/P12 Examples

Loads a PKCS12 / PFX file and iterates over the contents which include private keys and certificates.

Chilkat VB.NET Downloads

VB.NET
Dim success As Boolean = False

Dim pfx As New Chilkat.Pfx

' Load the PKCS12 from a file
success = pfx.LoadPfxFile("/someDir/my.p12","pfxFilePassword")
If (success = False) Then
    Debug.WriteLine(pfx.LastErrorText)
    Exit Sub
End If


Dim numPrivateKeys As Integer = pfx.NumPrivateKeys

Dim privKey As New Chilkat.PrivateKey

Debug.WriteLine("Private Keys:")

Dim i As Integer = 0
While i < numPrivateKeys
    pfx.PrivateKeyAt(i,privKey)

    ' Do something with the private key ...

    i = i + 1
End While

Dim cert As New Chilkat.Cert

Dim numCerts As Integer = pfx.NumCerts

Debug.WriteLine("Certs:")
i = 0
While i < numCerts
    pfx.CertAt(i,cert)
    Debug.WriteLine(cert.SubjectDN)

    ' If the certificate has a private key (one of the private keys within the PFX)
    ' then it can also be obtained via the certificate object:
    If (cert.HasPrivateKey() = True) Then

        Debug.WriteLine("Has private key!")

        success = cert.GetPrivateKey(privKey)
        ' ...

    End If


    i = i + 1
End While