Sample code for 30+ languages & platforms
VB.NET

Add a Certificate Identity to a PFX

See more PFX/P12 Examples

Demonstrates Pfx.AddCert, which adds a certificate as an identity in the PFX. The example loads the certificate from a .cer file and associates its private key from a PEM file (via Cert.SetPrivateKeyPem) before adding it.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. AddCert requires the certificate to have an accessible private key that Chilkat can obtain and copy. A certificate whose private key is non-exportable — for example a non-exportable key in a Windows certificate store or an Apple keychain, or a key on a smartcard/HSM — cannot be added to a PFX, because the private-key bytes are inaccessible. A certificate whose private key is exportable in these same locations, however, can be added to the PFX, because its private-key bytes can be obtained. Set the include-chain argument to true to also add the certificate's chain of authority.

Chilkat VB.NET Downloads

VB.NET
Dim success As Boolean = False

Dim pfx As New Chilkat.Pfx

'  The file paths are relative to the application's current working directory.  Absolute paths may
'  also be used.  Supply the paths appropriate to your own environment.

'  Load the certificate from a .cer file.
Dim cert As New Chilkat.Cert
success = cert.LoadFromFile("qa_data/certificate.cer")
If (success = False) Then
    Debug.WriteLine(cert.LastErrorText)
    Exit Sub
End If


'  Load the matching private key from a PEM file and associate it with the certificate.  AddCert
'  requires the certificate to have an accessible private key -- Chilkat must be able to obtain and
'  copy the actual private-key bytes.
Dim sbPem As New Chilkat.StringBuilder
success = sbPem.LoadFile("qa_data/private_key.pem","utf-8")
If (success = False) Then
    Debug.WriteLine("Failed to load the private key PEM.")
    Exit Sub
End If

success = cert.SetPrivateKeyPem(sbPem.GetAsString())
If (success = False) Then
    Debug.WriteLine(cert.LastErrorText)
    Exit Sub
End If


'  Add the certificate (and its chain) as an identity in the PFX.  Set the 2nd argument to True to
'  include the certificate's chain of authority.
Dim bIncludeChain As Boolean = True
success = pfx.AddCert(cert,bIncludeChain)
If (success = False) Then
    Debug.WriteLine(pfx.LastErrorText)
    Exit Sub
End If

Debug.WriteLine("Certificate added to the PFX.")

'  Note: the private-key material must be accessible to the application.  A certificate whose private
'  key is non-exportable -- for example a non-exportable key in a Windows certificate store or an
'  Apple keychain, or a key on a smartcard/HSM -- cannot be added to a PFX, because the private-key
'  bytes are inaccessible.  A certificate with an exportable key in a Windows certificate store or an
'  Apple keychain, however, can be added, because its private-key bytes can be obtained.