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

CkPhp
<?php

include("chilkat.php");

$success = false;

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

$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.
$pem = new CkPem();
$success = $pem->LoadPemFile('recipient.pem','RECIPIENT_PASSWORD');
if ($success != true) {
    print $pem->lastErrorText() . "\n";
    exit;
}

$success = $email->AddPemSource($pem);
if ($success != true) {
    print $email->lastErrorText() . "\n";
    exit;
}

$success = $email->LoadEml('signed-encrypted.eml');
if ($success != true) {
    print $email->lastErrorText() . "\n";
    exit;
}

if (($email->get_ReceivedEncrypted() != true) or ($email->get_Decrypted() != true)) {
    print 'Expected a successfully decrypted S/MIME envelope.' . "\n";
    exit;
}

if (($email->get_ReceivedSigned() != true) or ($email->get_SignaturesValid() != true)) {
    print 'Expected a valid signature inside the envelope.' . "\n";
    exit;
}

//  This message has one signer. Examine its certificate after verification.
$signer = new CkCert();
$success = $email->LastSignerCert(0,$signer);
if ($success != true) {
    print $email->lastErrorText() . "\n";
    exit;
}

print 'Signer certificate: ' . $signer->subjectDN() . "\n";
print 'Recovered subject: ' . $email->subject() . "\n";
print 'Recovered attachments: ' . $email->get_NumAttachments() . "\n";

?>