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

PureBasic
IncludeFile "CkPublicKey.pb"
IncludeFile "CkPrivateKey.pb"

Procedure ChilkatExample()

    success.i = 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.
    privateKey.i = CkPrivateKey::ckCreate()
    If privateKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPrivateKey::ckLoadAnyFormatFile(privateKey,"dkim-private.pem","")
    If success <> 1
        Debug CkPrivateKey::ckLastErrorText(privateKey)
        CkPrivateKey::ckDispose(privateKey)
        ProcedureReturn
    EndIf

    publicKey.i = CkPublicKey::ckCreate()
    If publicKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPrivateKey::ckToPublicKey(privateKey,publicKey)
    If success <> 1
        Debug CkPrivateKey::ckLastErrorText(privateKey)
        CkPrivateKey::ckDispose(privateKey)
        CkPublicKey::ckDispose(publicKey)
        ProcedureReturn
    EndIf

    If CkPublicKey::ckKeyType(publicKey) <> "rsa"
        Debug "Choose an RSA key for this rsa-sha256 example."
        CkPrivateKey::ckDispose(privateKey)
        CkPublicKey::ckDispose(publicKey)
        ProcedureReturn
    EndIf

    ;  false selects SubjectPublicKeyInfo. The p= value is base64 DER, on one line.
    pValue.s = CkPublicKey::ckGetEncoded(publicKey,0,"base64")
    If pValue = ""
        Debug CkPublicKey::ckLastErrorText(publicKey)
        CkPrivateKey::ckDispose(privateKey)
        CkPublicKey::ckDispose(publicKey)
        ProcedureReturn
    EndIf

    success = CkPublicKey::ckSavePemFile(publicKey,0,"dkim-public.pem")
    If success <> 1
        Debug CkPublicKey::ckLastErrorText(publicKey)
        CkPrivateKey::ckDispose(privateKey)
        CkPublicKey::ckDispose(publicKey)
        ProcedureReturn
    EndIf

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


    CkPrivateKey::ckDispose(privateKey)
    CkPublicKey::ckDispose(publicKey)


    ProcedureReturn
EndProcedure