How to Fix Base64 Decoding Errors
A Base64 decode fails for four reasons. The string uses the URL-safe alphabet, where minus and underscore replace plus and slash. The padding equals signs were stripped. Line breaks or spaces were introduced by copying. Or the value was encoded twice, in which case decoding once returns more Base64 rather than an error. Check the alphabet first.
Base64 Encoder and Decoder
Encode text to Base64 or decode it back, including URL-safe.
Base64 has two alphabets and inconsistent opinions about padding, which is why a string produced by one system so often fails in another. Neither variation is a mistake; they are both legitimate, and a decoder that only understands one will reject the other.
The length is the quickest diagnostic. Standard Base64 always has a length that is a multiple of four, because every three input bytes become exactly four output characters. A length that is not a multiple of four means padding was removed.
Step by step
-
Look for minus and underscore
If the string contains - or _ it is Base64url, the URL-safe variant defined for use in URLs and JWTs. Replace - with + and _ with / before decoding, or use a decoder that accepts both alphabets.
-
Check the length against a multiple of four
Divide the length by four. A remainder of 2 means two = signs were stripped; a remainder of 3 means one was. Append them and decode again. A remainder of 1 is not valid Base64 at all and means characters are missing.
-
Strip whitespace and line breaks
Copying a long value out of a terminal or an email often introduces newlines. Some decoders tolerate them and some do not. Removing all whitespace is always safe, because whitespace is never meaningful inside a Base64 payload.
-
Decode once and look at the result
If decoding succeeds but the output is still Base64-looking text, the value was encoded twice. This happens when a system encodes a payload that a framework had already encoded. Decode again rather than treating the first result as corrupt.
-
Confirm the result is the type you expected
A successful decode that produces unreadable bytes is not necessarily a failure. Binary content — an image, a compressed archive, a protobuf message — decodes correctly and simply is not text. Check the first few bytes for a file signature before assuming it is broken.
Example
The same value in both alphabets. The URL-safe form has no padding and uses _ where the standard form uses /.
Base64url (as found in a JWT)
eyJhbGciOiJIUzI1NiJ9
Standard Base64, padded
eyJhbGciOiJIUzI1NiJ9
decodes to: {"alg":"HS256"}
Frequently asked questions
Why does my Base64 string fail to decode?
Are the equals signs at the end required?
What is the difference between Base64 and Base64url?
Why is my decoded output unreadable?
Is Base64 a form of encryption?
Tools used in this guide
All tools →- Base64 Encoder and Decoder Encode text to Base64 or decode it back, including URL-safe. In your browser
- URL Encoder and Decoder Percent-encode text for URLs, or decode it back. In your browser
- Unicode Escape and Unescape Convert characters to \uXXXX escapes and back. In your browser
- Data URI Encoder Turn an SVG or snippet into an embeddable data: URI. In your browser
Related guides
All guides →- Base64 vs Encryption Base64 is an encoding, not a cipher. What it is actually for, and why it protects nothing.
- How to Decode a JWT Read the header and payload of a JSON Web Token, check its expiry, and understand why decoding is not verifying.
- UTF-8 vs UTF-16 Two encodings of the same character set, and why string lengths disagree between languages.
Last reviewed .