Sample code for 30+ languages & platforms
B4X Requires Chilkat v11.0.0+

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 B4X Downloads

B4X
Dim success As Boolean = False

Dim pfx As ChilkatPfx
pfx.Initialize

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


Dim numPrivateKeys As Int = pfx.NumPrivateKeys

Dim privKey As ChilkatPrivateKey
privKey.Initialize("privKey")

Log("Private Keys:")

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

    '  Do something with the private key ...

    i = i + 1
Loop

Dim cert As ChilkatCert
cert.Initialize("cert")

Dim numCerts As Int = pfx.NumCerts

Log("Certs:")
i = 0
Do While i < numCerts
    pfx.CertAt(i, cert)
    Log(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

        Log("Has private key!")

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

    End If


    i = i + 1
Loop