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

CkPython
import sys
import chilkat

success = False

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

store = chilkat.CkCertStore()
success = store.OpenCurrentUserStore(True)
if (success != True):
    print(store.lastErrorText())
    sys.exit()

search = chilkat.CkJsonObject()
search.UpdateString("thumbprint","YOUR_CERTIFICATE_THUMBPRINT")
cert = chilkat.CkCert()
success = store.FindCert(search,cert)
if (success != True):
    print(store.lastErrorText())
    sys.exit()

if (cert.HasPrivateKey() != True):
    print("The selected certificate has no accessible private key.")
    sys.exit()

privateKey = chilkat.CkPrivateKey()
success = cert.GetPrivateKey(privateKey)
if (success != True):
    print(cert.lastErrorText())
    sys.exit()

signer = chilkat.CkDkim()
signer.put_DkimDomain("example.com")
signer.put_DkimSelector("mail2026")
signer.put_DkimAlg("rsa-sha256")
success = signer.SetDkimPrivateKey(privateKey)
if (success != True):
    print(signer.lastErrorText())
    sys.exit()

message = chilkat.CkBinData()
success = message.LoadFile("message.eml")
if (success != True):
    print("Could not read message.eml.")
    sys.exit()

success = signer.DkimSign(message)
if (success != True):
    print(signer.lastErrorText())
    sys.exit()

success = message.WriteFile("signed.eml")
if (success != True):
    print("Could not write signed.eml.")
    sys.exit()

print("Saved signed.eml.")