Sample code for 30+ languages & platforms
Python 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 Python Downloads

Python
import sys
import chilkat2

success = False

# Assumes Chilkat is unlocked. See Global Unlock Sample.

email = chilkat2.Email()
# The recipient's certificate and private key are in a PEM file.  The 2nd argument of LoadPemFile
# is the password of the private key.
pem = chilkat2.Pem()
success = pem.LoadPemFile("recipient.pem","RECIPIENT_PASSWORD")
if (success != True):
    print(pem.LastErrorText)
    sys.exit()

success = email.AddPemSource(pem)
if (success != True):
    print(email.LastErrorText)
    sys.exit()

success = email.LoadEml("signed-encrypted.eml")
if (success != True):
    print(email.LastErrorText)
    sys.exit()

if ((email.ReceivedEncrypted != True) or (email.Decrypted != True)):
    print("Expected a successfully decrypted S/MIME envelope.")
    sys.exit()

if ((email.ReceivedSigned != True) or (email.SignaturesValid != True)):
    print("Expected a valid signature inside the envelope.")
    sys.exit()

# This message has one signer. Examine its certificate after verification.
signer = chilkat2.Cert()
success = email.LastSignerCert(0,signer)
if (success != True):
    print(email.LastErrorText)
    sys.exit()

print("Signer certificate: " + signer.SubjectDN)
print("Recovered subject: " + email.Subject)
print("Recovered attachments: " + str(email.NumAttachments))