DKIM Explained: Digital Signatures for Email Security
Understand how DKIM (DomainKeys Identified Mail) uses cryptographic signatures to verify email authenticity and prevent tampering during transit.
MailSentinel Team
Author
While SPF verifies where an email comes from, DKIM (DomainKeys Identified Mail) verifies that the email hasn't been tampered with in transit. It's like a wax seal on a letter - proof that the message is authentic and unchanged.
How DKIM Works
DKIM uses public-key cryptography to sign outgoing emails and verify them on receipt.
The Signing Process
- Generate a key pair: A private key (kept secret) and public key (published in DNS)
- Sign the email: The sending server creates a hash of certain email headers and body, then encrypts it with the private key
- Add the signature: The encrypted hash is added as a DKIM-Signature header
- Publish the public key: The receiving server can look up your public key in DNS
The Verification Process
When a receiving server gets your email:
- Extracts the DKIM-Signature header
- Looks up the public key from your DNS
- Uses the public key to decrypt the signature
- Calculates its own hash of the email
- Compares the hashes - if they match, the email is verified
Anatomy of a DKIM Signature
Here's what a DKIM-Signature header looks like:
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
4nujc7YopdG5dWLSdNg6xNAZpOPr+kHxt1IrE+NahM6L/LbvaHut
KVdkLLkpVaVVQPzeRDI009SO2Il5Lu7rDNH6mZckBdrIx0orEtZV
4bmp/YzhwvcubU4=Key Components
| Tag | Meaning | Example |
|---|---|---|
v | Version | v=1 |
a | Algorithm | a=rsa-sha256 |
c | Canonicalization | c=relaxed/relaxed |
d | Signing domain | d=example.com |
s | Selector | s=selector1 |
h | Signed headers | h=from:to:subject |
bh | Body hash | Base64-encoded hash |
b | Signature | Base64-encoded signature |
DKIM Selectors
The selector allows you to have multiple DKIM keys for the same domain. This is essential for:
- Using different keys for different sending services
- Rotating keys without downtime
- Separating production and testing environments
Common Selector Patterns
googleorgoog- Google Workspaceselector1,selector2- Microsoft 365s1,s2- SendGridk1- Mailchimp- Custom selectors for other services
Finding DKIM Selectors
DKIM public keys are published as TXT records at:
<selector>._domainkey.<domain>
Example for Google Workspace:
google._domainkey.example.com
Setting Up DKIM
Step 1: Generate Keys
Most email services generate DKIM keys for you. For custom setups:
# Generate a 2048-bit RSA key pair
openssl genrsa -out private.key 2048
openssl rsa -in private.key -pubout -out public.keyStep 2: Publish the Public Key
Create a TXT record in your DNS:
Record: selector._domainkey.example.com
Type: TXT
Value: v=DKIM1; k=rsa; p=MIIBIjANBgkqhki...Step 3: Configure Your Mail Server
Configure your email server or service to sign outgoing emails with the private key.
Step 4: Test the Configuration
Send a test email and check the headers for a valid DKIM-Signature.
DKIM for Third-Party Services
Google Workspace
- Admin Console → Apps → Google Workspace → Gmail → Authenticate Email
- Generate new record
- Add the TXT record to your DNS
- Start authentication
Microsoft 365
- Exchange Admin Center → Protection → DKIM
- Enable DKIM for your domain
- Add the two CNAME records provided
- Enable signing
SendGrid
- Settings → Sender Authentication → Domain Authentication
- Enter your domain
- Add the provided DNS records (CNAME for DKIM)
- Verify
Mailchimp
- Account → Settings → Domains
- Add and verify your domain
- Add the provided DKIM record
- Enable authentication
Key Size Recommendations
| Key Size | Security | Recommendation |
|---|---|---|
| 1024-bit | Minimum | Legacy only |
| 2048-bit | Standard | Recommended |
| 4096-bit | High | May have DNS length issues |
Important: Google and Yahoo now require at least 1024-bit keys. Use 2048-bit for best compatibility and security.
DKIM and DMARC Alignment
For DMARC to pass based on DKIM:
- DKIM signature must be valid
- The
d=domain in the signature must align with the From: header domain
Alignment Modes
Relaxed Alignment (default):
d=mail.example.com → From: hello@example.com ✅
Strict Alignment:
d=mail.example.com → From: hello@example.com ❌
d=example.com → From: hello@example.com ✅
Common DKIM Problems
1. Signature Verification Fails
Causes:
- Email modified in transit (mailing lists, forwarding)
- DNS propagation delays
- Incorrect public key format
Solutions:
- Check DNS record is correctly published
- Verify the public key format
- Wait for DNS propagation (up to 48 hours)
2. Key Not Found
Causes:
- Wrong selector name
- DNS record not published
- TTL caching issues
Solutions:
- Verify selector name matches the signature
- Check DNS with multiple tools
- Clear DNS caches
3. Body Hash Mismatch
Causes:
- Email content modified after signing
- Different canonicalization algorithms
- Character encoding issues
Solutions:
- Check for systems modifying emails in transit
- Use relaxed canonicalization (
c=relaxed/relaxed)
4. Multiple DKIM Signatures
It's valid to have multiple DKIM signatures (e.g., from your domain and your ESP). Receiving servers evaluate each one.
Monitoring DKIM with MailSentinel
MailSentinel helps you stay on top of DKIM:
- Selector Discovery: Automatically find all DKIM selectors for your domain
- Key Validation: Verify key size, format, and DNS publication
- Pass Rate Tracking: Monitor DKIM pass/fail rates from DMARC reports
- Expiration Alerts: Get notified when DKIM keys should be rotated
Best Practices
Do's
- ✅ Use 2048-bit RSA keys
- ✅ Sign important headers (From, To, Subject, Date, Message-ID)
- ✅ Use relaxed canonicalization for resilience
- ✅ Rotate keys annually
- ✅ Monitor DKIM pass rates
Don'ts
- ❌ Share private keys between services
- ❌ Use 512 or 768-bit keys
- ❌ Forget to sign when adding new sending services
- ❌ Ignore DKIM failures in DMARC reports
Conclusion
DKIM is essential for proving your emails are authentic and unmodified. Combined with SPF and DMARC, it creates a robust authentication system that protects your domain and improves deliverability.
Start monitoring your DKIM with MailSentinel to ensure your signatures are always valid.