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

Open an RSA-OAEP and AES-128-CBC message

See more Email Object Examples

Read the output of the matching encryption example. Chilkat takes the algorithm parameters from the CMS envelope. Give it the recipient’s private key, then read the recovered subject and body only after decryption succeeds.

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();
//  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 CkPem();
success = pem.LoadPemFile("recipient.pem","PEM_PASSWORD");
if (success !== true) {
    console.log(pem.LastErrorText);
    return;
}

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

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

if (email.ReceivedEncrypted !== true) {
    console.log("The input has no S/MIME encryption layer.");
    return;
}

if (email.Decrypted !== true) {
    console.log("Could not decrypt with the available private keys.");
    return;
}

console.log("Subject: " + email.Subject);
if (email.HasPlainTextBody() == true) {
    console.log(email.GetPlainTextBody());
}

success = email.SaveEml("oaep-decrypted.eml");
if (success !== true) {
    console.log(email.LastErrorText);
    return;
}

console.log("Saved oaep-decrypted.eml.");