What does Base64 do, and when should you use it?

Why does a file sometimes turn into a long wall of letters and numbers? That's Base64, a way to spell any data as plain text so it can travel where only text is allowed. It isn't a lock, though: anyone can turn it back.

Why turn a file into text?

Some places only take text: the body of an email, a field in a settings file, a line of a web page's code. A photo or a PDF is made of bytes, and plenty of those bytes aren't letters or digits at all, so they can't ride along as they are. Base64 rewrites the bytes with 64 ordinary characters: capital letters, small letters, digits, plus and slash. That's where the name comes from.

It's like reading a phone number aloud instead of handing over the paper it's written on: the words go where the paper can't, and the listener writes down exactly the same number.

That's how a file rides along inside an email, or how a small picture sits inside a web page's code as a data URL. Here's the round trip.

A file's bytes are encoded as Base64 text, sent through a place that only takes text, such as an email, and decoded back into the same file.

A file carried as Base64 text

Decoding at the other end gives back every byte of the original file. Sources and review notes

So use Base64 when the place you're sending data to asks for it. The receiver has to know the text is Base64, or it'll treat the letters as ordinary words.

What changes when data becomes Base64?

Only the spelling. Base64 reads your bytes 3 at a time and writes each group as 4 characters. The official Base64 standard, RFC 4648, uses the word Man as its example.

Man is 3 bytes, and Base64 writes it as the 4 characters TWFu. Decoding turns those characters straight back into the same bytes.

The 3 bytes of Man are encoded as TWFu, and decoding TWFu without any key gives back the same bytes.

Man becomes TWFu and back

Three bytes in, four characters out, and nothing lost on the way back.

Two things follow. The text is about a third longer than the data it carries, which matters when a field has a length limit; the output-size guide compares Base64 with other spellings.

And it isn't a secret. Anyone who sees Base64 can decode it, with no key or password to find, so a password written in Base64 is still readable by anyone. Encryption is what keeps data private; Base64 can carry encrypted bytes, but it doesn't encrypt them.

Want to see your own words in Base64?

FluffyBench's Base encoder

Type any word and it shows the Base64 right away, so you can watch every 3 bytes become 4 characters.

What does the equals sign at the end mean?

Base64 always writes whole groups of 4 characters. When your data runs out partway through a group, equals signs fill the empty spots. They aren't data; they tell the decoder where the bytes stop.

The standard's own examples show the pattern: one leftover byte gets two equals signs, two leftover bytes get one, and a full group of three gets none.

f becomes Zg==; fo becomes Zm8=; foo becomes Zm9v. The equals signs are shaded as padding.

Equals signs pad a short final group

Only the shaded equals signs are padding; every other character spells the bytes.

The standard uses padding, but some formats that ask for Base64 say to leave it off. If a value arrives without equals signs, follow that format's rules instead of adding or deleting them yourself.

What happens when you encode typed text?

Typed text has to become bytes first. An encoding called UTF-8 turns each character into one or more bytes, and Base64 spells those bytes.

Here's the standard's five-letter example, fooba, at each stage.

The text fooba becomes the UTF-8 bytes 66 6F 6F 62 61, which Base64 writes as Zm9vYmE=.

Text, then UTF-8 bytes, then Base64

The 5 letters make 5 bytes, which Base64 writes as 8 characters, the last one padding.

A different text encoding would make different bytes, and so different Base64, from the same words. If your Base64 has to match someone else's exactly, learn how text encodings differ.

What changes for web addresses and file names?

Plus and slash already mean something in web addresses and file paths. So a variant called Base64url swaps them for a hyphen and an underscore, and leaves everything else alone.

Only the last two of the 64 symbols change.

Base64 and Base64url share their first 62 symbols. Base64url writes + as - and / as _.

Base64url swaps two symbols

Same grouping, two different symbols. Whether the equals signs stay is up to the format that asks for Base64url.

FluffyBench's Base encoder and its File to Base64 tool both write standard Base64, with plus, slash and padding.

Doing it in FluffyBench

