You paste a peer's public key into wg0.conf or run wg set, and wg stops with:
Key is not the correct length or format: `...'
The key looks fine. It's about the right length, it's Base64, and an online decoder or Python's base64 module turns it into 32 bytes without complaining. So why does wg refuse it?
wg is stricter than a general-purpose Base64 decoder. This post walks through exactly what the reference wg tool checks, shows five broken variants of one illustrative key, and gives a quick way to see which kind of damage you're dealing with.
What a WireGuard key actually is
WireGuard keys are Curve25519 keys, 32 raw bytes. The official quick start says WireGuard "requires base64-encoded public and private keys".
32 bytes encode to Base64 like this:
- 30 bytes → 10 full groups of 4 characters = 40 characters
- the last 2 bytes → 3 characters + one
=pad - total: 44 characters, always ending in exactly one
=
The last 2 bytes are 16 bits, but 3 Base64 characters carry 18 bits. So the final 2 bits of the 43rd character have to be zero in a canonical encoding. That detail matters later.
What wg checks
In wireguard-tools src/encoding.c (master branch, checked 3 Dec 2026; the file was last changed 21 Mar 2026), key_from_base64() rejects the key unless:
- the string length is exactly 44, and character 44 is
=; - every character before it is in the standard alphabet
A–Z a–z 0–9 + /; - the leftover bits in the last group are zero (
ret |= ... | (val & 0xff)).
parse_key() in src/config.c prints the error message when any of these checks fails.
Two useful details from the same file:
- When
wgreads a config file, it strips all whitespace from each line before parsing, and#starts a comment. So stray spaces insidePublicKey = ...are usually not your problem. -
wg set wg0 peer <key>passes the key argument straight toparse_key(), so a trailing space inside quotes will fail. Key files (private-key,preshared-key) may have trailing whitespace or a newline. Any other trailing character givesFound trailing character in key file.
Worked example: one key, five ways to break it
Illustrative only. We derived this public key from a throwaway private key (SHA-256 of a fixed test phrase). It isn't used anywhere, and public keys aren't secret anyway:
zIKziHqJs8nN/jvyBFj2LX/uLpKLfnJIxBCcX5qyc0I=
Hex (32 bytes): cc82b3887a89b3c9cdfe3bf20458f62d7fee2e928b7e7248c4109c5f9ab27342
We compiled encoding.c from wireguard-tools unchanged, wrapped key_from_base64() in a tiny test harness, and fed it each variant. We compared the result with Python's base64.b64decode() and GNU base64 -d (coreutils 9.7):
| # | Variant | Length |
wg (key_from_base64) |
Python b64decode
|
GNU base64 -d
|
|---|---|---|---|---|---|
| 0 | Original | 44 | accepted | 32 bytes | 32 bytes |
| 1 |
= dropped (...c0I) |
43 | rejected | error: Incorrect padding | 32 bytes, no error |
| 2 | URL-safe (/ → _) |
44 | rejected | error | invalid input |
| 3 | JSON-escaped (\/) |
46 | rejected | 32 bytes (backslashes silently discarded) | 32 bytes, no error |
| 4 | Last char before = changed I → J
|
44 | rejected | 32 bytes, identical to #0 |
invalid input (after printing 32 bytes) |
| 5 | Hex pasted instead of Base64 | 64 | rejected | 48 bytes of garbage | 48 bytes |
Where each one usually comes from:
-
Missing
=: a padding-stripping helper, a URL-safe library, or a copy that missed the last character. -
URL-safe alphabet: the key passed through code that uses Base64URL (
-and_). WireGuard wants standard Base64. -
\/: you copied the key out of raw JSON. Some serializers (PHP'sjson_encodeby default, for example) escape/as\/. Parse the JSON instead of copying the raw text. -
Non-canonical last character: the sneaky one. A one-character typo near the end still decodes to the same 32 bytes in lenient decoders, because the changed bits are the discarded padding bits.
wgrejects it. With the first 42 characters fixed, only 16 characters are accepted in position 43:A E I M Q U Y c g k o s w 0 4 8(the ones whose 6-bit value is a multiple of 4). We checked this by trying all 64 characters against the harness. - Hex: some dashboards show keys as hex. Convert the 64 hex characters back to Base64 first.
Quick local checks (keep private keys off websites)
Public keys are safe to inspect anywhere. For private and preshared keys, stay in your terminal:
# length must be 44 (no newline counted)
printf %s "$KEY" | wc -c
# must decode to exactly 32 bytes
printf %s "$KEY" | base64 -d | wc -c
# round-trip: a canonical key re-encodes to itself
python3 -c 'import base64,sys;k=sys.argv[1];print(base64.b64encode(base64.b64decode(k,validate=True)).decode()==k)' "$KEY"
Each check catches different damage. The length check catches #1, #3 and #5, because GNU base64 -d happily returns 32 bytes for #1 and #3. The round-trip line catches #4: Python accepts the J version but re-encodes it as ...c0I=, so it prints False. With validate=True it raises an error on #2 and #3. On a machine with wireguard-tools installed, printf %s "$PRIV" | wg pubkey is the ultimate test for a private key.
Using the Base64 Validator for a public key
The Base64 Validator runs in your browser. It's handy for a public key copied from a ticket or chat:
- Paste the key into Input String.
- Check Character Length =
44 charsand Decoded Byte Size =32 bytes (32 B). - Check Variant Type =
Standard Base64(it showsURL-Safe (Base64URL)for variant 2) and Padding Status =Valid (1 pad char)(variant 1 showsUnpadded).
What it tells you for the variants above (we checked by running the page's own validation code): #3 fails with Contains non-Base64 characters: \, and #5 shows 48 bytes. Like most decoders, it is lenient: #1, #2 and #4 still show PASSED (VALID) with 32 bytes. So treat "PASSED" as "it's Base64", not "WireGuard will accept it". Then check the three fields above plus the 43rd-character rule. The Hex ↔ Base64 converter on the same site turns a 64-character hex key back into Base64 (choose Hex to Base64).
Checklist
- [ ] Exactly 44 characters, ending in one
= - [ ] Only
A–Z a–z 0–9 + /before the=(no-,_,\) - [ ] 43rd character is one of
AEIMQUYcgkosw048 - [ ] Decodes to 32 bytes and re-encodes to the same string
- [ ] Copied from parsed output, not raw JSON
Validator: https://base64decodetool.com/validator/
Disclosure: this tool is from Mind vs Drive, the team behind this account.
— The Mind vs Drive team
Top comments (0)