Base64 ends with = or == because it encodes 3 bytes at a time into 4 characters, and the last group is often short. That base64 padding fills the final 4-character block: == means the last group held 1 byte, = means it held 2 bytes, and no padding means the input length was a multiple of 3.
How Base64 turns 3 bytes into 4 characters
Base64 works in 24-bit blocks, because 24 is the smallest number of bits that splits evenly into 8-bit bytes and 6-bit characters. That is 3 bytes in, 4 characters out. Each character picks one of 64 symbols, which is exactly what 6 bits can address.
RFC 4648, section 4, describes the process: take the input in 24-bit groups, split each group into four 6-bit values, and map each value to a letter, digit, + or /. The spec also reserves a 65th character, =, which "is used to signify a special processing function". That function is padding.
If the input length is a multiple of 3, every block is full. The trouble starts when it isn't.
Where the = signs come from
The = signs come from the final, incomplete block. RFC 4648 lists only three possible endings:
- A full 24 bits. No padding.
- Exactly 8 bits (1 byte). Zero bits go on the right to make two 6-bit characters, then "two "=" padding characters" fill the block.
- 16 bits, or 2 bytes. Zero bits make three characters, then one
=.
Take a single byte, the letter f. Its 8 bits fill one 6-bit character and spill 2 bits into a second. The encoder pads that second character with zero bits, giving Z and g, and two = signs fill the last two slots. So f encodes to Zg==, and the g holds two real bits plus four zeros.
This is why === never appears in valid Base64. One leftover byte already needs two real characters, so at most two slots are left to pad.
Predict the padding from the length, and the length from the padding
Base64 padding is predictable in both directions: the input length decides it, and a padded string tells you the exact byte count. One write-up states the rule: a string ends with == "if and only if the number of bytes it encodes, mod 3, equals 1".
The RFC's own test vectors show the full cycle:
| Input | Bytes | Bytes mod 3 | Output | Padding |
|---|---|---|---|---|
"" |
0 | 0 | "" |
none |
f |
1 | 1 | Zg== |
== |
fo |
2 | 2 | Zm8= |
= |
foo |
3 | 0 | Zm9v |
none |
foob |
4 | 1 | Zm9vYg== |
== |
fooba |
5 | 2 | Zm9vYmE= |
= |
foobar |
6 | 0 | Zm9vYmFy |
none |
To check a payload, run it backwards: divide the character count by 4, multiply by 3, and subtract one byte per =. Zm9vYmE= is 8 characters: 8 / 4 × 3 = 6, minus 1 for the single =, gives 5 bytes, which matches fooba.
For the size side of the same math, see why Base64 makes data bigger.
Why some Base64 has no padding at all
Base64 with no padding is usually Base64URL, the URL-safe variant, where dropping = is allowed. RFC 4648 says that when "the data length is known implicitly", the padding can be skipped. A = also has a meaning in URLs and query strings, so URL-bound tokens tend to lose it.
JWTs make this mandatory. RFC 7515 defines Base64url encoding for JSON Web Signatures "with all trailing '=' characters omitted". That's why a JWT header like eyJhbGciOiJIUzI1NiJ9 has no = at its end even when its length isn't a multiple of 4. The Base64 vs Base64URL post covers the other two character swaps.
My take: keep the padding unless a spec tells you to drop it. It costs at most two characters, and it spares the next person a decode error.
What decoders do when the = is missing
Decoders split into forgiving and strict, and the same unpadded string can work in one and fail in the other.
Browsers are forgiving. atob() follows the forgiving-base64 decode algorithm. It strips trailing = signs and then only fails if the length "divides by 4 leaving a remainder of 1". So atob("Zg") and atob("Zg==") both return f. A 5-character string still fails, because no byte count produces that length. If you're hitting that error, this guide to atob failures walks through each cause.
Python's standard decoder is strict. base64.b64decode raises binascii.Error when the input "is incorrectly padded". The usual fix is to add the missing signs before decoding:
import base64
s = "Zm9vYmE" # padding stripped
s += "=" * (-len(s) % 4) # back to "Zm9vYmE="
base64.b64decode(s) # b'fooba'
Python 3.15 relaxed this for the URL-safe function only: urlsafe_b64decode no longer requires padding by default. The full set of Python cases is in how to decode Base64 in Python.
An = in the middle means two strings were joined
A = anywhere but the end means two separately encoded Base64 strings were glued together. The stray = marks where the first one stopped.
RFC 4648 section 3.2 says padding matters for exactly this case. With padding, you can split at the = and decode both halves. Without it, a Stack Overflow example shows the decoder reading the joined string as one stream and returning garbage, because the boundary is gone.
One limitation: padding only tells you the byte count of the last group. It doesn't prove the data is intact or that the string is even Base64. A truncated string can still end on a clean 4-character boundary and decode without any error.
To try this on your own data, the Base64 decoder decodes standard and URL-safe input in the browser.
FAQ
What does == mean at the end of Base64?
It means the last group held only one byte of input. The encoder wrote two characters for that byte and added two = signs to complete the 4-character block.
Can Base64 end with three = signs?
No. A final group of one byte gives ==, two bytes give =, and three bytes give no padding. === never appears in valid Base64.
Can I remove the = from Base64?
You can if the decoder accepts unpadded input or you add it back before decoding. JWTs and many URL tokens drop it on purpose. Python's b64decode still raises an error without it.
Is the = sign part of the encoded data?
No. It carries no data bits. It only marks how many bytes were in the final group, which tells the decoder how many bytes to output.
Why is there = in the middle of my Base64 string?
Two padded Base64 strings were probably joined together. Split them at the padding and decode each part on its own.