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

CkPerl
use chilkat();

$success = 0;

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

$store = chilkat::CkCertStore->new();
$success = $store->OpenCurrentUserStore(1);
if ($success != 1) {
    print $store->lastErrorText() . "\r\n";
    exit;
}

$search = chilkat::CkJsonObject->new();
$search->UpdateString("thumbprint","YOUR_CERTIFICATE_THUMBPRINT");
$cert = chilkat::CkCert->new();
$success = $store->FindCert($search,$cert);
if ($success != 1) {
    print $store->lastErrorText() . "\r\n";
    exit;
}

if ($cert->HasPrivateKey() != 1) {
    print "The selected certificate has no accessible private key." . "\r\n";
    exit;
}

$privateKey = chilkat::CkPrivateKey->new();
$success = $cert->GetPrivateKey($privateKey);
if ($success != 1) {
    print $cert->lastErrorText() . "\r\n";
    exit;
}

$signer = chilkat::CkDkim->new();
$signer->put_DkimDomain("example.com");
$signer->put_DkimSelector("mail2026");
$signer->put_DkimAlg("rsa-sha256");
$success = $signer->SetDkimPrivateKey($privateKey);
if ($success != 1) {
    print $signer->lastErrorText() . "\r\n";
    exit;
}

$message = chilkat::CkBinData->new();
$success = $message->LoadFile("message.eml");
if ($success != 1) {
    print "Could not read message.eml." . "\r\n";
    exit;
}

$success = $signer->DkimSign($message);
if ($success != 1) {
    print $signer->lastErrorText() . "\r\n";
    exit;
}

$success = $message->WriteFile("signed.eml");
if ($success != 1) {
    print "Could not write signed.eml." . "\r\n";
    exit;
}

print "Saved signed.eml." . "\r\n";