Base64 Encoding Explained: What It Is, How It Works, and When to Use It

What Is Base64 Encoding?

Base64 is a binary-to-text encoding scheme that converts binary data into a string of ASCII characters. It uses a set of 64 characters — the uppercase letters A-Z, lowercase letters a-z, digits 0-9, and two additional characters (typically + and /) — plus = for padding.

The name “Base64” simply refers to the 64-character alphabet used for encoding. The scheme was originally designed for transmitting binary data over channels that only reliably support text, such as email (MIME) and early internet protocols.

How Base64 Encoding Works

The encoding process follows a straightforward algorithm:

  1. Convert to binary — Each byte of input data is represented as 8 bits.
  2. Group into 6-bit chunks — The binary stream is divided into groups of 6 bits (since 2^6 = 64, each group maps to one character in the Base64 alphabet).
  3. Map to characters — Each 6-bit value (0-63) is mapped to a character: A=0, B=1, …, Z=25, a=26, …, z=51, 0=52, …, 9=61, +=62, /=63.
  4. Add padding — If the input length is not divisible by 3, the output is padded with = characters to make it a multiple of 4.

Example

Encoding the text “Hi” in Base64:

  • “H” = 72 = 01001000, “i” = 105 = 01101001
  • Combined binary: 01001000 01101001
  • Split into 6-bit groups: 010010 000110 100100 (last group padded with zeros)
  • Map to Base64: S, G, k
  • Add padding: SGk=

So “Hi” encodes to SGk=. The = indicates one byte of padding was needed.

Important: Base64 Is NOT Encryption

A very common misconception is that Base64 provides security. It does not. Base64 is a reversible encoding — anyone can decode it instantly. Never use Base64 to “hide” passwords, API keys, tokens, or any sensitive data. It provides zero confidentiality. For actual security, use proper encryption algorithms like AES-256 or use HTTPS for data in transit.

Common Use Cases for Base64

1. Embedding Images in HTML/CSS

Base64 allows you to embed small images directly in HTML or CSS using Data URIs, eliminating an extra HTTP request:

<img src="data:image/png;base64,iVBORw0KGgo..." />

This technique is useful for small icons and logos (under 10KB). For larger images, the 33% size increase from Base64 encoding outweighs the saved HTTP request.

2. Email Attachments (MIME)

Email protocols (SMTP) were designed for 7-bit ASCII text. Binary attachments — images, PDFs, documents — are Base64-encoded in MIME format so they can travel through email servers without corruption.

3. JSON and XML Data Transfer

When you need to include binary data in JSON or XML (which are text-based formats), Base64 is the standard approach. For example, JWT (JSON Web Tokens) use Base64URL encoding for their header and payload segments.

4. Basic Authentication

HTTP Basic Authentication encodes the username:password string in Base64 before sending it in the Authorization header. Again, this is merely encoding, not encryption — which is why Basic Auth should only be used over HTTPS.

5. Storing Binary Data in Text Fields

Some databases and configuration systems only support text fields. Base64 encoding lets you store small binary blobs (like cryptographic keys or certificates) in these fields.

Base64 Variants

Several Base64 variants exist for different contexts:

  • Standard Base64 (RFC 4648) — Uses + and / with = padding. The most common variant.
  • Base64URL — Replaces + with - and / with _ to make the output safe for URLs and filenames. Used in JWT tokens and many web APIs.
  • MIME Base64 — Standard Base64 with line breaks every 76 characters. Used in email encoding.

The 33% Size Overhead

Base64 encoding increases data size by approximately 33%. This happens because every 3 bytes of input become 4 bytes of output (3 × 8 = 24 bits → 4 × 6 = 24 bits, but each 6-bit group is stored as an 8-bit character). For small data, this overhead is negligible. For large files, it can significantly impact performance and bandwidth, which is why Base64-encoding large images or files is generally discouraged.

Working with Base64 in Code

JavaScript

// Encode
btoa("Hello World");  // "SGVsbG8gV29ybGQ="

// Decode
atob("SGVsbG8gV29ybGQ=");  // "Hello World"

Note: btoa() and atob() only handle ASCII. For Unicode text, encode to UTF-8 first using TextEncoder.

Python

import base64
base64.b64encode(b"Hello World")  # b'SGVsbG8gV29ybGQ='
base64.b64decode(b"SGVsbG8gV29ybGQ=")  # b'Hello World'

Command Line

echo -n "Hello World" | base64        # Encode
echo "SGVsbG8gV29ybGQ=" | base64 -d   # Decode

When NOT to Use Base64

  • For securing or hiding data (use encryption instead)
  • For large file embedding (the 33% overhead is wasteful)
  • For data compression (Base64 makes data larger, not smaller)
  • When binary transport is available (modern HTTP supports binary just fine)

Need to quickly encode or decode Base64 data? Try our Base64 Encoder & Decoder — it runs entirely in your browser, so your data stays private.