All posts
tutorials

Base64 Encoding Explained: What It Is and When to Use It

A clear explanation of Base64 encoding — how it works, why it exists, common use cases in web development, and when not to use it.

July 10, 20267 min read
Alex

Written by Alex · Developer & Founder

Solo developer based in Adelaide, Australia. Built MyEasyTools to make everyday file and text tasks faster and free for everyone.

Get more from MyEasyToolsNo ads, higher limits, faster processing

Work through the examples yourself with our free tool

Try JSON Formatter →

Base64 appears in web development constantly — data URIs, JWT tokens, email attachments, API authentication headers — but it's often used without a clear understanding of what it actually does or why it exists. This guide explains the mechanics and the practical decision of when Base64 is the right tool.


What Base64 is (and isn't)

Base64 is an encoding scheme, not encryption. It converts binary data into a string of printable ASCII characters. Anyone can decode it instantly. It provides zero security and zero compression. Its only purpose is to make binary data safely transmittable through text-based systems.

The "Base64" name means it uses 64 printable characters as its alphabet: A–Z (26), a–z (26), 0–9 (10), + (1), / (1). The = character is used for padding.


How it works

Binary data is made of bytes (8 bits each). Base64 takes 3 bytes at a time (24 bits) and encodes them as 4 characters from the 64-character alphabet (6 bits per character: 4 × 6 = 24 bits).

Example: encoding the text "Man":

  1. ASCII bytes: M = 01001101, a = 01100001, n = 01101110
  2. Combined: 010011010110000101101110
  3. Split into 6-bit groups: 010011 | 010110 | 000101 | 101110
  4. Decimal values: 19, 22, 5, 46
  5. Base64 alphabet lookup: T, W, F, u → TWFu

The 3-byte-to-4-character ratio means Base64 output is always exactly 33.3% larger than the input. A 1 MB image encoded as Base64 becomes approximately 1.37 MB.


Why it exists

Many systems were designed to handle text but not arbitrary binary data. In the early internet:

  • Email protocols (SMTP) were designed for 7-bit ASCII text. Sending a binary file directly would corrupt it
  • HTTP headers must be ASCII text
  • HTML and XML documents must be valid text — embedding binary data directly isn't possible
  • Some databases, logging systems, and message queues accept strings but not binary blobs

Base64 solves the "binary data in a text channel" problem by converting binary into a character set that every text system can handle safely.


Common use cases in web development

Data URIs (embedding images in HTML/CSS)

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

A data URI embeds the image data directly in the HTML instead of referencing an external file. This eliminates an HTTP request but increases page size by ~33%.

When it's useful: small icons (under 2 KB) where the request overhead exceeds the size increase. Not useful for photos or large graphics — the size penalty and loss of browser caching make it counterproductive.

I've used this for inline logos in email templates where external image links are blocked by corporate email clients.

JWT authentication tokens

JSON Web Tokens (JWTs) consist of three Base64url-encoded sections separated by dots:

eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJ1c2VyMTIzIn0.signature

The first two sections (header and payload) are Base64url-encoded JSON — anyone can decode them without a key. The signature section is a cryptographic hash that validates the token's integrity. JWT is not encrypted; it's authenticated. The contents are readable by anyone.

Base64url is a variant that replaces + with - and / with _ to make the string URL-safe.

HTTP Basic Authentication

The Authorization: Basic header in HTTP sends credentials as Base64(username:password):

Authorization: Basic dXNlcjpwYXNzd29yZA==

This decodes to user:password. Again — Base64 is not encryption. Basic authentication is only safe over HTTPS, which encrypts the entire header.

Embedding fonts and icons in CSS

@font-face {
  src: url('data:font/woff2;base64,d09GMgAB...') format('woff2');
}

Like data URI images, this embeds font data directly in the stylesheet. Useful for small icon fonts in single-file deployments; counterproductive for body text fonts.

API file uploads

Some REST APIs accept file uploads as Base64-encoded strings in JSON payloads:

{
  "filename": "photo.jpg",
  "content": "data:image/jpeg;base64,/9j/4AAQ..."
}

This is convenient for simple APIs but inefficient — the 33% overhead applies, and large payloads degrade API performance. For production file uploads, multipart form data or direct-to-S3 uploads are better.


When NOT to use Base64

For security or encryption. Base64 is reversible by anyone, instantly. atob('aGVsbG8=') in any browser returns hello. Never Base64-encode sensitive data and consider it protected.

For large file transfers. The 33% size overhead matters at scale. A 10 MB file encoded as Base64 is 13.7 MB. For file transfers, use multipart/form-data or direct binary transfer.

For image optimisation. Data URI images lose the benefit of browser caching. If a 3 KB icon appears on 50 pages and you embed it as a data URI, the browser downloads it 50 times (once per page). As an external file, it downloads once and caches.


Base64 in the browser

The browser provides two built-in functions:

// Encode string to Base64
btoa('hello')           // "aGVsbG8="

// Decode Base64 to string
atob('aGVsbG8=')        // "hello"

// For binary data (files/images), use FileReader or Uint8Array
const reader = new FileReader()
reader.onload = () => console.log(reader.result) // data:image/...;base64,...
reader.readAsDataURL(file)

Note: btoa/atob work on ASCII strings. For arbitrary Unicode or binary data, use the Uint8Array approach or a library.

For working with JSON payloads that contain Base64 data, use our JSON Formatter to pretty-print and verify the structure. The validator handles Base64 strings correctly as string values.


FAQ

Is Base64 the same as encryption? No. Base64 is encoding, not encryption. It converts data to a different representation that any tool can instantly reverse. Encryption requires a key to decode. Never rely on Base64 to protect sensitive data.

Why does Base64 output sometimes end with == or =? Padding. Base64 encodes 3 bytes into 4 characters. If the input isn't a multiple of 3 bytes, padding characters (=) are added to make the output a multiple of 4. One = means 1 byte was padded; == means 2 bytes were padded.

What's the difference between Base64 and Base64url? Standard Base64 uses + and / characters. These have special meaning in URLs, so Base64url replaces them with - and _ respectively. JWT tokens use Base64url encoding to make them URL-safe.

How do I encode a file as Base64 in Python?

import base64
with open('file.jpg', 'rb') as f:
    encoded = base64.b64encode(f.read()).decode('utf-8')

What is Base32 and when is it used? Base32 uses a 32-character alphabet (A–Z plus 2–7). It's less efficient than Base64 (overhead is ~60%) but produces strings with only uppercase letters and numbers, which are easier to type by hand. Common in TOTP (one-time password) seeds and some encoding schemes where case-insensitivity matters.

Get more from MyEasyToolsNo ads, higher limits, faster processing