Blog

Base64 Padding Errors: Why They Happen and How to Fix Them

A practical guide to the equals signs at the end of Base64 strings, incorrect padding errors, and safe repair rules.

The short answer

Base64 works in groups of four characters. A final group may need one or two equals signs so the decoder can rebuild the last bytes. When a copied value loses those signs, many decoders show an incorrect padding error.

Read the length before you edit

Count the characters after you remove spaces and line breaks. If the length modulo 4 equals 0, the string needs no extra padding. If it equals 2, add ==. If it equals 3, add =. If it equals 1, stop: one Base64 character cannot hold a full byte, so the value has missing data or a wrong alphabet.

Check the alphabet too

Padding fixes length only. Standard Base64 uses + and /. Base64URL uses - and _. A JWT segment, route token, or filename-safe value may look broken in a standard decoder until you switch the alphabet. Change - to + and _ to / only when the surrounding format says it uses Base64URL.

Do not hide corruption

A decoder can ignore whitespace in some formats, but RFC 4648 tells general decoders to reject characters outside the chosen alphabet unless a protocol says otherwise. That rule protects you from silent data changes. Repair padding when the length rule proves it, but do not delete random characters to make an error disappear.

A safe checklist

First, copy the whole value again from the source. Second, remove wrapping whitespace. Third, identify standard Base64 or Base64URL from context. Fourth, apply the modulo 4 padding rule. Last, decode and confirm the output type you expected: text, JSON, image bytes, or another file format.

Technical sources