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

Prepare a DKIM public key and DNS TXT record

See more DKIM Examples

Start with an RSA signing key in dkim-private.pem. This writes its public half to dkim-public.pem for local testing and prints the TXT record to enter at your DNS provider. Keep the private PEM on the signer. Use a 2048-bit or larger RSA key for a new deployment. DKIM RSA key sizes.

Use your signing domain and selector in place of example.com and mail2026. A provider that appends the zone usually wants just mail2026._domainkey as the name. If the value exceeds 255 bytes, split it into adjacent character strings within one TXT record. DNS joins those strings without adding spaces. Publish a new selector before rotating keys and keep the old record while previously signed mail is still in transit.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_PrivateKey
oleobject loo_PublicKey
string ls_PValue
string ls_Selector
string ls_Domain

li_Success = 0

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

//  Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
loo_PrivateKey = create oleobject
li_rc = loo_PrivateKey.ConnectToNewObject("Chilkat.PrivateKey")
if li_rc < 0 then
    destroy loo_PrivateKey
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_PrivateKey.LoadAnyFormatFile("dkim-private.pem","")
if li_Success <> 1 then
    Write-Debug loo_PrivateKey.LastErrorText
    destroy loo_PrivateKey
    return
end if

loo_PublicKey = create oleobject
li_rc = loo_PublicKey.ConnectToNewObject("Chilkat.PublicKey")

li_Success = loo_PrivateKey.ToPublicKey(loo_PublicKey)
if li_Success <> 1 then
    Write-Debug loo_PrivateKey.LastErrorText
    destroy loo_PrivateKey
    destroy loo_PublicKey
    return
end if

if loo_PublicKey.KeyType <> "rsa" then
    Write-Debug "Choose an RSA key for this rsa-sha256 example."
    destroy loo_PrivateKey
    destroy loo_PublicKey
    return
end if

//  false selects SubjectPublicKeyInfo. The p= value is base64 DER, on one line.
ls_PValue = loo_PublicKey.GetEncoded(0,"base64")
if ls_PValue = "" then
    Write-Debug loo_PublicKey.LastErrorText
    destroy loo_PrivateKey
    destroy loo_PublicKey
    return
end if

li_Success = loo_PublicKey.SavePemFile(0,"dkim-public.pem")
if li_Success <> 1 then
    Write-Debug loo_PublicKey.LastErrorText
    destroy loo_PrivateKey
    destroy loo_PublicKey
    return
end if

ls_Selector = "mail2026"
ls_Domain = "example.com"
Write-Debug "TXT owner: " + ls_Selector + "._domainkey." + ls_Domain
Write-Debug "TXT value: v=DKIM1; k=rsa; p=" + ls_PValue
Write-Debug "Saved dkim-public.pem."


destroy loo_PrivateKey
destroy loo_PublicKey