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

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

$email = New-Object Chilkat.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 = New-Object Chilkat.Pem
$success = $pem.LoadPemFile("recipient.pem","RECIPIENT_PASSWORD")
if ($success -ne $true) {
    $($pem.LastErrorText)
    exit
}

$success = $email.AddPemSource($pem)
if ($success -ne $true) {
    $($email.LastErrorText)
    exit
}

$success = $email.LoadEml("signed-encrypted.eml")
if ($success -ne $true) {
    $($email.LastErrorText)
    exit
}

if (($email.ReceivedEncrypted -ne $true) -or ($email.Decrypted -ne $true)) {
    $("Expected a successfully decrypted S/MIME envelope.")
    exit
}

if (($email.ReceivedSigned -ne $true) -or ($email.SignaturesValid -ne $true)) {
    $("Expected a valid signature inside the envelope.")
    exit
}

#  This message has one signer. Examine its certificate after verification.
$signer = New-Object Chilkat.Cert
$success = $email.LastSignerCert(0,$signer)
if ($success -ne $true) {
    $($email.LastErrorText)
    exit
}

$("Signer certificate: " + $signer.SubjectDN)
$("Recovered subject: " + $email.Subject)
$("Recovered attachments: " + $email.NumAttachments)