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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Store
oleobject loo_Search
oleobject loo_Cert
oleobject loo_PrivateKey
oleobject loo_Signer
oleobject loo_Message

li_Success = 0

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

loo_Store = create oleobject
li_rc = loo_Store.ConnectToNewObject("Chilkat.CertStore")
if li_rc < 0 then
    destroy loo_Store
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Store.OpenCurrentUserStore(1)
if li_Success <> 1 then
    Write-Debug loo_Store.LastErrorText
    destroy loo_Store
    return
end if

loo_Search = create oleobject
li_rc = loo_Search.ConnectToNewObject("Chilkat.JsonObject")

loo_Search.UpdateString("thumbprint","YOUR_CERTIFICATE_THUMBPRINT")
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Store.FindCert(loo_Search,loo_Cert)
if li_Success <> 1 then
    Write-Debug loo_Store.LastErrorText
    destroy loo_Store
    destroy loo_Search
    destroy loo_Cert
    return
end if

if loo_Cert.HasPrivateKey() <> 1 then
    Write-Debug "The selected certificate has no accessible private key."
    destroy loo_Store
    destroy loo_Search
    destroy loo_Cert
    return
end if

loo_PrivateKey = create oleobject
li_rc = loo_PrivateKey.ConnectToNewObject("Chilkat.PrivateKey")

li_Success = loo_Cert.GetPrivateKey(loo_PrivateKey)
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Store
    destroy loo_Search
    destroy loo_Cert
    destroy loo_PrivateKey
    return
end if

loo_Signer = create oleobject
li_rc = loo_Signer.ConnectToNewObject("Chilkat.Dkim")

loo_Signer.DkimDomain = "example.com"
loo_Signer.DkimSelector = "mail2026"
loo_Signer.DkimAlg = "rsa-sha256"
li_Success = loo_Signer.SetDkimPrivateKey(loo_PrivateKey)
if li_Success <> 1 then
    Write-Debug loo_Signer.LastErrorText
    destroy loo_Store
    destroy loo_Search
    destroy loo_Cert
    destroy loo_PrivateKey
    destroy loo_Signer
    return
end if

loo_Message = create oleobject
li_rc = loo_Message.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Message.LoadFile("message.eml")
if li_Success <> 1 then
    Write-Debug "Could not read message.eml."
    destroy loo_Store
    destroy loo_Search
    destroy loo_Cert
    destroy loo_PrivateKey
    destroy loo_Signer
    destroy loo_Message
    return
end if

li_Success = loo_Signer.DkimSign(loo_Message)
if li_Success <> 1 then
    Write-Debug loo_Signer.LastErrorText
    destroy loo_Store
    destroy loo_Search
    destroy loo_Cert
    destroy loo_PrivateKey
    destroy loo_Signer
    destroy loo_Message
    return
end if

li_Success = loo_Message.WriteFile("signed.eml")
if li_Success <> 1 then
    Write-Debug "Could not write signed.eml."
    destroy loo_Store
    destroy loo_Search
    destroy loo_Cert
    destroy loo_PrivateKey
    destroy loo_Signer
    destroy loo_Message
    return
end if

Write-Debug "Saved signed.eml."


destroy loo_Store
destroy loo_Search
destroy loo_Cert
destroy loo_PrivateKey
destroy loo_Signer
destroy loo_Message