FluffyBench has two Base64 tools, one for typed words and one for files. For typed text, use the Base encoder. Here's fooba going in and Zm9vYmE= coming out.

  1. FluffyBench Base encoder with From set to Text and To set to Base64.
    FluffyBench Base encoder with From set to Text and To set to Base64.
    Step 1. Set From to Text and To to Base64.
    1. From: Text, because you're typing words.
    2. To: Base64, the spelling you need.
  2. The Input box holding the word fooba, with Input and Result tabs above it.
    The Input box holding the word fooba, with Input and Result tabs above it.
    Step 2. Type or paste your text. A short input updates by itself, with no button to press.
    1. Type fooba here.
    2. On a phone, open Result to see it.
  3. The Result box showing Zm9vYmE= with its Copy and Download buttons.
    The Result box showing Zm9vYmE= with its Copy and Download buttons.
    Step 3. Check the result, then Copy it or Download it as a text file.
    1. fooba in Base64: Zm9vYmE=
    2. Copy puts it on your clipboard.
    3. Download saves the Base64 as a text file.

To read Base64 back as words, swap the From and To choices. If the decoded bytes aren't valid UTF-8 text, converting to Text shows an error instead of garbled characters. Changing Base64 into another spelling, such as Base16, works for any bytes.

For a file, use FluffyBench's File to Base64 and data URLs. Here it turns a small PNG into Base64 text.

  1. FluffyBench File to Base64 controls with From set to File and To set to Base64.
    FluffyBench File to Base64 controls with From set to File and To set to Base64.
    Step 1. Keep From set to File, then open To.
    1. From: File, because you're starting with a file.
    2. Open To to pick the text you need.
  2. The To list open, with Base64 and Data URL among the choices.
    The To list open, with Base64 and Data URL among the choices.
    Step 2. Choose Base64 for the file's bytes as plain text. Choosing closes the list.
    1. Base64: the bytes as text, nothing added.
    2. Data URL: adds a type label for web pages.
  3. The file drop area with its Choose file button.
    The file drop area with its Choose file button.
    Step 3. Choose your file. Encoding starts as soon as it's picked.
    1. Choose file opens your device's file picker.
  4. The Base64 result with its character count, Copy and Download .txt buttons.
    The Base64 result with its character count, Copy and Download .txt buttons.
    Step 4. Check the length against the destination's limit, then Copy the text or Download .txt.
    1. The character count of the Base64 text.
    2. Copy puts all of it on your clipboard.
    3. Download .txt saves the same text.

To rebuild a file from Base64, use File to Base64 and data URLs the other way round. The Base encoder can't do this job, because its Download saves text.

  1. The From list open, with Base64 among the choices.
    The From list open, with Base64 among the choices.
    Step 1. Open From and choose Base64. To switches to File.
    1. From: Base64, for plain Base64 text.
  2. Base64 text pasted into the box to decode.
    Base64 text pasted into the box to decode.
    Step 2. Paste the Base64. It is decoded as you paste.
    1. Paste the whole Base64 value here.
  3. The recovered file card with its byte count and the Download decoded-file.bin button.
    The recovered file card with its byte count and the Download decoded-file.bin button.
    Step 3. Check the byte count, then download. The file is always named decoded-file.bin, so rename it with the right ending, such as .png.
    1. How many bytes came back.
    2. Download saves the recovered bytes as a file.

For the curious: how Base64 regroups the bits

Every byte holds 8 bits, the 0s and 1s a computer stores. Base64 regroups them 6 at a time, because 6 bits make exactly 64 possible values, one for each symbol in its alphabet.

Here's fooba again, with each byte's bits in its own color so you can follow them into the six-bit groups that spell Zm9vYmE=.

The RFC example fooba starts as UTF-8 bytes, becomes Base64 symbols, and ends with a padding mark.

How bytes become Base64 symbols

  1. Start with UTF-8 bytes

    Each letter of fooba is one byte, shown in hex and as its eight bits.

  2. One piece picks m

    Group 2 reads 100110, which is 38: the 39th symbol, m. Group 6 has the same bits, so it's m too.

  3. The last mark is padding

    No bits are left for the final spot, so an equals sign fills it. It isn't one of the original bytes.

Read each group's bits as a number, then find that position in the alphabet, which runs A–Z, a–z, 0–9, + and /, counting from 0.
Search FluffyBench