Base64 Encoding Explained: How It Works and When to Use It

6 min read · Published 1 November 2026 · Updated 2 November 2026

Base64 appears in more places than most developers notice: email attachments, images embedded in web pages, HTTP login headers and the three parts of every JSON Web Token. It is a simple idea, a way to write any binary data using only 64 safe, printable characters, and it is often misunderstood as a way to protect data. This guide explains how it works, what it is for, and where it goes wrong.

What problem Base64 solves

Many channels were designed for text, not for arbitrary bytes. Email bodies, JSON documents, URLs and HTML attributes can break if they contain raw binary values, such as line breaks, null bytes or characters the system treats as special. Base64 turns any sequence of bytes into a string made only of letters, digits and a couple of symbols, which passes safely through those channels, and it can be turned back into the exact original bytes at the other end.

How it works

Base64 reads the input three bytes (24 bits) at a time and splits them into four groups of six bits. Each six-bit value, from 0 to 63, is mapped to one character of a 64-character alphabet: A to Z, a to z, 0 to 9, plus the symbols + and /. Three bytes therefore become four characters.

When the input length is not a multiple of three, the last group is padded with the = character so that the output length is always a multiple of four. Here are the classic examples:

Examples

"Man"  ->  TWFu      (3 bytes, no padding)
"Ma"   ->  TWE=      (2 bytes, one =)
"M"    ->  TQ==      (1 byte, two =)

The size cost

Four characters for every three bytes means the output is about one third larger than the input (a factor of 4/3). Some formats, such as email, also insert line breaks every 76 characters, which adds a little more. This overhead is the reason not to use Base64 for large files when a binary transfer is possible, and the reason a page with many large inline images can load more slowly than one that links to image files.

Base64URL: the URL-safe variant

The standard alphabet contains + and /, which have special meanings in URLs and file names, and the padding character = is reserved in query strings. The Base64URL variant, defined in the same standard, replaces + with - and / with _, and usually omits the padding. JSON Web Tokens use Base64URL for their header, payload and signature, which is why a JWT can travel safely in a URL or an HTTP header.

Where you will meet Base64

  • Data URIs, for example data:image/png;base64,iVBOR..., which embed a small image directly in HTML or CSS.
  • Email attachments (MIME), where binary files are sent as Base64 text.
  • HTTP Basic authentication. The header Authorization: Basic followed by the Base64 of "username:password" is only encoded, not protected, so it must always travel over HTTPS.
  • JSON Web Tokens and many other web tokens.
  • Storing or sending a small amount of binary data, such as a key or a thumbnail, inside a JSON document.

Base64 is not encryption

Anyone can decode Base64 instantly, with no key, because the alphabet and the method are public. It hides nothing. Seeing a string that looks like random letters and ending in = does not mean the content is protected. If data must stay confidential, use real encryption. If you only need to prove it was not changed, use a signature or an HMAC. Never store passwords or secrets in Base64.

A common bug with Unicode text

The browser function btoa() works on strings whose characters are all in the Latin-1 range, so it throws an error for text such as "xin chào" with accents outside that range or for emoji. Convert the text to UTF-8 bytes first, then encode those bytes:

JavaScript

const bytes = new TextEncoder().encode("xin chào");
const base64 = btoa(String.fromCharCode(...bytes));

// Node.js
Buffer.from("xin chào", "utf8").toString("base64");

Quick rules

  1. Use Base64 to carry bytes through text-only channels, not to protect them.
  2. Use the URL-safe variant for anything that goes into a URL, a file name or a cookie.
  3. Convert text to UTF-8 bytes before encoding, and decode back to bytes before reading as text.
  4. Do not Base64 large files when you can send them as binary.

Frequently asked questions

Is Base64 secure?
No. It is a reversible encoding that anyone can undo without a key. Use encryption to keep data secret.
Why does Base64 sometimes end with = or ==?
The = signs are padding. They make the output length a multiple of four characters when the input length is not a multiple of three bytes. Some systems, including JWT, omit the padding.
Why is Base64 output larger than the input?
Each group of three bytes becomes four characters, so the result is about 33% longer, before any line breaks.
What is the difference between Base64 and Base64URL?
Base64URL swaps + for - and / for _, and normally drops the = padding, so the result can be used safely in URLs and file names.

Sources and further reading

Try it yourself: JWT Encoder & Decoder runs in your browser, free and with no sign-up.

More guides