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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPrivateKey
    Variant vPublicKey
    Handle hoPublicKey
    String sPValue
    String sSelector
    String sDomain
    String sTemp1

    Move False To iSuccess

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

    //  Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
    Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivateKey
    If (Not(IsComObjectCreated(hoPrivateKey))) Begin
        Send CreateComObject of hoPrivateKey
    End
    Get ComLoadAnyFormatFile Of hoPrivateKey "dkim-private.pem" "" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoPrivateKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatPublicKey)) To hoPublicKey
    If (Not(IsComObjectCreated(hoPublicKey))) Begin
        Send CreateComObject of hoPublicKey
    End
    Get pvComObject of hoPublicKey to vPublicKey
    Get ComToPublicKey Of hoPrivateKey vPublicKey To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoPrivateKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComKeyType Of hoPublicKey To sTemp1
    If (sTemp1 <> "rsa") Begin
        Showln "Choose an RSA key for this rsa-sha256 example."
        Procedure_Return
    End

    //  false selects SubjectPublicKeyInfo. The p= value is base64 DER, on one line.
    Get ComGetEncoded Of hoPublicKey False "base64" To sPValue
    If (sPValue = "") Begin
        Get ComLastErrorText Of hoPublicKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSavePemFile Of hoPublicKey False "dkim-public.pem" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoPublicKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Move "mail2026" To sSelector
    Move "example.com" To sDomain
    Showln "TXT owner: " sSelector "._domainkey." sDomain
    Showln "TXT value: v=DKIM1; k=rsa; p=" sPValue
    Showln "Saved dkim-public.pem."


End_Procedure