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

Decrypt a saved S/MIME message with a PEM file

See more Email Object Examples

Register the recipient certificate and private key before loading encrypted.eml. Check that an encryption layer was present and successfully removed before writing a plaintext copy. Choose a protected output directory appropriate for that copy.

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("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;
}

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

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