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

Tcl

load ./chilkat.dll

set 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.
set privateKey [new_CkPrivateKey]

set success [CkPrivateKey_LoadAnyFormatFile $privateKey "dkim-private.pem" ""]
if {$success != 1} then {
    puts [CkPrivateKey_lastErrorText $privateKey]
    delete_CkPrivateKey $privateKey
    exit
}

set publicKey [new_CkPublicKey]

set success [CkPrivateKey_ToPublicKey $privateKey $publicKey]
if {$success != 1} then {
    puts [CkPrivateKey_lastErrorText $privateKey]
    delete_CkPrivateKey $privateKey
    delete_CkPublicKey $publicKey
    exit
}

if {[CkPublicKey_keyType $publicKey] != "rsa"} then {
    puts "Choose an RSA key for this rsa-sha256 example."
    delete_CkPrivateKey $privateKey
    delete_CkPublicKey $publicKey
    exit
}

#  false selects SubjectPublicKeyInfo. The p= value is base64 DER, on one line.
set pValue [CkPublicKey_getEncoded $publicKey 0 "base64"]
if {$pValue == ""} then {
    puts [CkPublicKey_lastErrorText $publicKey]
    delete_CkPrivateKey $privateKey
    delete_CkPublicKey $publicKey
    exit
}

set success [CkPublicKey_SavePemFile $publicKey 0 "dkim-public.pem"]
if {$success != 1} then {
    puts [CkPublicKey_lastErrorText $publicKey]
    delete_CkPrivateKey $privateKey
    delete_CkPublicKey $publicKey
    exit
}

set selector "mail2026"
set domain "example.com"
puts "TXT owner: $selector._domainkey.$domain"
puts "TXT value: v=DKIM1; k=rsa; p=$pValue"
puts "Saved dkim-public.pem."

delete_CkPrivateKey $privateKey
delete_CkPublicKey $publicKey