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

Decrypt an S/MIME email and verify its signature

See more Email Object Examples

Register the recipient’s certificate and private key (from a PEM file) before opening signed-encrypted.eml. This workflow expects both encryption and a signature, so it checks both layers before reading the message. Cryptographic signature validity is one input to a signer-trust decision.

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

Dim email As ChilkatEmail
email.Initialize
'  The recipient's certificate and private key are in a PEM file.  The 2nd argument of LoadPemFile
'  is the password of the private key.
Dim pem As ChilkatPem
pem.Initialize
success = pem.LoadPemFile("recipient.pem", "RECIPIENT_PASSWORD")
If success <> True Then
    Log(pem.LastErrorText)
    Return
End If

success = email.AddPemSource(pem)
If success <> True Then
    Log(email.LastErrorText)
    Return
End If

success = email.LoadEml("signed-encrypted.eml")
If success <> True Then
    Log(email.LastErrorText)
    Return
End If

If (email.ReceivedEncrypted <> True) Or (email.Decrypted <> True) Then
    Log("Expected a successfully decrypted S/MIME envelope.")
    Return
End If

If (email.ReceivedSigned <> True) Or (email.SignaturesValid <> True) Then
    Log("Expected a valid signature inside the envelope.")
    Return
End If

'  This message has one signer. Examine its certificate after verification.
Dim signer As ChilkatCert
signer.Initialize("signer")
success = email.LastSignerCert(0, signer)
If success <> True Then
    Log(email.LastErrorText)
    Return
End If

Log("Signer certificate: " & signer.SubjectDN)
Log("Recovered subject: " & email.Subject)
Log("Recovered attachments: " & email.NumAttachments)