Java Requires Chilkat v11.8.0+
Java
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 Java Downloads
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.Pem;
public class ChilkatExample {
public static void main(String[] args) {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Email email = new 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.
Pem pem = new Pem();
try {
pem.loadPemFile("recipient.pem", "RECIPIENT_PASSWORD");
email.addPemSource(pem);
email.loadEml("signed-encrypted.eml");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
if ((!email.getReceivedEncrypted()) || (!email.getDecrypted())) {
System.out.println("Expected a successfully decrypted S/MIME envelope.");
return;
}
if ((!email.getReceivedSigned()) || (!email.getSignaturesValid())) {
System.out.println("Expected a valid signature inside the envelope.");
return;
}
// This message has one signer. Examine its certificate after verification.
Cert signer = new Cert();
try {
email.lastSignerCert(0, signer);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Signer certificate: " + signer.getSubjectDN());
System.out.println("Recovered subject: " + email.getSubject());
System.out.println("Recovered attachments: " + email.getNumAttachments());
}
}