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#
bool success = false;

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

Chilkat.Email 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.
Chilkat.Pem pem = new Chilkat.Pem();
success = pem.LoadPemFile("recipient.pem","RECIPIENT_PASSWORD");
if (success != true) {
    Debug.WriteLine(pem.LastErrorText);
    return;
}

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

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

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

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

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

Debug.WriteLine("Signer certificate: " + signer.SubjectDN);
Debug.WriteLine("Recovered subject: " + email.Subject);
Debug.WriteLine("Recovered attachments: " + Convert.ToString(email.NumAttachments));