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

PureBasic
IncludeFile "CkEmail.pb"
IncludeFile "CkCert.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkEmail::ckAddPfxSourceFile(email,"recipient.pfx","RECIPIENT_PASSWORD")
    If success <> 1
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    success = CkEmail::ckLoadEml(email,"signed-encrypted.eml")
    If success <> 1
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    If (CkEmail::ckReceivedEncrypted(email) <> 1) OR (CkEmail::ckDecrypted(email) <> 1)
        Debug "Expected a successfully decrypted S/MIME envelope."
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    If (CkEmail::ckReceivedSigned(email) <> 1) OR (CkEmail::ckSignaturesValid(email) <> 1)
        Debug "Expected a valid signature inside the envelope."
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    ;  This message has one signer. Examine its certificate after verification.
    signer.i = CkCert::ckCreate()
    If signer.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkEmail::ckLastSignerCert(email,0,signer)
    If success <> 1
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        CkCert::ckDispose(signer)
        ProcedureReturn
    EndIf

    Debug "Signer certificate: " + CkCert::ckSubjectDN(signer)
    Debug "Recovered subject: " + CkEmail::ckSubject(email)
    Debug "Recovered attachments: " + Str(CkEmail::ckNumAttachments(email))


    CkEmail::ckDispose(email)
    CkCert::ckDispose(signer)


    ProcedureReturn
EndProcedure