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

Unicode C++
#include <CkEmailW.h>
#include <CkCertW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkEmailW email;
    success = email.AddPfxSourceFile(L"recipient.pfx",L"RECIPIENT_PASSWORD");
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    success = email.LoadEml(L"signed-encrypted.eml");
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    if ((email.get_ReceivedEncrypted() != true) || (email.get_Decrypted() != true)) {
        wprintf(L"Expected a successfully decrypted S/MIME envelope.\n");
        return;
    }

    if ((email.get_ReceivedSigned() != true) || (email.get_SignaturesValid() != true)) {
        wprintf(L"Expected a valid signature inside the envelope.\n");
        return;
    }

    //  This message has one signer. Examine its certificate after verification.
    CkCertW signer;
    success = email.LastSignerCert(0,signer);
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    wprintf(L"Signer certificate: %s\n",signer.subjectDN());
    wprintf(L"Recovered subject: %s\n",email.subject());
    wprintf(L"Recovered attachments: %d\n",email.get_NumAttachments());
    }