Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

Decrypt an S/MIME email and verify its signature

See more Email Object Examples

Register the recipient’s PFX 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oEmail
LOCAL oSigner

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

oEmail := CreateObject("Chilkat.Email")
nSuccess := oEmail:AddPfxSourceFile("recipient.pfx", "RECIPIENT_PASSWORD")
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oEmail:destroy()
    RETURN
ENDIF

nSuccess := oEmail:LoadEml("signed-encrypted.eml")
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oEmail:destroy()
    RETURN
ENDIF

IF ((oEmail:ReceivedEncrypted != 1) .OR. (oEmail:Decrypted != 1))
    ? "Expected a successfully decrypted S/MIME envelope."
    oEmail:destroy()
    RETURN
ENDIF

IF ((oEmail:ReceivedSigned != 1) .OR. (oEmail:SignaturesValid != 1))
    ? "Expected a valid signature inside the envelope."
    oEmail:destroy()
    RETURN
ENDIF

//  This message has one signer. Examine its certificate after verification.
oSigner := CreateObject("Chilkat.Cert")
nSuccess := oEmail:LastSignerCert(0, oSigner)
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oEmail:destroy()
    oSigner:destroy()
    RETURN
ENDIF

? "Signer certificate: " + oSigner:SubjectDN
? "Recovered subject: " + oEmail:Subject
? "Recovered attachments: " + Str(oEmail:NumAttachments)

oEmail:destroy()
oSigner:destroy()