Skip to content
DoItJag

Base64 explained: how it works and when to use it

Published . Last reviewed .

In short:Base64 writes any bytes using 64 safe characters, so binary data can travel through systems built for text. Every 3 bytes become 4 characters, which makes the result about a third larger. It hides nothing: anyone can decode it.

What problem Base64 solves

Many systems were built to carry text, not arbitrary bytes. Email, URLs, JSON, XML and HTTP headers all have characters that mean something special, or bytes they cannot carry at all. An image or a PDF is full of such bytes.

Base64 gets around this by writing any data using only 64 characters that pass safely through almost anything: the letters A to Z and a to z, the digits 0 to 9, and two symbols, + and /. A 65th character, =, is used for padding at the end. The standard is RFC 4648, published by the IETF.

You meet Base64 in email attachments, in data: URLs that embed images in web pages, in HTTP Basic authentication headers, in JSON Web Tokens and in many configuration files.

How the encoding works

The idea is to regroup bits. A byte has 8 bits. Base64 takes 3 bytes at a time, which is 24 bits, and splits them into four groups of 6 bits. Each 6-bit group is a number from 0 to 63, and that number picks one character from the alphabet. RFC 4648 puts it in one line: “24-bit groups of input bits” become “output strings of 4 encoded characters”.

Values Characters
0 to 25 A to Z
26 to 51 a to z
52 to 61 0 to 9
62 and 63 + and /

Worked example: “foo”

The text foo is three bytes. In binary:

f = 0x66 = 01100110
o = 0x6F = 01101111
o = 0x6F = 01101111

Joined together, that is 24 bits. Split into four groups of 6:

011001  100110  111101  101111
  25      38      61      47

Look each number up in the table:

  • 25 is Z (the 26th capital letter, counting from 0)
  • 38 is m (38 − 26 = 12, the 13th small letter)
  • 61 is 9 (61 − 52 = 9)
  • 47 is v (47 − 26 = 21)

So foo becomes Zm9v. RFC 4648 gives this as one of its test vectors, along with foobar, which becomes Zm9vYmFy.

Padding with =

Data rarely comes in exact multiples of three bytes. When it does not, the last group is short, and the RFC says what to do:

  • One byte left over (8 bits): it becomes two characters followed by ==.
  • Two bytes left over (16 bits): they become three characters followed by =.

The RFC’s own examples show it clearly:

Input Bytes Base64
f 1 Zg==
fo 2 Zm8=
foo 3 Zm9v

So an = or == at the end of a string is a strong hint that you are looking at Base64. The padding tells a decoder how many real bytes the last group holds. Where the length is already known some other way, the RFC allows the padding to be left out, and many decoders accept input without it.

Why Base64 makes data bigger

Every 3 bytes become 4 characters, so the output is 4/3 the size of the input, about 33% larger. The exact length of standard Base64 output is:

length = 4 × ceiling(bytes ÷ 3)

Some worked figures:

  • a 100-byte value: 100 ÷ 3 = 33.33, rounded up to 34 groups, 34 × 4 = 136 characters;
  • a 100 KB image (102,400 bytes): 102,400 ÷ 3 = 34,133.33, rounded up to 34,134 groups, 34,134 × 4 = 136,536 characters;
  • a 1,000,000-byte file: rounded up to 333,334 groups, so 1,333,336 characters.

That growth matters. Embedding a 100 KB photo in an HTML page or a JSON response as Base64 adds about 34 KB to it, and the page cannot cache the image separately. For small icons it is a fair trade; for large images, a normal file link is usually better.

Text adds a twist: before encoding, characters are turned into bytes, usually as UTF-8, and non-English characters take more than one byte. Café ₹ is six characters but nine bytes (é takes two, the rupee sign takes three), so it encodes to twelve characters: Q2Fmw6kg4oK5.

base64url: the variant for addresses and file names

The standard alphabet has two characters that cause trouble in URLs and file names: + (which can mean a space in a query string) and / (a path separator). RFC 4648 section 5 defines a second alphabet, “base64url”, that is identical except for those two:

Value Standard base64url
62 + -
63 / _

Base64url usually drops the = padding as well. JSON Web Tokens use it, which is why their parts never contain +, / or =. For example, the two bytes 0xFB 0xFF encode to +/8= in standard Base64 and to -_8 in base64url.

If a decoder rejects your string, check which alphabet it uses. A - or _ means base64url.

Data URLs

RFC 2397 defines the data: URL scheme, which carries a small file inside the address itself:

data:[<mediatype>][;base64],<data>

A tiny PNG image in a web page might look like data:image/png;base64,iVBORw0KGgo.... If the media type is left out, the RFC says it defaults to plain ASCII text. The RFC also notes the scheme “is only useful for short values”. Browsers may limit how long a data URL can be, and the 33% growth applies here too.

Base64 is not encryption

This is the most important point. Base64 is an encoding: a different way of writing the same bytes. There is no key, and anyone can reverse it in a second. A password or API key “hidden” in Base64 is not hidden at all.

HTTP Basic authentication shows why this matters. The header Authorization: Basic YWRtaW46c2VjcmV0 decodes straight back to admin:secret. It is safe only because the connection itself is encrypted with HTTPS. The same goes for a JSON Web Token: its contents are readable by anyone, and only the signature stops them from being changed.

If you need to keep data private, encrypt it. If you need to check that it has not changed, use a hash or a signature. Use Base64 only to carry bytes through a text channel.

Decoding something you found

If you come across a long block of letters ending in = in a log, an email source or a config file:

  1. Paste it into the Base64 encoder and decoder. If it decodes to readable text, you will see it at once.
  2. If the result is not text, it may be a file. Images often start with iVBOR (PNG) or /9j/ (JPEG) once encoded. The Base64 to image tool shows the picture and lets you save it.
  3. To go the other way, for a CSS background or an email template, use image to Base64.

All three run in your browser, so a token or file you paste stays on your device.

Sources

Rules and rates change. Check the current notice or official page before you rely on a figure here. This guide is general information, not legal, tax or financial advice. Spotted a mistake? See our editorial and corrections policy.