learn

What is Base64 encoding?

Base64 turns bytes into 64 safe text characters. Learn how the encoding works step by step, why it grows by a third, padding, Base64URL and data URIs.

Base64 in one paragraph

Base64 is a way to write any bytes (an image, a file, binary data) using only 64 printable characters: A–Z, a–z, 0–9, + and /, plus = for padding. It exists because many channels were built for text: e-mail (MIME), JSON, XML, URLs and HTTP headers. Base64 is an encoding, not encryption: anyone can decode it, with no key. The cost is size: the output is about a third larger than the input.

How the encoding works

Base64 takes the input 3 bytes at a time (24 bits), splits them into four groups of 6 bits and writes each group as one character of the alphabet (6 bits = 64 values). The word Man:

StepValue
BytesM 77, a 97, n 110
Bits01001101 01100001 01101110
Groups of 6010011 010110 000101 101110
Indexes19, 22, 5, 46
Base64TWFu

When the input does not fill the last group of 3 bytes, = marks the missing bytes: Ma becomes TWE= and M becomes TQ==. Text is first turned into bytes, normally UTF-8, so olá (4 bytes in UTF-8) becomes b2zDoQ==. Try it in the Base64 encoder and back in the Base64 decoder.

Size: why it grows by a third

Every 3 bytes become 4 characters, so the output is 4/3 of the input, rounded up to a multiple of 4. A 3 MB image becomes about 4 MB of text. That is fine for a small icon in a CSS data URI and wasteful for large files, which should travel as binary (multipart upload, a link to the file).

Variants you will meet

  • Base64URL (RFC 4648, section 5) replaces + with - and / with _, and usually drops the = padding, so the result is safe in URLs and file names. JWT uses it for all three parts.
  • MIME Base64 breaks lines every 76 characters, as in e-mail attachments.
  • Data URIs prefix the type: data:image/png;base64,iVBORw0…. The image to Base64 tool writes them for you.

Common mistakes

  • Treating Base64 as a way to hide passwords or tokens: it is reversible by design. See hash vs encryption for what actually protects data.
  • Decoding Base64URL with a strict standard decoder: restore +, / and the padding first, or use a decoder that accepts both, like the one on this site.
  • Getting garbled accents after decoding: the bytes were UTF-8 but were read as Latin-1, or the other way round.

FAQ

Is Base64 encryption?

No. It only changes the representation of the bytes. Anyone can decode it instantly, so never use it to protect secrets.

Why does Base64 end with = or ==?

The padding fills the last block of 4 characters when the input length is not a multiple of 3 bytes: one = for 2 leftover bytes, two for 1 leftover byte.

How much bigger is Base64 than the original?

About 33% bigger: 4 characters for every 3 bytes, plus line breaks in MIME. A 30 KB file becomes about 40 KB.

What is the difference between Base64 and Base64URL?

Base64URL uses - and _ instead of + and /, and usually omits the padding, so the value can go into a URL or a file name without escaping.