Base64 rewrites any data (text, image, file) using only 64 safe characters: letters, digits, “+” and “/”. It is used wherever a channel only carries text: email attachments, images embedded in CSS, authentication tokens, binary data inside JSON. It is defined by the RFC 4648 standard.

How the encoding works

The data is split into chunks of 3 bytes (24 bits), and each chunk into 4 groups of 6 bits. Each 6-bit group is a number from 0 to 63, replaced by a character of the alphabet:

Values0–2526–5152–616263
CharactersA–Za–z0–9+/

Example with “Man”: the bytes M, a, n are 77, 97 and 110, in binary 01001101 01100001 01101110. Regrouped by 6 bits: 010011 010110 000101 101110, that is 19, 22, 5 and 46, which give TWFu.

The “=” sign at the end

When the data length is not a multiple of 3, the last chunk is incomplete. It is padded, and one or two “=” are added to show it:

  • “Man” (3 bytes) → TWFu
  • “Ma” (2 bytes) → TWE=
  • “M” (1 byte) → TQ==

A complete Base64 string therefore always has a length that is a multiple of 4. Some formats (such as JWTs) drop the “=”: the tool’s decoder adds them back automatically.

Encoding examples

TextBytes (UTF-8)Base64
Hello5SGVsbG8=
Man3TWFu
café5 (“é” takes 2 bytes)Y2Fmw6k=
€34oKs

A third bigger

Since 3 bytes become 4 characters, the output is about 33% larger than the original: a 30 KB image becomes roughly 40 KB of Base64. In email (the MIME standard), a line break is also added every 76 characters.

That is why embedding images as Base64 in a page (data:image/png;base64,…) only makes sense for very small files such as an icon: beyond that, the extra weight and the lack of separate caching slow the page down. To make an image lighter, compress it or convert it to WebP instead.

Base64 is not encryption

There is no key and no secret: anyone can decode Base64 in a second. It is an encoding, like writing text in Morse code. Two practical consequences:

  • HTTP “Basic” authentication sends Authorization: Basic followed by username:password in Base64. Without HTTPS, the password travels in readable form.
  • The middle part of a JWT token is Base64URL: anyone can read its content. Only the signature prevents tampering; do not put confidential data in it.

To make text unreadable without a key, you need real encryption, such as the text encryption tool. To get an irreversible fingerprint, use a hash function.

Base64URL: the URL-safe variant

The characters “+”, “/” and “=” have special meanings in a URL. The Base64URL variant (defined in the same RFC 4648) replaces “+” with “-” and “/” with “_”, and usually drops the “=”. The bytes FB FF give +/8= in Base64 and -_8 in Base64URL. This is the format used by JWT tokens: {"alg":"HS256","typ":"JWT"} becomes eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.

Do not confuse it with URL encoding (%20, %C3%A9…), which is used to put text into a web address.

Encoding and decoding in code

JavaScript: watch out for accents

The built-in btoa() and atob() functions only know Latin-1 characters: btoa("€") throws an error, and btoa("é") returns 6Q== instead of the UTF-8 w6k=. Use TextEncoder:

// Encode UTF-8 text
const bytes = new TextEncoder().encode("café");
const b64 = btoa(String.fromCharCode(...bytes)); // "Y2Fmw6k="

// Decode
const bin = atob(b64);
const text = new TextDecoder().decode(Uint8Array.from(bin, c => c.charCodeAt(0))); // "café"

Other languages and the terminal

  • Python: base64.b64encode("café".encode()).decode(), and base64.urlsafe_b64encode() for the URL variant.
  • PHP: base64_encode() and base64_decode().
  • Linux: printf 'Hello' | base64 to encode, base64 -d to decode. With echo, a trailing newline is encoded too (SGVsbG8K).

Frequently asked questions

Why does my decoded output show strange characters?

Either the original content is not text (an image, a PDF, a compressed file), or it was not encoded as UTF-8. Base64 restores the bytes faithfully; it is reading them as text that fails.

How can I recognise Base64?

A string made only of letters, digits, “+” and “/” (or “-” and “_”), often ending in “=” or “==”, with a length that is a multiple of 4. It is only a hint: the word “Test” is itself a valid Base64 string, which does not decode to readable text.

Does Base64 compress data?

No, quite the opposite: it makes it a third larger. Compress first (gzip, zip), then encode if needed.