Suped

How do I generate an a=rsa-sha256 key for DKIM?

Matthew Whittaker profile picture
Matthew Whittaker
Co-founder & CTO, Suped
Published 23 May 2025
Updated 16 Aug 2025
9 min read
When setting up email authentication, you'll often encounter terms like DKIM, SPF, and DMARC. Among these, DKIM (DomainKeys Identified Mail) plays a crucial role in verifying the authenticity of outgoing emails and preventing spoofing. A core component of DKIM is the cryptographic key pair, which includes a private key used for signing emails and a public key published in your DNS.
Specifically, you might need to generate an a=rsa-sha256 key for DKIM. This specifies the signing algorithm, using RSA for encryption and SHA-256 for hashing, which is the recommended and widely accepted standard for new DKIM implementations. Generating this key pair securely is fundamental to ensuring your emails pass authentication checks and reach the inbox reliably.
This process involves a few technical steps, typically using a command-line tool like OpenSSL. It’s important to manage these keys carefully to maintain the integrity of your email communications and avoid deliverability issues that could lead to emails being sent to spam folders or even being blocklisted.
Having robust email authentication in place is vital for maintaining a good sender reputation and preventing your domain from being flagged by email providers. A properly configured DKIM record, with the correct key, contributes significantly to your overall email deliverability performance. Let's delve into how to generate this essential key.
Suped DMARC monitoring
Free forever, no credit card required
Learn more
Trusted by teams securing millions of inboxes
Company logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logoCompany logo

Understanding DKIM and RSA-SHA256

DKIM is a method that allows an organization to associate a domain name with an email message, thereby vouching for its authenticity. It uses cryptographic authentication to verify the sender of the email. When an email is sent, the sending mail server generates a digital signature by hashing parts of the email, and then encrypts this hash with its private DKIM key. This signature is then added to the email header.
The a=rsa-sha256 component in a DKIM signature header specifies the signing algorithm. RSA refers to the Rivest–Shamir–Adleman public-key cryptosystem, which is used for the encryption. SHA-256 (Secure Hash Algorithm 256) is the hashing function used to create a fixed-size string of characters, or a hash, from the email content. This combination is considered highly secure and is the current industry standard.
When you generate a DKIM key, you create a pair: a private key that remains secret on your sending server, and a public key that you publish in your Domain Name System (DNS) as a TXT record. Recipient mail servers use this public key to decrypt the signature and verify that the email truly originated from your domain and hasn't been tampered with in transit. Using rsa-sha256 ensures that your email authentication aligns with modern security practices.
The recommended key length for rsa-sha256 DKIM keys is 2048 bits. While 1024-bit keys are still functional, 2048-bit keys offer enhanced security and are increasingly becoming the standard accepted by major mailbox providers like google.com logoGoogle and yahoo.com logoYahoo. If you're wondering what the pros and cons are of different key lengths, consider using 2048-bit keys for optimal security and deliverability. This practice also aligns with recommendations from entities like the NCSC (National Cyber Security Centre) in the UK.

Generating DKIM keys with OpenSSL

The most common and secure way to generate an a=rsa-sha256 key pair is by using OpenSSL, a robust cryptographic toolkit available on most Linux/Unix systems and Windows (with appropriate installation). This method gives you full control over the key generation process and ensures that your private key remains secure and confidential. It’s a preferred approach for administrators who manage their own mail servers or want to avoid relying on third-party online key generators, which carry inherent security risks.
To generate the private and public keys, you'll use the openssl genrsa command. This command creates a new RSA private key of a specified bit length. Following this, you can extract the public key from the private key using openssl rsa. For a detailed walkthrough of this process, you can refer to Mailhardener's guide on creating DKIM records with OpenSSL.
OpenSSL commands to generate DKIM keysbash
openssl genrsa -out private.key 2048 openssl rsa -in private.key -pubout -out public.key
After executing these commands, you'll have two files: private.key, which contains your secret private key, and public.key, containing the public key. The private.key file must be kept extremely secure, as its compromise could allow malicious actors to impersonate your domain. The public.key is what you'll publish in your DNS. Remember to consider why DKIM key rotation is recommended for ongoing security, even after initial setup.
If you're looking for alternative methods or tools to generate DKIM public and private keys, there are various online generators and service-specific tools available, but OpenSSL offers the highest level of control and security for this sensitive process.

Publishing your DKIM record

