How-toMath & conversion4 min read

Base64 Decode Errors and Padding: Why It Fails and How to Fix It

The most common reasons a Base64 string will not decode, and a checklist to fix each.

SH

Founder, CalculatePilot

On this pageexpand_more
Fixing Base64 decode errors and padding
lightbulb

Short answer

A Base64 decode usually fails because of characters outside the alphabet, a bad length, stripped = padding, or mixing the URL-safe (- and _) and standard (+ and /) alphabets. Pad the length to a multiple of 4 and use the matching alphabet.

1. Checklist

SymptomLikely causeFix
Invalid characterSpace, quote or line break inside the stringRemove whitespace and quotes
Invalid lengthTruncated copy; 1 leftover characterRe-copy the full string
Missing padding= stripped (common in JWTs and URLs)Add = until length is a multiple of 4
Garbled outputURL-safe string decoded as standard or the reverseSwitch alphabet: - and _ vs + and /
Gibberish textData is binary, such as an imageDecode as hex bytes, not text

2. Padding rule

  • Length mod 4 = 0: no padding needed.
  • Length mod 4 = 2: add ==. Example: TQ is M, so use TQ==.
  • Length mod 4 = 3: add =. Example: TWE becomes TWE=.
  • Length mod 4 = 1: not a valid Base64 length.

3. Example

SGVsbG8 is 7 characters long (7 mod 4 = 3), so add one =: SGVsbG8= decodes to Hello.

Use the Base64 tool with the URL-safe and padding options to test. Background: how Base64 works.

4. Frequently asked questions

Why does my Base64 string fail to decode?expand_more

Common causes are invalid characters, a bad length, missing padding or the wrong alphabet.

Do I always need = padding?expand_more

Standard Base64 includes it. Some systems such as JWTs omit it, so add = to a multiple of 4 before decoding.

What length is invalid?expand_more

A length that leaves exactly 1 character in the last group of 4 can never be valid.

Why do I get gibberish after decoding?expand_more

The data is probably binary, such as an image, not UTF-8 text.

5. Sources

Spotted a mistake or a number that looks off?

We fix errors quickly and note the change.