CkJava Requires Chilkat v11.8.0+
CkJava
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 CkJava Downloads
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();
// The recipient's certificate and private key are in a PEM file. The 2nd argument of LoadPemFile
// is the password of the private key.
CkPem pem = new CkPem();
success = pem.LoadPemFile("recipient.pem","RECIPIENT_PASSWORD");
if (success != true) {
System.out.println(pem.lastErrorText());
return;
}
success = email.AddPemSource(pem);
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());
}
}