Why a Base64 value can have valid characters and still be rejected
A Base64 string can use only valid characters and the right length and still be rejected. In the final group, the leftover bits have to be zero, and a strict decoder checks them.
Where pad bits come from
Base64 turns every three bytes into four characters, each carrying six bits. When the data ends after one or two bytes, the last group is incomplete. One byte gives eight bits, which fill one full six-bit character and two bits of a second one, so the second character is padded with four zero bits. Two bytes give sixteen bits, which fill two characters plus four bits of a third, so the third has two zero bits. Equals signs then fill the output to a multiple of four characters. Those unused low bits are the pad bits, and RFC 4648 says an encoder sets them to zero.
Decoding Zg==
The first case decodes Zg==. The character Z has the six-bit value 25, which is 011001, and g has the value 32, which is 100000. Joined, the twelve bits are 011001100000. The first eight make 01100110, the byte 0x66, which is the letter f. The last four bits, 0000, are unused and are all zero, so the encoding is canonical. The tool returns f. This is the same string the encoder would produce for the one-letter text f, which the third case confirms. Reading the table of the Base64 alphabet makes this easy to repeat by hand: A to Z are 0 to 25, a to z are 26 to 51, the digits 0 to 9 are 52 to 61, plus is 62 and slash is 63. With that table and a calculator that shows binary, you can verify any short value without running anything.
Why Zh== is rejected
The second case changes only the second character from g to h. The value of h is 33, or 100001. The twelve bits become 011001100001. The first byte is still 01100110, the letter f, so a lenient decoder would give f and carry on. The last four bits are now 0001, which is not zero. The length is the same, the alphabet is valid and the equals signs are in the right place, yet this is not what an encoder would have written. This tool decodes the value, re-encodes the bytes and compares, so the mismatch exposes the change and the error mentions pad bits.
Why strictness is useful
Accepting several encodings of one message has a cost. If Zg== and Zh== both decode to f, then two different strings stand for the same data, which can defeat a comparison, a cache key, a signature over the encoded text or a deduplication step. Security-sensitive code has been caught by this kind of ambiguity. Requiring canonical form means the encoded text is a unique representation of its bytes. RFC 4648 describes rejecting non-zero pad bits as an option for implementations that want that property, and this tool chooses it.
How such a value arises
Non-zero pad bits rarely come from a correct encoder. A hand edit that changes one character in the last position, a copy that dropped a character and then another source supplied a substitute, or an encoder with a bug can all create one. Some tools also decode text written with a different alphabet, and a character swapped by mistake may still look plausible. The decoder cannot tell which byte the writer meant. All it can say is that the value is not the canonical spelling of any byte sequence, which is a statement about the representation.
Using the encoder as a control
The third case encodes the one-letter text f and returns Zg==. Use this method for any suspect value: decode what you can from a trusted source, encode the expected text with the encoder, and compare the two strings character by character. For a short value, the difference is easy to see, as here where only the second character differs. For a longer value, the difference will be in the last group, since pad bits only exist there. A mismatch earlier in the string indicates different data, not a pad bit problem.
What not to do with a rejected value
It is tempting to fix the last character until the decoder accepts it. Doing so produces a value that decodes, but not necessarily the message the sender had in mind, and a guessed correction can hide the fact that part of the data was damaged. If a single character was altered, there is no way to know which one from the result alone. Ask for the original text again, or recover it from the system that created it, then compare against an encoder. When the data is a token, key or checksum, an altered character is a reason to treat the value as untrusted until you have a verified copy, because the damage may extend beyond the pad bits. Writing down where the rejected value came from helps whoever has to find the source copy.
A second example with two characters of data
The same arithmetic works for two bytes, where the last group has three characters and one equals sign. The text fo has the bytes 66 and 6F, which are 01100110 and 01101111. Split into six-bit pieces, they are 011001, 100110, 1111 and then two padding zeros, giving Z, m and 8 and the string Zm8=. The final character 8 stands for 111100, and the two low bits are zero. A noncanonical neighbor such as Zm9= would carry the bits 111101 in the last position, with a non-zero low bit, and would be rejected for the same reason, even though it decodes to fo under a lenient rule. Only one final character is canonical for each possible pair of data bits.
Final group only, standard Base64
The rule applies to the final group of standard Base64 only. A string whose length is a multiple of four without padding has no spare bits, and an unpadded URL-safe value is handled by the Base64url tool with its own rules. The check concerns representation. It does not say anything about whether the decoded bytes are valid UTF-8, which is a separate step covered in another guide. Passing the pad-bit test only means the string is the canonical encoding of some byte sequence. For the one-byte case there are sixteen second characters that decode to the same byte, and only one of them is canonical. For the two-byte case there are four, and again only one. Everything before the final character is fully constrained, since each of its six bits carries data.
Reproduce the checks
Canonical representation
Open the Base64 Encoder Decoder and enter:
- Calculation mode
decode- Text or encoded input
Zg==
Expected output:
Text
fThe final unused bits are zero.
Same shape, noncanonical final bits
Open the Base64 Encoder Decoder and enter:
- Calculation mode
decode- Text or encoded input
Zh==
Expected rejection:
pad bitsThe alphabet and padding alone do not establish canonical encoding.
Regenerate the known control
Open the Base64 Encoder Decoder and enter:
- Calculation mode
encode- Text or encoded input
f
Expected output:
Text
Zg==The encoder supplies a reproducible canonical spelling.
References and implementation scope
The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.
Sources
- Primary definition and convention reference ↗ · 2026-10-08