Skip to main content
Back to blog

What Is Base64 Encoding and How Does It Work?

Learn what Base64 encoding is, how the algorithm works, and where it is used in practice — from email attachments to data URIs and API tokens.


What Is Base64?

Base64 is a binary-to-text encoding scheme that represents binary data using a set of 64 printable ASCII characters. The name comes directly from the fact that the encoding alphabet contains exactly 64 characters: the uppercase letters A through Z, the lowercase letters a through z, the digits 0 through 9, and the two symbols + and /. A padding character = is also used when the input length is not a multiple of three.

The core purpose of Base64 is simple: allow binary data to travel safely through systems that were designed to handle only text. Email protocols, JSON payloads, HTML attributes, and URL parameters all expect text. Base64 gives you a reliable way to embed binary content inside those text-only channels without corruption.

How the Encoding Algorithm Works

Base64 operates on groups of three bytes (24 bits) at a time. Each group is split into four 6-bit chunks, and each chunk is mapped to one of the 64 characters in the encoding table.

Here is a step-by-step example encoding the string Hi!:

  1. Convert each character to its ASCII byte value: H = 72, i = 105, ! = 33.
  2. Write those bytes in binary: 01001000 01101001 00100001.
  3. Split the 24 bits into four 6-bit groups: 010010, 000110, 100100, 100001.
  4. Convert each group to a decimal index: 18, 6, 36, 33.
  5. Look up each index in the Base64 alphabet: S, G, k, h.

The result is SGkh. If the input length is not divisible by three, the output is padded with one or two = characters so the encoded string length is always a multiple of four.

Decoding

Decoding is the reverse. Each Base64 character maps back to a 6-bit value. Reassemble those bits into 8-bit bytes and you recover the original binary data.

Common Use Cases

Email Attachments (MIME)

The original motivation for Base64 was email. The SMTP protocol is a text protocol, so binary attachments like images and PDFs must be encoded. MIME (Multipurpose Internet Mail Extensions) uses Base64 to embed attachments directly in the email body.

Data URIs in HTML and CSS

You can embed small images or fonts directly in HTML or CSS using data URIs:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...

This eliminates an extra HTTP request, which can improve page load performance for small assets. The trade-off is that the encoded data is roughly 33% larger than the original binary.

API Tokens and HTTP Headers

Many authentication schemes use Base64. HTTP Basic Authentication encodes the username and password pair as Base64(username:password) and sends it in the Authorization header. JSON Web Tokens (JWTs) use a URL-safe variant called Base64URL to encode their header and payload sections.

Storing Binary Data in JSON or XML

JSON and XML have no native way to represent binary data. Base64 lets you include images, certificates, or encrypted blobs inside a JSON object or XML document as a simple string field.

Base64 Is Not Encryption

A common misconception is that Base64 provides security. It does not. Base64 is fully reversible by anyone — there is no key, no secret, and no protection. If you need confidentiality, use actual encryption (AES, ChaCha20, etc.) and then optionally Base64-encode the ciphertext for transport.

The 33% Size Overhead

Because three bytes of input become four bytes of output, Base64 always increases data size by approximately 33%. For large files, this overhead matters. That is why Base64 is best suited for small payloads — thumbnails, icons, configuration tokens — rather than multi-megabyte files.

Variants of Base64

The standard Base64 alphabet uses + and /, which conflict with URL syntax. The Base64URL variant replaces them with - and _ and typically omits padding. This variant is used in JWTs and anywhere the encoded string may appear in a URL.

Try It Yourself

If you want to encode or decode Base64 strings quickly, you can use our Base64 Encode / Decode tool. Paste any text or Base64 string and get instant results — everything runs in your browser with nothing uploaded to a server.

Summary

Base64 is a foundational encoding that bridges the gap between binary data and text-only systems. It is used everywhere from email to web development to API design. Understanding how it works — and what it does not do (encryption) — makes you a more effective developer and helps you choose the right tool for the job.