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

CkPython
import sys
import chilkat

success = False

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

email = chilkat.CkEmail()
#  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 = chilkat.CkPem()
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.get_ReceivedEncrypted() != True) or (email.get_Decrypted() != True)):
    print("Expected a successfully decrypted S/MIME envelope.")
    sys.exit()

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

#  This message has one signer. Examine its certificate after verification.
signer = chilkat.CkCert()
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.get_NumAttachments()))