DKIM: How Email Signing Works
DKIM (DomainKeys Identified Mail) adds a digital signature to your emails, allowing receiving servers to verify that messages actually came from your domain and were not modified in transit.
What is DKIM?
DKIM is an email authentication method that uses public-key cryptography to digitally sign outgoing emails. Think of it as a tamper-evident seal for your messages. When you send an email, your mail server creates a unique signature using a private key that only you control. Recipients can then verify this signature using a public key published in your DNS records.
This cryptographic approach provides two key guarantees:
The email genuinely came from the domain claiming to send it, verified through the cryptographic signature.
The email content has not been altered since it was signed, protecting against tampering during transit.
How DKIM Works
The DKIM process involves two main steps: signing when sending and verification when receiving.
Signing (Outbound)
- Your mail server takes the email content (headers and body)
- It creates a hash of this content using a cryptographic algorithm
- The hash is encrypted with your domain's private key to create the signature
- The signature is added to the email as a DKIM-Signature header
Verification (Inbound)
- The receiving server extracts the DKIM-Signature header
- It looks up the public key in your domain's DNS records
- Using the public key, it decrypts the signature to get the original hash
- It calculates its own hash of the received message
- If both hashes match, the signature is valid
DKIM Record Structure
DKIM public keys are published as TXT records in DNS. The record is located at a specific subdomain that combines the selector with "_domainkey".
Host: selector._domainkey.example.com
Type: TXT
Value: v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A...Record Components
| Tag | Required | Description |
|---|---|---|
| v=DKIM1 | Yes | Version identifier (always DKIM1) |
| k=rsa | No | Key type (rsa is default, ed25519 also supported) |
| p=... | Yes | Base64-encoded public key |
| t=s | No | Flags (s = strict mode, no subdomains) |
| h=sha256 | No | Hash algorithms allowed (sha256 recommended) |
The Selector
The selector is a name you choose that identifies which key to use. This allows you to have multiple DKIM keys for different purposes or to rotate keys without disruption.
# Common selector naming conventions
google._domainkey.example.com # For Google Workspace
s1._domainkey.example.com # Generic numbered selector
2024-01._domainkey.example.com # Date-based for rotation
mail._domainkey.example.com # Generic mail selectorThe DKIM-Signature Header
When your mail server signs an email, it adds a DKIM-Signature header. This header contains all the information needed to verify the signature.
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=example.com; s=selector1;
h=from:to:subject:date:message-id;
bh=2jUSOH9NhtVGCQWNr9BrIAPreKQjO6Sn7XIkfJVOzv8=;
b=AuUoFEfDxTDkHlLXSZEpZj79LICEps6eda7W3deTVFOk4yAUoqOB
4nujwH2vXN6P1ksKU5kMdC1sNTzmJIJv/zMbh5N/N4N/B...Key Header Tags
| Tag | Meaning |
|---|---|
| v=1 | DKIM signature version |
| a=rsa-sha256 | Algorithm used (RSA with SHA-256 hash) |
| c=relaxed/relaxed | Canonicalization for header/body (relaxed allows minor changes) |
| d=example.com | Signing domain (must match or be parent of From domain for DMARC alignment) |
| s=selector1 | Selector name to find the public key in DNS |
| h=from:to:subject... | List of headers included in the signature |
| bh=... | Body hash (hash of the email body) |
| b=... | The actual signature value (base64 encoded) |
Setting Up DKIM
The exact steps vary by email provider, but the general process is:
Step 1: Generate a Key Pair
Most email services generate this for you. If you need to generate your own (for self-hosted mail servers):
# Generate a 2048-bit RSA private key
openssl genrsa -out dkim_private.pem 2048
# Extract the public key
openssl rsa -in dkim_private.pem -pubout -out dkim_public.pemStep 2: Add the DNS Record
Publish your public key as a TXT record. Your email provider typically gives you the exact record to add.
# Example for Google Workspace
Host: google._domainkey
Type: TXT
Value: v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A...
# Example for Microsoft 365
Host: selector1._domainkey
Type: CNAME
Value: selector1-example-com._domainkey.example.onmicrosoft.comStep 3: Enable DKIM Signing
Once DNS propagates (can take up to 48 hours), enable DKIM signing in your email service. This tells your mail server to start signing outgoing messages.
Step 4: Verify the Setup
Send a test email and check the headers to confirm DKIM is working:
Authentication-Results: mx.google.com;
dkim=pass header.i=@example.com header.s=google header.b=abc123Common DKIM Issues
The signature fails to verify at the receiving end. Common causes:
- DNS record not published or has typos
- Email was modified in transit (mailing lists, forwarding)
- Using "simple" canonicalization instead of "relaxed"
- Clock skew between servers (check the t= timestamp)
When rotating to a new key, maintain both old and new keys during the transition:
- Create the new key with a new selector
- Publish the new DNS record
- Wait for DNS propagation (24-48 hours)
- Switch signing to the new key
- Keep the old record for 7+ days for delayed emails
DNS TXT records have a 255-character string limit. For 2048-bit keys, the record must be split:
v=DKIM1; k=rsa; p="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A..."
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQ..."Most DNS providers handle this automatically, but verify the record is correctly published.
Choose selector names that help with organization and rotation:
- Include the provider name (google, microsoft, sendgrid)
- Use date-based names for rotation (2024q1, 202401)
- Avoid generic names that make tracking difficult
- Document which selector is used by which service
Summary
- 1.DKIM uses public-key cryptography to sign emails and prove authenticity
- 2.The private key signs; the public key (in DNS) verifies
- 3.Selectors allow multiple keys for different services and rotation
- 4.DKIM is required for DMARC alignment and helps email deliverability
Resources
Check Your DKIM Configuration
Use our scanner to verify your domain has DKIM configured correctly, along with SPF and DMARC records for complete email authentication.
Scan Your Domain