How to encode and decode Base64
- Choose Encode or Decode at the top.
- Type or paste into the box — or drop a file. To encode an image or a PDF, drop it; to decode one, paste its Base64 and the file type is recognised for you.
- Set the options that match where the result is going: the URL-safe alphabet for a URL
or a JWT, no padding if the receiver wants none, a
data:prefix for HTML or CSS, or line breaks at 64 or 76 characters for PEM and e-mail. - Copy the result, or download it. A decoded file is saved with the right extension.
Why decoding fails, and how to tell which reason
Most Base64 problems are one of five things, and each looks different once you know what to look for. The decoder checks for all of them and says which one it found:
- A character that does not belong, reported with its position. A
%means the text was URL-encoded first:%3Dis=,%2Bis+,%2Fis/. A quotation mark or a space of another kind means it was copied out of JSON or a document. - An impossible length. Base64 turns 3 bytes into 4 characters, so a length that leaves a remainder of 1 after dividing by 4 cannot be right: a character went missing or the text was cut off.
- An
=in the middle. Padding only ends a string. Two encoded strings pasted together have padding in the middle. - Two alphabets in one string. Standard Base64 uses
+and/; the URL-safe form uses-and_. A real string uses one of them. - The wrong text encoding. The decode is fine but the letters are wrong. See the next section.
Things that are not errors are accepted and mentioned: line breaks every 64 or 76
characters, a missing = at the end, quotation marks around the whole value, a
data: prefix, and the URL-safe alphabet.
The same letter, two encodings
Base64 turns bytes into text, and a letter becomes bytes according to a text encoding.
The letter é is w6k= in UTF-8 and 6Q== in Latin-1 —
both checked against Python's base64 module. The browser's own
btoa function uses Latin-1, so it gives 6Q== and, for anything
past Latin-1 such as é世, refuses with an
InvalidCharacterError. This page encodes text as UTF-8, which is what
almost every modern system expects, and reads decoded bytes as UTF-8 too.
If you decode 6Q== as UTF-8 you get a single byte, 0xE9, which is not valid
UTF-8, so the page says the result is not readable as text and suggests Windows-1252 —
where 0xE9 is é. That is the usual explanation when a string from an older system
decodes into question marks or boxes.
Files, images and data URIs
Drop a file in Encode mode and its Base64 comes out, optionally with a prefix such as
data:image/png;base64, that browsers accept in an <img>
or a CSS url(). Going the other way, the first bytes of the decoded data are
compared with the signatures of PNG, JPEG, GIF, WebP, BMP, PDF, ZIP, gzip, 7-Zip and ICO,
so a pasted image is shown and downloaded as an image rather than as a heap of characters.
Base64 makes data about a third larger: 1,000,000 bytes always become 1,333,336
characters with padding. That is why inlining a large image in a page is rarely a saving.
Tokens, encoded twice, and other special cases
A JSON Web Token is three URL-safe Base64 pieces joined by dots. Paste one, with or
without Bearer in front, and the header and payload are shown as formatted
JSON. The signature is not checked — decoding a token says what it claims,
not whether the claim is true. If a decoded result is itself Base64 — a value that
was encoded twice — the page says so and offers to decode it again.
How it was checked
The encoder and decoder are written out here rather than built on btoa and
atob, so the output was compared with Python's base64 module: 47
input lengths from 0 to over 100,000 bytes in the standard alphabet, the URL-safe
alphabet, without padding, and wrapped at 64 and 76 columns, all identical; then 80
random strings written in six different ways — padded, unpadded, URL-safe,
wrapped, split by spaces and tabs — decoded back to the same bytes. A 30 MiB
block encodes in about a fifth of a second in Chrome on the machine these pages are built
on, and decodes in about a second.
Base64 is not encryption
There is no key and no secret: anyone can decode Base64, so it does not protect a password, a token or a file. Its job is to let bytes travel through places that only carry text — e-mail bodies, JSON, URLs, HTML. At least one widely used Base64 page sends what you type to its own server unless you switch on an in-browser mode; here the work is done in the page and nothing is uploaded, which matters when the string in front of you is a credential.
Related tools
To turn an image into a Base64 data: string in one step, use
image to Base64. To tidy the JSON that a decoded string
turns out to contain, paste it into the JSON formatter. To
check the integrity of a file you are about to encode, the
hash generator gives its SHA-256.