Once you have your public.key file, the next step is to format it for your DNS TXT record. The public key needs to be extracted from the file and then typically placed within a DNS TXT record for your domain. This record tells recipient mail servers where to find your public key to verify the DKIM signature.
The public key content, often referred to as the 'p=' tag in the DKIM record, needs to be a continuous string without line breaks or extraneous characters. You will usually copy the content between -----BEGIN PUBLIC KEY----- and -----END PUBLIC KEY-----, then remove all line breaks. Some DNS providers might automatically handle the formatting, but it's crucial to be aware of this detail to prevent invalid RSA public key errors.
Example DKIM public key for DNS TXT record
v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDyQ5h/kQ3...YOUR_PUBLIC_KEY_CONTINUED...Gg44/GqjXwIDAQAB
You'll also need to choose a DKIM selector, which is a string (e.g., s1 or default) that allows you to publish multiple DKIM keys for a single domain. This is useful for key rotation or if you use different sending services. The selector forms part of the DNS record name, typically selector._domainkey.yourdomain.com. DKIM.org provides further technical specifications on constructing these records. Proper publication is essential for ensuring your emails are delivered and not marked as spam.

Key management and best practices

Managing your DKIM keys properly extends beyond just generating them. Regularly rotating your DKIM keys is a crucial security practice. While there isn't a strict rule on how often to rotate, changing them annually or semi-annually is a good strategy to mitigate risks associated with potential key compromises. This process involves generating a new key pair, publishing the new public key with a different selector, and updating your sending system to use the new private key.
Monitoring your DKIM alignment and deliverability is also vital. After publishing your DKIM record, use an email deliverability tester or DMARC monitoring tool to ensure your emails are correctly signed and validated. Issues like a DKIM body hash mismatch can occur if the email content is altered, even slightly, after signing, leading to validation failures and potentially impacting your sender reputation.

Best practices for DKIM key management

  1. Secure storage: Keep your private DKIM key on a secure server or system, limiting access only to authorized personnel.
  2. Key rotation: Implement a regular key rotation schedule (e.g., annually) to enhance security.
  3. DNS TTL: Set a reasonable TTL (Time To Live) for your DKIM DNS record to allow for quick updates during key rotation.
Proper DKIM implementation is a cornerstone of modern email security. By understanding the relationship between DKIM, SPF, and DMARC, you can build a robust email authentication framework that protects your domain from unauthorized use and improves email deliverability. Always verify your setup and troubleshoot any issues promptly to maintain trust with recipients and avoid your emails being blocklisted.

Views from the trenches

Best practices
Always generate DKIM keys on your own secure server using OpenSSL, not online tools.
Use a key length of 2048 bits for new DKIM implementations to ensure strong security.
Implement a key rotation schedule, such as yearly, to update your DKIM keys.
Utilize a unique selector for each DKIM key, especially when testing or using multiple senders.
Common pitfalls
Using 1024-bit DKIM keys, which are becoming less secure and might be phased out.
Relying on public online tools for key generation, risking private key exposure.
Incorrectly formatting the public key when publishing it in your DNS TXT record.
Not implementing key rotation, leaving your domain vulnerable to potential compromises.
Expert tips
If your DNS provider splits your DKIM public key due to character limits, make sure it is concatenated correctly in the DNS record.
Ensure your email sending software is configured to use the correct private key and selector.
Test your DKIM record immediately after publishing it to confirm proper validation.
For large organizations, consider using automated key management systems to simplify rotation.
Marketer view
Marketer from Email Geeks says that using an online DKIM generator can be quick, but there's a risk involved since you are trusting a third-party with your private key, which is a significant security concern.
2023-01-01 - Email Geeks
Expert view
Expert from Email Geeks says that OpenSSL is the recommended method for generating DKIM keys, as it provides full control over the key pair and eliminates third-party trust issues.
2023-01-02 - Email Geeks

Ensuring email authenticity with DKIM

Generating an a=rsa-sha256 key for DKIM is a critical step in setting up robust email authentication for your domain. By using tools like OpenSSL to generate a 2048-bit RSA key pair, you ensure that your emails are signed with a strong cryptographic signature, making them less susceptible to spoofing and increasing their chances of reaching recipient inboxes.
Remember, the security of your email ecosystem relies heavily on the careful handling of your private key and the accurate publication of your public key in your DNS. Regularly review your authentication setup and address any reported issues to maintain high email deliverability and protect your brand's reputation.
A strong DKIM configuration, alongside SPF and DMARC, forms the backbone of a reliable and trustworthy email sending infrastructure, crucial for avoiding your domain being blacklisted (or blocklisted) and ensuring your messages land where they belong: the inbox.

Frequently asked questions

DMARC monitoring

Start monitoring your DMARC reports today

Suped DMARC platform dashboard

What you'll get with Suped

Real-time DMARC report monitoring and analysis
Automated alerts for authentication failures
Clear recommendations to improve email deliverability
Protection against phishing and domain spoofing