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

Add a PEM File as a Decryption Source

See more MIME Examples

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

AddPemSource registers the certificates and private keys of a Pem object, here loaded from a PEM file, as a source of certificates and private keys used by subsequent decryption operations.

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. Multiple PEM sources can be added so that a single Mime object is able to decrypt messages addressed to any of several identities. Chilkat chooses the matching key at decryption time. 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


'  Load a PEM file that has the certificate and its private key.  The 1st argument of LoadPemFile is
'  the path and the 2nd is the password of the private key (from a secure source).
Dim pemPassword As String = "myPemPassword"
Dim pem As ChilkatPem
pem.Initialize
success = pem.LoadPemFile("qa_data/recipient.pem", pemPassword)
If success = False Then
    Log(pem.LastErrorText)
    Return
End If


'  Add the certificates and private keys of the Pem object as a source for later decryption.
'  Call AddPemSource once per source.  Each call adds more certificates to the ones already added.
success = mime.AddPemSource(pem)
If success = False Then
    Log(mime.LastErrorText)
    Return
End If


'  Decrypt using the keys from the added PEM source(s).
success = mime.Decrypt
If success = False Then
    Log(mime.LastErrorText)
    Return
End If

Log("Decrypted.")