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

Unicode C
#include <C_CkPrivateKeyW.h>
#include <C_CkPublicKeyW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPrivateKeyW privateKey;
    HCkPublicKeyW publicKey;
    const wchar_t *pValue;
    const wchar_t *selector;
    const wchar_t *domain;

    success = FALSE;

    //  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 = CkPrivateKeyW_Create();
    success = CkPrivateKeyW_LoadAnyFormatFile(privateKey,L"dkim-private.pem",L"");
    if (success != TRUE) {
        wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privateKey));
        CkPrivateKeyW_Dispose(privateKey);
        return;
    }

    publicKey = CkPublicKeyW_Create();
    success = CkPrivateKeyW_ToPublicKey(privateKey,publicKey);
    if (success != TRUE) {
        wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privateKey));
        CkPrivateKeyW_Dispose(privateKey);
        CkPublicKeyW_Dispose(publicKey);
        return;
    }

    if (CkPublicKeyW_keyType(publicKey) != L"rsa") {
        wprintf(L"Choose an RSA key for this rsa-sha256 example.\n");
        CkPrivateKeyW_Dispose(privateKey);
        CkPublicKeyW_Dispose(publicKey);
        return;
    }

    //  false selects SubjectPublicKeyInfo. The p= value is base64 DER, on one line.
    pValue = CkPublicKeyW_getEncoded(publicKey,FALSE,L"base64");
    if (pValue == L"") {
        wprintf(L"%s\n",CkPublicKeyW_lastErrorText(publicKey));
        CkPrivateKeyW_Dispose(privateKey);
        CkPublicKeyW_Dispose(publicKey);
        return;
    }

    success = CkPublicKeyW_SavePemFile(publicKey,FALSE,L"dkim-public.pem");
    if (success != TRUE) {
        wprintf(L"%s\n",CkPublicKeyW_lastErrorText(publicKey));
        CkPrivateKeyW_Dispose(privateKey);
        CkPublicKeyW_Dispose(publicKey);
        return;
    }

    selector = L"mail2026";
    domain = L"example.com";
    wprintf(L"TXT owner: %s._domainkey.%s\n",selector,domain);
    wprintf(L"TXT value: v=DKIM1; k=rsa; p=%s\n",pValue);
    wprintf(L"Saved dkim-public.pem.\n");


    CkPrivateKeyW_Dispose(privateKey);
    CkPublicKeyW_Dispose(publicKey);

    }