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 <C_CkEmailW.h>
#include <C_CkCertW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmailW email;
    HCkCertW signer;

    success = FALSE;

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

    email = CkEmailW_Create();
    success = CkEmailW_AddPfxSourceFile(email,L"recipient.pfx",L"RECIPIENT_PASSWORD");
    if (success != TRUE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkEmailW_Dispose(email);
        return;
    }

    success = CkEmailW_LoadEml(email,L"signed-encrypted.eml");
    if (success != TRUE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkEmailW_Dispose(email);
        return;
    }

    if ((CkEmailW_getReceivedEncrypted(email) != TRUE) || (CkEmailW_getDecrypted(email) != TRUE)) {
        wprintf(L"Expected a successfully decrypted S/MIME envelope.\n");
        CkEmailW_Dispose(email);
        return;
    }

    if ((CkEmailW_getReceivedSigned(email) != TRUE) || (CkEmailW_getSignaturesValid(email) != TRUE)) {
        wprintf(L"Expected a valid signature inside the envelope.\n");
        CkEmailW_Dispose(email);
        return;
    }

    //  This message has one signer. Examine its certificate after verification.
    signer = CkCertW_Create();
    success = CkEmailW_LastSignerCert(email,0,signer);
    if (success != TRUE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkEmailW_Dispose(email);
        CkCertW_Dispose(signer);
        return;
    }

    wprintf(L"Signer certificate: %s\n",CkCertW_subjectDN(signer));
    wprintf(L"Recovered subject: %s\n",CkEmailW_subject(email));
    wprintf(L"Recovered attachments: %d\n",CkEmailW_getNumAttachments(email));


    CkEmailW_Dispose(email);
    CkCertW_Dispose(signer);

    }