Sample code for 30+ languages & platforms
C++ Requires Chilkat v11.8.0+

Decrypt an S/MIME email and verify its signature

See more Email Object Examples

Register the recipient’s certificate and private key (from a PEM file) 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 C++ Downloads

C++
#include <CkEmail.h>
#include <CkPem.h>
#include <CkCert.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkEmail email;
    //  The recipient's certificate and private key are in a PEM file.  The 2nd argument of LoadPemFile
    //  is the password of the private key.
    CkPem pem;
    success = pem.LoadPemFile("recipient.pem","RECIPIENT_PASSWORD");
    if (success != true) {
        std::cout << pem.lastErrorText() << "\r\n";
        return;
    }

    success = email.AddPemSource(pem);
    if (success != true) {
        std::cout << email.lastErrorText() << "\r\n";
        return;
    }

    success = email.LoadEml("signed-encrypted.eml");
    if (success != true) {
        std::cout << email.lastErrorText() << "\r\n";
        return;
    }

    if ((email.get_ReceivedEncrypted() != true) || (email.get_Decrypted() != true)) {
        std::cout << "Expected a successfully decrypted S/MIME envelope." << "\r\n";
        return;
    }

    if ((email.get_ReceivedSigned() != true) || (email.get_SignaturesValid() != true)) {
        std::cout << "Expected a valid signature inside the envelope." << "\r\n";
        return;
    }

    //  This message has one signer. Examine its certificate after verification.
    CkCert signer;
    success = email.LastSignerCert(0,signer);
    if (success != true) {
        std::cout << email.lastErrorText() << "\r\n";
        return;
    }

    std::cout << "Signer certificate: " << signer.subjectDN() << "\r\n";
    std::cout << "Recovered subject: " << email.subject() << "\r\n";
    std::cout << "Recovered attachments: " << email.get_NumAttachments() << "\r\n";
    }