Base64 Padding Error — Incorrect Padding Fix
💡A Base64 incorrect padding error means the string length is not a multiple of 4. Base64 encodes 3 bytes into 4 characters, and = padding characters are appended to fill the last group. Add the missing padding with this formula: (4 - length % 4) % 4 equals signs at the end.
Why Padding Exists
Base64 works in groups of 3 input bytes, converting each group to 4 characters. If the input is not a multiple of 3 bytes, the last group is shorter and = characters are appended so the total length is always a multiple of 4. Decoders use these padding characters to determine how many real bytes are in the last group. Removing padding without adjusting the decoder causes an incorrect padding error.
Fixes with Code Examples
Adding missing padding
❌ Wrong
atob("aGVsbG8")
// InvalidCharacterError: Invalid character
// String length 7 — not a multiple of 4✅ Fixed
function addPadding(s) {
return s + "=".repeat((4 - s.length % 4) % 4);
}
atob(addPadding("aGVsbG8"))
// → "hello"Base64 strings must be padded to a multiple of 4. Add = characters until the length is divisible by 4.
URL-safe Base64 without padding
❌ Wrong
// JWT segments use URL-safe Base64 without padding
atob("aGVsbG8-d29ybGQ")
// InvalidCharacterError✅ Fixed
function decodeJwtSegment(seg) {
const padded = seg.replace(/-/g, "+").replace(/_/g, "/");
const pad = padded + "=".repeat((4 - padded.length % 4) % 4);
return atob(pad);
}JWT uses URL-safe Base64 (no padding, - instead of +, _ instead of /). Convert and pad before decoding.
When Padding Can Be Omitted
Some contexts deliberately omit padding to keep strings shorter. JWT header and payload segments use unpadded URL-safe Base64. If you are building a system that controls both encoder and decoder, you can omit padding — but you must add it back before passing the string to a standard library decoder like atob() or Python's base64.b64decode().
Decode Base64 Without Padding Issues
Paste your Base64 string — padded or not — and decode it instantly. The tool handles missing padding and URL-safe encoding automatically.
Related Guides
Frequently Asked Questions
Why does Base64 need padding?
Base64 encodes 3 bytes into 4 characters. If the input is not a multiple of 3 bytes, = characters are added to make the output length a multiple of 4. Decoders use padding to determine the exact byte count.
Can I use Base64 without padding?
Some implementations allow omitting padding (JWT, URL-safe Base64). If your decoder requires it, add = characters back: padding = (4 - length % 4) % 4.
How many = characters does Base64 padding need?
Zero, one, or two. Never three — a valid Base64 string always needs 0, 1, or 2 padding characters. The formula is: (4 - length % 4) % 4.
All tools run in your browser. Your data never leaves your device.