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

Node.js
NODEJS_PRELUDE

function chilkatExample() {

    var success = false;

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

    var email = new chilkat.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.
    var pem = new chilkat.Pem();
    success = pem.LoadPemFile("recipient.pem","RECIPIENT_PASSWORD");
    if (success !== true) {
        console.log(pem.LastErrorText);
        return;
    }

    success = email.AddPemSource(pem);
    if (success !== true) {
        console.log(email.LastErrorText);
        return;
    }

    success = email.LoadEml("signed-encrypted.eml");
    if (success !== true) {
        console.log(email.LastErrorText);
        return;
    }

    if ((email.ReceivedEncrypted !== true) || (email.Decrypted !== true)) {
        console.log("Expected a successfully decrypted S/MIME envelope.");
        return;
    }

    if ((email.ReceivedSigned !== true) || (email.SignaturesValid !== true)) {
        console.log("Expected a valid signature inside the envelope.");
        return;
    }

    //  This message has one signer. Examine its certificate after verification.
    var signer = new chilkat.Cert();
    success = email.LastSignerCert(0,signer);
    if (success !== true) {
        console.log(email.LastErrorText);
        return;
    }

    console.log("Signer certificate: " + signer.SubjectDN);
    console.log("Recovered subject: " + email.Subject);
    console.log("Recovered attachments: " + email.NumAttachments);

}

chilkatExample();