Sample code for 30+ languages & platforms
Chilkat2-Python

Get the Certificate Used for Decryption

See more MIME Examples

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

After a successful decryption, LastDecryptCert loads the certificate(s) actually used into a Cert object, indexed from zero up to NumDecryptCerts minus one.

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. Following decryption or security unwrapping, Chilkat records which certificate(s) were used. LastDecryptCert retrieves them so the application can report or verify the identity involved.

Chilkat Chilkat2-Python Downloads

Chilkat2-Python
import sys
import chilkat2

success = False

mime = chilkat2.Mime()
success = mime.LoadMimeFile("qa_data/encrypted.eml")
if (success == False):
    print(mime.LastErrorText)
    sys.exit()

# Configure a decryption source and decrypt.
pfxPassword = "myPfxPassword"
success = mime.AddPfxSourceFile("qa_data/recipient.pfx",pfxPassword)
if (success == False):
    print(mime.LastErrorText)
    sys.exit()

success = mime.Decrypt()
if (success == False):
    print(mime.LastErrorText)
    sys.exit()

# After a successful decryption, load the certificate(s) that were used.  The 1st argument is the
# zero-based index and the 2nd is a Cert that receives the certificate.
n = mime.NumDecryptCerts
cert = chilkat2.Cert()

for i in range(0,n):
    success = mime.LastDecryptCert(i,cert)
    if (success == False):
        print(mime.LastErrorText)
        sys.exit()

    print("Decrypted with: " + cert.SubjectCN)