Base64 Encoder & Decoder

Published Updated

Encode or decode Base64 for text or a file, with UTF-8 support and an optional data: prefix.

Which Base64 forms are accepted

Section 4 of RFC 4648 defines the standard alphabet and section 5 the URL-safe one. This tool uses the first.

InputText DecodeFile Decode
Standard alphabet (A-Z a-z 0-9 + /)YesYes
URL-safe alphabet (- and _)NoYes
Missing = paddingYesYes
Line breaks or spaces insideYesYes
data:image/png;base64, prefixNoYes

A few exact results

The five letters Hello become SGVsbG8=: 5 bytes make 8 characters, and the final = pads the last group. Text with accents or other scripts is first converted to UTF-8, so the word café becomes Y2Fmw6k=. Encoded as Latin-1 it would be Y2Fm6Q==, which this tool never writes. Any Unicode text is handled the same way.

File encoding reads the whole file into memory and prints it as one unbroken line, so a 3,000,000-byte file produces exactly 4,000,000 characters. Tick the prefix box to get data:<type>;base64, in front, using the browser's file type or application/octet-stream when it has none.

Using the Base64 tool

  1. Step 1

    Choose Text or File, Encode or Decode

    Four combinations are available. Text decoding takes pasted Base64, and file decoding turns Base64 back into a download.

  2. Step 2

    Enter or load your data

    Type or paste text, or add a file. For a file, tick the box if you want the data: prefix in front.

  3. Step 3

    Process, then copy or download

    Press Process. Copy the output, or download the rebuilt file.

Checks the file decoder makes

When the decoded type is an image, the tool checks it before offering a download. A string cut short in the middle still decodes to bytes, so the tool first checks the ending of the bytes: a PNG must reach its IEND chunk, a JPEG must end with FF D9, a GIF with 3B and a WebP must have the size its header declares. A cut-off file of those types is refused with a message to copy the whole string again. Our check ran in Node on whole files and on cuts at 99, 90, 50 and 10 percent of PNG, JPEG, GIF and WebP (20 of 20 refused, whole files accepted), not in Firefox or WebKit, but the check reads bytes, so it does not depend on the browser. A JPEG with extra non-zero bytes after its end marker would be refused by this check (we did not test such a file). Other file types are written out as they are, so a damaged PDF or ZIP will still download.

For tokens, use JWT Decoder, and to go from picture to string directly use Image to Base64.

Text mode and file mode, side by side

We ran the tool's own decoding code (the functions in its component and lib/base64Image.ts) on the same strings in Chromium 153 (Playwright build, not Google Chrome), Firefox 155.0 and Playwright's WebKit build (not the Safari app). All three gave the same answers on every row below.

You pasteText decodeFile decode
SGVsbG8=Hello5 bytes
SGVsbG8 (no padding)Hello5 bytes
SGVs, a line break, bG8=Hello5 bytes
Pz8-Pg (URL-safe form of the bytes 3F 3F 3E 3E)Error4 bytes
data:text/plain;base64,SGVsbG8=Error5 bytes
//4= (the bytes FF FE, which are not UTF-8)Error2 bytes
SGVsb (5 characters, one too many for a group)ErrorRejected
Hello, world!ErrorRejected

The row with //4= is the one to know about. Text mode reads the bytes as UTF-8, fails, and says that the string is valid Base64 but the bytes are not UTF-8 text, pointing to File mode. File mode returns the two bytes.

Encoding went the other way without surprises: café gave Y2Fmw6k=, the Greek word καλημέρα gave zrrOsc67zrfOvM6tz4HOsQ== and the emoji 😀 gave 8J+YgA==. A lone surrogate half (the character U+D800 on its own) failed to encode, and the tool shows its general error message for it.

Size, speed and truncated pictures

