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

Xbase++
LOCAL nSuccess
LOCAL oStore
LOCAL oSearch
LOCAL oCert
LOCAL oPrivateKey
LOCAL oSigner
LOCAL oMessage

nSuccess := 0

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

oStore := CreateObject("Chilkat.CertStore")
nSuccess := oStore:OpenCurrentUserStore(1)
IF (nSuccess != 1)
    ? oStore:LastErrorText
    oStore:destroy()
    RETURN
ENDIF

oSearch := CreateObject("Chilkat.JsonObject")
oSearch:UpdateString("thumbprint", "YOUR_CERTIFICATE_THUMBPRINT")
oCert := CreateObject("Chilkat.Cert")
nSuccess := oStore:FindCert(oSearch, oCert)
IF (nSuccess != 1)
    ? oStore:LastErrorText
    oStore:destroy()
    oSearch:destroy()
    oCert:destroy()
    RETURN
ENDIF

IF (oCert:HasPrivateKey() != 1)
    ? "The selected certificate has no accessible private key."
    oStore:destroy()
    oSearch:destroy()
    oCert:destroy()
    RETURN
ENDIF

oPrivateKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oCert:GetPrivateKey(oPrivateKey)
IF (nSuccess != 1)
    ? oCert:LastErrorText
    oStore:destroy()
    oSearch:destroy()
    oCert:destroy()
    oPrivateKey:destroy()
    RETURN
ENDIF

oSigner := CreateObject("Chilkat.Dkim")
oSigner:DkimDomain := "example.com"
oSigner:DkimSelector := "mail2026"
oSigner:DkimAlg := "rsa-sha256"
nSuccess := oSigner:SetDkimPrivateKey(oPrivateKey)
IF (nSuccess != 1)
    ? oSigner:LastErrorText
    oStore:destroy()
    oSearch:destroy()
    oCert:destroy()
    oPrivateKey:destroy()
    oSigner:destroy()
    RETURN
ENDIF

oMessage := CreateObject("Chilkat.BinData")
nSuccess := oMessage:LoadFile("message.eml")
IF (nSuccess != 1)
    ? "Could not read message.eml."
    oStore:destroy()
    oSearch:destroy()
    oCert:destroy()
    oPrivateKey:destroy()
    oSigner:destroy()
    oMessage:destroy()
    RETURN
ENDIF

nSuccess := oSigner:DkimSign(oMessage)
IF (nSuccess != 1)
    ? oSigner:LastErrorText
    oStore:destroy()
    oSearch:destroy()
    oCert:destroy()
    oPrivateKey:destroy()
    oSigner:destroy()
    oMessage:destroy()
    RETURN
ENDIF

nSuccess := oMessage:WriteFile("signed.eml")
IF (nSuccess != 1)
    ? "Could not write signed.eml."
    oStore:destroy()
    oSearch:destroy()
    oCert:destroy()
    oPrivateKey:destroy()
    oSigner:destroy()
    oMessage:destroy()
    RETURN
ENDIF

? "Saved signed.eml."

oStore:destroy()
oSearch:destroy()
oCert:destroy()
oPrivateKey:destroy()
oSigner:destroy()
oMessage:destroy()