Perl Requires Chilkat v11.8.0+
Perl
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 Perl Downloads
use strict;
use warnings;
use Chilkat2;
# Assumes Chilkat is unlocked. See Global Unlock Sample.
my $email = Chilkat2::Email->new;
# The recipient's certificate and private key are in a PEM file. The 2nd argument of LoadPemFile
# is the password of the private key.
my $pem = Chilkat2::Pem->new;
my $success = $pem->LoadPemFile('recipient.pem', 'RECIPIENT_PASSWORD');
if (!$success) {
print $pem->LastErrorText, "\n";
exit;
}
$success = $email->AddPemSource($pem);
if (!$success) {
print $email->LastErrorText, "\n";
exit;
}
$success = $email->LoadEml('signed-encrypted.eml');
if (!$success) {
print $email->LastErrorText, "\n";
exit;
}
if ((!$email->ReceivedEncrypted) || (!$email->Decrypted)) {
print "Expected a successfully decrypted S/MIME envelope.\n";
exit;
}
if ((!$email->ReceivedSigned) || (!$email->SignaturesValid)) {
print "Expected a valid signature inside the envelope.\n";
exit;
}
# This message has one signer. Examine its certificate after verification.
my $signer = Chilkat2::Cert->new;
$success = $email->LastSignerCert(0, $signer);
if (!$success) {
print $email->LastErrorText, "\n";
exit;
}
print 'Signer certificate: ', $signer->SubjectDN, "\n";
print 'Recovered subject: ', $email->Subject, "\n";
print 'Recovered attachments: ', $email->NumAttachments, "\n";