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

Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

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

var email = new CkEmail();
success = email.AddPfxSourceFile("recipient.pfx","RECIPIENT_PASSWORD");
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 CkCert();
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);