We encoded and decoded random bytes with the file code path (FileReader for encoding, the tool's decoder for decoding) and checked that the SHA-256 of the result matched the input. In Chromium 153, 1,000,000 bytes became 1,333,336 characters in 3 ms and decoded in 10 ms. For 10,000,000 bytes it was 13,333,336 characters, 19 ms and 81 ms. For 50,000,000 bytes it was 66,666,668 characters, 72 ms and 408 ms. At 50 MB the decode took 196 ms in WebKit and 651 ms in Firefox. These are single runs and leave out showing the text in the page, which we did not time.

The browsers alone do not catch every cut picture. Asked only to decode 8 cuts per browser of a 400 x 300 PNG and a JPEG at 99, 90, 50 and 10 percent, Chromium 153 rejected all 7 with a valid length, while WebKit 26.6 accepted all 5 and Firefox 155 all 7. The other cuts ended on a length no Base64 string can have, which the decoder refused first. The byte check described above refused every such cut in Node.

Spaces, stray padding and spare bits in Text and File mode

The same strings went through Text decode and File decode of the real tool, measured in Google Chrome 157, Firefox 155 and the WebKit build; the three gave the same result in every row. Error stands for This doesn't look like valid Base64 text, and Rejected for the same message with data in place of text.

You pasteText decodeFile decode
SGVsbG9= (one letter off from SGVsbG8=)Hello5 bytes, Hello
YQ or YRa1 byte, a
SGVsbG8== (one = too many)Error5 bytes, Hello
SGVs=bG8 (= in the middle)ErrorRejected
SGVs, a tab or form feed, bG8=Hello5 bytes, Hello
SGVs, a no-break space (U+00A0), bG8=Error5 bytes, Hello
SGVsbG8= with a no-break space after itHello5 bytes, Hello
SGVs, an ideographic space (U+3000), bG8=Error5 bytes, Hello
SGVs, a zero-width space (U+200B), bG8=ErrorRejected
A byte order mark (U+FEFF), then SGVsbG8=Hello5 bytes, Hello

Text mode trims the ends of the string and hands it to the browser's atob, which follows the WHATWG forgiving-base64 decode: inside the string it skips only ASCII whitespace (tab, line feed, form feed, carriage return and space), and it drops the spare bits of the last character, which is why YQ and YR both give a. File mode also deletes Unicode spaces such as the no-break and ideographic spaces, and drops = signs at the end before decoding, but a zero-width space is not a space to it. RFC 4648, section 3.5 says conforming encoders MUST set those spare bits to zero and that decoders MAY reject a string where they are not, so a stricter tool may refuse SGVsbG9=.

Encoding keeps every character it is given. Hello followed by a line break gives SGVsbG8K, not SGVsbG8=, and Hello with a space at each end gives IEhlbGxvIA==. café with a single é gives Y2Fmw6k=, and with e plus a combining accent Y2FmZcyB. In File mode the bytes of the file are encoded as they are: a file holding a, a carriage return, a line feed and b gave YQ0KYg==.

Base64 questions

Why does my JWT or URL-safe string fail to decode?

In Text mode only standard Base64 works: letters, digits, plus and slash. URL-safe Base64 uses minus and underscore instead, as in JWT segments, and those characters make the text decode fail. File mode with Decode does accept them, so use that to get the bytes out, or open the token in the JWT Decoder.

Why does it say my text is not valid Base64 when it looks fine?

Text decoding expects the bytes to be UTF-8. If the text is valid Base64 but holds other bytes, such as an image or a Latin-1 file, the message says that: it is valid Base64 but not UTF-8 text, and to use File mode. A string that is not valid Base64 gets the invalid-Base64 message. Use File mode with Decode and download the result as a file.

Can I paste a line-wrapped string or one without the final = signs?

Yes. Spaces and line breaks are ignored when decoding, and missing = padding is accepted. Wrapped output such as 76-character MIME lines decodes fine.

How much bigger does Base64 make my data?

About a third. Every 3 bytes become 4 characters, plus padding, so 2,000 bytes become 2,668 characters and 1 MiB becomes 1,398,104 characters.

How do I turn a Base64 string back into a PNG or PDF?

Choose File and Decode, paste the string or the whole data: URL, and press Process. With a data: URL the type is read from it. Otherwise pick the file extension in the menu, from bin, png, jpg, gif, webp, pdf, txt, json and zip.

Is Base64 encryption?

No. Anyone can decode it, and it only changes how bytes are written so they survive text-only channels. Never use it to hide a password.

Does the check for a cut-off image work in every browser?

For PNG, JPEG, GIF and WebP the tool checks the ending of the bytes itself, so that check does not depend on the browser; it was tested in Node. The browsers alone differ: in our test Chromium 153 refused cut pictures, while Firefox 155 and the WebKit build accepted the ones we tried. If the picture looks cropped or gray at the bottom, copy the whole string again.

How long does a large file take?

In Chromium 153 on one Mac, 50 MB of random bytes became 66,666,668 characters in 72 ms and decoded again in 408 ms. Pasting that much text into a box is a separate cost that we did not measure.

Why do SGVsbG8= and SGVsbG9= both decode to Hello?

The last character of a Base64 string carries a few spare bits that belong to no byte, and the browser drops them. Both strings gave the same 5 bytes in Text and File mode in Chrome 157, Firefox 155 and the WebKit build, just as YQ and YR both gave the letter a. RFC 4648 lets a decoder reject a string whose spare bits are not zero, so a stricter tool may refuse SGVsbG9=.

Why does a string copied from a web page fail in Text mode but work in File mode?

A no-break space or another Unicode space inside the string does that. Text mode skips only ASCII spaces, tabs and line breaks, so SGVs, a no-break space and bG8= gave the invalid-Base64 message, while File mode decoded it to Hello. A zero-width space fails in both modes, so delete it and decode again.

Does encoding trim spaces or a final line break?

No. Hello followed by a line break encodes as SGVsbG8K, not SGVsbG8=, and a space at each end turns Hello into IEhlbGxvIA==. Remove the line break first if the other side expects SGVsbG8=.

Related Tools