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

Chilkat CkJava Downloads

CkJava
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

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

    CkEmail email = new CkEmail();
    success = email.AddPfxSourceFile("recipient.pfx","RECIPIENT_PASSWORD");
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

    success = email.LoadEml("signed-encrypted.eml");
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

    if ((email.get_ReceivedEncrypted() != true) || (email.get_Decrypted() != true)) {
        System.out.println("Expected a successfully decrypted S/MIME envelope.");
        return;
        }

    if ((email.get_ReceivedSigned() != true) || (email.get_SignaturesValid() != true)) {
        System.out.println("Expected a valid signature inside the envelope.");
        return;
        }

    //  This message has one signer. Examine its certificate after verification.
    CkCert signer = new CkCert();
    success = email.LastSignerCert(0,signer);
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

    System.out.println("Signer certificate: " + signer.subjectDN());
    System.out.println("Recovered subject: " + email.subject());
    System.out.println("Recovered attachments: " + email.get_NumAttachments());
  }
}