DEV Community

Mind vs Drive
Mind vs Drive

Posted on

Your DKIM Key Decodes Fine but Still Fails: Count the Bytes Behind p=

You add a DKIM record, a checker says the key is invalid or the signatures don't verify, and you look at the record. It looks fine. It starts with MIIB, it's a long blob of Base64, and if you paste it into a decoder it decodes without complaint.

The problem is often that the key is incomplete. Base64 decoders are forgiving, so a key that lost its last chunk can still "decode" into fewer bytes than it should. This post shows how to read the p= value, which byte counts to expect, and how to spot a truncated key in about a minute.

What is inside p=

A DKIM key record is a DNS TXT record at selector._domainkey.example.com with tags like v=DKIM1; k=rsa; p=.... RFC 6376 defines p= as "Public-key data (base64; REQUIRED)", and k= defaults to rsa.

In practice, the RSA keys you get from OpenSSL and most providers are DER-encoded SubjectPublicKeyInfo structures. That's why they almost always start the same way. RFC 8463 even uses one in its own sample record:

test._domainkey.football.example.com. IN TXT (
 "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkHlOQoBTzWR"
 "iGs5V6NpP3idY6Wk08a5qhdR6wy5bdOKb2jLQiY/J16JYi0Qvx/byYzCNb3W91y3FutAC"
 "DfzwQ/BC/e/8uBsCR+yz1Lxj+PL6lHvqMKrM3rG4hstT5QjvHO9PzoxZyVYLzBfO2EeC3"
 "Ip3G+2kryOTIKT+l/K4w3QIDAQAB")
Enter fullscreen mode Exit fullscreen mode

Ed25519 keys (k=ed25519, RFC 8463) are different: p= is just the raw 32-byte public key, so the Base64 is 44 characters. The RFC notes that it "will generally fit in a single 255-byte TXT string".

Why keys get cut

A single string inside a TXT record can hold at most 255 characters (RFC 1035, section 3.3: a length octet followed by up to 255 characters). A 2048-bit RSA key is 392 Base64 characters on its own, and a full record like v=DKIM1; k=rsa; p= followed by the key comes to 410. So it must be split into several strings inside one record. RFC 6376, section 3.6.2.2, says: "Strings in a TXT RR MUST be concatenated together before use with no intervening whitespace."

Three common ways this goes wrong:

  1. A DNS panel quietly keeps only the first 255 characters of what you pasted.
  2. The key is split into two separate TXT records instead of two strings in one record. The RFC says that if there are multiple records in the RRset, "the results are undefined".
  3. You copy the record from dig output and paste the " " between the chunks into a decoder or a config file.

The trap: a truncated key still decodes

Take a 2048-bit key (392 characters) and keep only its first 255 characters. 255 isn't a multiple of 4, but lenient decoders just add padding:

// Node.js
Buffer.from(p.slice(0, 255), "base64").length   // 191 (no error)
Enter fullscreen mode Exit fullscreen mode
# Python is stricter by default
base64.b64decode(p[:255])         # binascii.Error: Incorrect padding
base64.b64decode(p[:255] + "=")   # 191 bytes
Enter fullscreen mode Exit fullscreen mode

So "it decodes" proves nothing. Count the bytes.

Quick reference: what a complete key looks like

These sizes are for RSA keys with the usual public exponent 65537, in SubjectPublicKeyInfo form:

Key Base64 chars Decoded bytes Base64 starts with Hex starts with
RSA 1024 216 162 MIGfMA0G 30 81 9F
RSA 2048 392 294 MIIBIjAN 30 82 01 22
Ed25519 (raw) 44 32 varies varies

Two built-in checks for RSA keys:

  • The header gives you the length. 30 82 01 22 means "a SEQUENCE of 0x0122 = 290 bytes follows". Add the 4 header bytes and you get 294. For 1024-bit keys, 30 81 9F means 159 + 3 = 162.
  • The tail. With exponent 65537, the key ends in the bytes 02 03 01 00 01, which shows up as IDAQAB at the end of the Base64. A key that doesn't end in IDAQAB has probably lost its tail.

Worked example: the RFC 8463 sample key

Join the four strings above with nothing in between. Then take everything after p=:

echo "$RECORD" | tr -d '" \n' | sed 's/.*p=//' | cut -d';' -f1 > p.txt
tr -d '\n' < p.txt | wc -c                        # 216
base64 -d p.txt | wc -c                           # 162
base64 -d p.txt | openssl pkey -pubin -inform DER -noout -text | head -1
# Public-Key: (1024 bit)
Enter fullscreen mode Exit fullscreen mode

Decoded, the first bytes are:

30 81 9F 30 0D 06 09 2A 86 48 86 F7 0D 01 01 01 05 00 03 81 8D 00 ...
Enter fullscreen mode Exit fullscreen mode

That's 30 81 9F (159 + 3 = 162 bytes ✓), then the rsaEncryption OID 2A 86 48 86 F7 0D 01 01 01, and the value ends in 02 03 01 00 01 (the Base64 ends in IDAQAB ✓). The key is complete.

Now repeat with a 2048-bit key cut at 255 characters. The decoder gives you 191 bytes and no error. But the header still says 30 82 01 22, a promise of 294 bytes. That 103-byte gap is your bug.

Doing the same check in a browser

If you don't have a shell handy, or you want to look at the bytes:

  1. Open https://base64decodetool.com/hex/ and click the Base64 to Hex tab.
  2. Paste only the value after p= into Base64 Input, with no quotes, no p=, and nothing between the chunks. (Line breaks and surrounding spaces are fine.)
  3. Read the first bytes in Hexadecimal Output, and check the counter above it. The output is space-separated, so bytes = (chars + 1) ÷ 3. A complete 2048-bit key shows 881 chars, which is 294 bytes.

Disclosure: this tool is from Mind vs Drive.

The tool runs in your browser. It removes whitespace at the start and end and adds missing = padding, so it is lenient on purpose. That's why you should look at the byte count, not at whether it "worked". The " " from dig output and a leading p= both cause an "Invalid character" error.

Limitations

  • This checks the structure, not the match. A complete key can still be the wrong key. To compare with your private key, run openssl rsa -in private.pem -pubout -outform DER | base64 -w0 and compare that string with p=.
  • Other key formats exist. RFC 6376's text names the PKCS#1 RSAPublicKey structure. If someone publishes that form, a 2048-bit key is 270 bytes (360 Base64 characters, starting MIIBCgKCAQEA), so the table above doesn't apply. Unusual exponents also change the lengths slightly.
  • DNS caching. After fixing a record, resolvers may serve the old one until its TTL expires.
  • The hex tool doesn't parse ASN.1. You read the header bytes yourself, as shown above.

TL;DR

  • p= is Base64 of a DER public key. A complete 2048-bit RSA key is 392 characters / 294 bytes, starts with MIIBIjAN and ends with IDAQAB.
  • TXT strings max out at 255 characters, so long keys must be split into strings inside one record, joined with no whitespace.
  • A truncated key often decodes without an error. Count the bytes and check the 30 82 01 22 header.

Sources (checked 4 Dec 2026): RFC 6376 sections 3.6.1 and 3.6.2.2 (https://www.rfc-editor.org/rfc/rfc6376.html), RFC 8463 sections 3, 4 and appendix A.2 (https://www.rfc-editor.org/rfc/rfc8463.html), RFC 1035 section 3.3 (https://www.rfc-editor.org/rfc/rfc1035.html). Byte counts were generated with OpenSSL 3.5.9 and checked with GNU coreutils base64, Node.js and Python.

Top comments (0)