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

Add a PEM Source of Certificates and Private Keys for S/MIME

See more MIME Examples

Loads a CMS/PKCS #7 encrypted S/MIME message and decrypts it back to the original MIME content.

AddPemSource adds the certificates and private keys of a Pem object to the sources that are searched for certificates and private keys by S/MIME operations such as decryption.

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. A Pem object is a convenient container for one or more certificates and private keys. AddPemSource can be called multiple times. Each call adds more certificates to the ones already added. AddPemSource was added in Chilkat v11.8.0 and replaces UseCertVault and the AddPfxSource methods.

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

Dim mime As ChilkatMime
mime.Initialize
success = mime.LoadMimeFile("qa_data/encrypted.eml")
If success = False Then
    Log(mime.LastErrorText)
    Return
End If


'  Use a Pem object as a source of certificates and private keys for S/MIME operations.
'  Here the Pem object is loaded from a PEM file.  The 2nd argument of LoadPemFile is the
'  password of the private key.
Dim pem As ChilkatPem
pem.Initialize
Dim pemPassword As String = "myPemPassword"
success = pem.LoadPemFile("qa_data/recipient.pem", pemPassword)
If success = False Then
    Log(pem.LastErrorText)
    Return
End If


'  The only argument is the Pem object.  AddPemSource can be called multiple times.  Each call
'  adds more certificates to the ones already added.
success = mime.AddPemSource(pem)
If success = False Then
    Log(mime.LastErrorText)
    Return
End If


'  Decryption now draws credentials from the added certificates and private keys.
success = mime.Decrypt
If success = False Then
    Log(mime.LastErrorText)
    Return
End If

Log("Decrypted using the certificate and private key of the PEM source.")