Sample code for 30+ languages & platforms
PowerShell Requires Chilkat v11.0.0+

Sign an email with a key in the Windows Current User certificate store

See more DKIM Examples

Run on Windows as the account that owns the signing certificate. Copy its thumbprint from Current User > Personal and supply it as hexadecimal digits. The thumbprint selects a certificate. The DKIM signature itself uses RSA-SHA256.

The certificate needs an accessible, exportable RSA private key for GetPrivateKey. Publish its matching public key under the selector in DNS. DKIM recipients use that DNS key to verify the message. An X.509 chain is unnecessary for this step.

Chilkat PowerShell Downloads

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

$success = $false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

$store = New-Object Chilkat.CertStore
$success = $store.OpenCurrentUserStore($true)
if ($success -ne $true) {
    $($store.LastErrorText)
    exit
}

$search = New-Object Chilkat.JsonObject
$search.UpdateString("thumbprint","YOUR_CERTIFICATE_THUMBPRINT")
$cert = New-Object Chilkat.Cert
$success = $store.FindCert($search,$cert)
if ($success -ne $true) {
    $($store.LastErrorText)
    exit
}

if ($cert.HasPrivateKey() -ne $true) {
    $("The selected certificate has no accessible private key.")
    exit
}

$privateKey = New-Object Chilkat.PrivateKey
$success = $cert.GetPrivateKey($privateKey)
if ($success -ne $true) {
    $($cert.LastErrorText)
    exit
}

$signer = New-Object Chilkat.Dkim
$signer.DkimDomain = "example.com"
$signer.DkimSelector = "mail2026"
$signer.DkimAlg = "rsa-sha256"
$success = $signer.SetDkimPrivateKey($privateKey)
if ($success -ne $true) {
    $($signer.LastErrorText)
    exit
}

$message = New-Object Chilkat.BinData
$success = $message.LoadFile("message.eml")
if ($success -ne $true) {
    $("Could not read message.eml.")
    exit
}

$success = $signer.DkimSign($message)
if ($success -ne $true) {
    $($signer.LastErrorText)
    exit
}

$success = $message.WriteFile("signed.eml")
if ($success -ne $true) {
    $("Could not write signed.eml.")
    exit
}

$("Saved signed.eml.")