What Is Base64 Encoding? How It Works and When to Use It
Dev

What Is Base64 Encoding? How It Works and When to Use It

A developer's guide to Base64 encoding and decoding. Covers how it works, practical uses like image embedding, email attachments, and JWT, plus common pitfalls.

Converting Binary Data for Text-Based Systems

Web developers frequently encounter Base64 in contexts like "encode the data as Base64 before sending" or "embed the image as Base64 in the HTML." But why bother converting to text in the first place?

Base64 is an encoding scheme that converts binary data (images, audio, PDFs, etc.) into 64 printable ASCII characters (A-Z, a-z, 0-9, +, /). Since many internet protocols were originally designed for text, Base64 enables binary data to be safely transmitted through text-based systems.

Base64 Encoder/DecoderEncode or decode strings and files into Base64 format effortlessly.

How Base64 Works

Why 64 Characters?

The "64" relates directly to binary: 2⁶ = 64, so each character represents exactly 6 bits. This allows 3 bytes (24 bits) of binary data to be represented as 4 Base64 characters (4 × 6 bits = 24 bits).

Base64 converts every 3 bytes of binary data into 4 text characters.

The Conversion Process

  1. Split binary data into 3-byte (24-bit) blocks
  2. Divide each block into four 6-bit groups
  3. Map each 6-bit value (0–63) to its Base64 character using the encoding table

If the final block has fewer than 3 bytes, = padding characters are appended.

Data Size Increases by ~33%

Base64-encoded data is approximately 4/3 the size of the original (~33% larger) because 3 bytes become 4 characters. Factor this in when Base64-encoding large files.

Common Use Cases

1. Embedding Images in HTML (Data URLs)

You may have seen data:image/png;base64,iVBOR... in HTML or CSS. This embeds image data directly:

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

Pros: No separate image file needed; reduces HTTP requests. Cons: ~33% size increase; no browser caching; impractical for large images.

2. Email Attachments (MIME)

SMTP (email protocol) was originally text-only. MIME (Multipurpose Internet Mail Extensions) enables binary attachments using Base64. Your email client automatically encodes attachments when sending and decodes them on receipt.

3. JWT (JSON Web Token)

JWTs use Base64URL encoding for their three sections (header, payload, signature). Base64URL is a URL-safe variant that replaces + with - and / with _.

Critical warning: JWTs are Base64-encoded, not encrypted. Anyone can decode the payload. Never put secret information in a JWT payload.

4. Web Font Embedding

Embedding font files as Base64 in CSS can reduce HTTP requests, though the size increase is a tradeoff for smaller fonts.

5. HTTP Basic Authentication

Basic auth sends username:password as Base64 in the Authorization header:

Authorization: Basic dXNlcjpwYXNzd29yZA==

Warning: Base64 is NOT encryption. Always use HTTPS (TLS) with Basic authentication.

Base64 Encoder/DecoderEncode or decode strings and files into Base64 format effortlessly.

Base64 vs. Base64URL

Standard Base64 uses + and /, which are problematic in URLs and filenames. Base64URL addresses this by:

  • Replacing + with -
  • Replacing / with _
  • Omitting = padding

Used widely in JWT, OAuth, and other web protocols.

Code Examples

JavaScript (Browser)

// Encodeconst encoded = btoa('Hello, World!');// Result: "SGVsbG8sIFdvcmxkIQ=="// Decodeconst decoded = atob('SGVsbG8sIFdvcmxkIQ==');// Result: "Hello, World!"

Note: btoa()/atob() don't handle multi-byte characters (like Japanese) directly. Use TextEncoder for Unicode strings.

Node.js

// Encodeconst encoded = Buffer.from('Hello, World!').toString('base64');// Decodeconst decoded = Buffer.from('SGVsbG8sIFdvcmxkIQ==', 'base64').toString('utf-8');

Command Line (macOS/Linux)

# Encodeecho -n 'Hello' | base64# Result: SGVsbG8=# Decodeecho 'SGVsbG8=' | base64 --decode# Result: Hello

Important Caveats

Base64 Is NOT Encryption

Base64 is encoding, not encryption. Anyone can decode it instantly. Never use it to protect sensitive information. For security, combine with HTTPS and use proper encryption (AES, etc.) for sensitive data.

Consider the Size Increase

Embedding many large images as Base64 inflates HTML size and hurts performance. Use sparingly — for small icons or critical above-the-fold images only.

Watch for Newlines

Some Base64 implementations insert newlines every 76 characters. When transmitting via APIs, ensure you're using single-line Base64 strings.

Base64 Encoder/DecoderEncode or decode strings and files into Base64 format effortlessly.

Frequently Asked Questions

Q1. Is Base64-encoded data secure? A. No. Base64 is encoding, not encryption, and anyone can decode it instantly. Use it only for data format conversion, not for protecting sensitive information. Always combine with HTTPS for secure transmission, and use proper encryption algorithms (AES, etc.) for data confidentiality.

Q2. Why does Base64-encoded text end with = or ==? A. Base64 processes data in 3-byte blocks. If the final block has fewer than 3 bytes, padding characters (=) are added to complete it: two = signs mean 1 byte of actual data in the final block, one = means 2 bytes. Base64URL variants typically omit this padding.

Q3. How do I Base64-encode strings containing non-ASCII characters? A. JavaScript's btoa() only handles Latin-1 characters (byte values 0–255). For Unicode/multi-byte strings, convert to UTF-8 bytes first using TextEncoder, then encode. Node.js handles this cleanly: Buffer.from(str, 'utf-8').toString('base64').

Summary

  • Base64 converts binary data into 64 printable ASCII characters
  • Data size increases by ~33%
  • Used for HTML image embedding, email attachments, JWTs, and Basic auth
  • NOT encryption — anyone can decode it
  • Use Base64URL (- and _) for URLs and filenames

Related Articles