What is Base64?

Base64 is an encoding algorithm that converts binary data into ASCII strings using 64 printable characters (A-Z, a-z, 0-9, +, /) and = as padding. It's widely used for transmitting binary data over text-based protocols.

How Base64 Works

Core Steps

  1. Take 3 bytes (24 bits) of binary data
  2. Split into 4 groups of 6 bits each
  3. Map each 6-bit group to a Base64 character (0-63)
  4. Pad with = if the last group is less than 3 bytes

Encoding Overhead

Base64 increases data size by approximately 33% (3 bytes → 4 characters). This is important to consider for large files.

URL-Safe Base64

Standard Base64 uses + and / which can cause issues in URLs. URL-safe Base64 (RFC 4648 §5) replaces:

  • +-
  • /_
  • Removes trailing = padding

Common Use Cases

1. Email Attachments (MIME)

Email systems use Base64 to encode binary attachments for safe transmission over SMTP.

2. JWT Tokens

JWT Header and Payload use Base64 URL-safe encoding.

3. Data URIs

Embed small images in HTML/CSS:

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

4. HTTP Basic Auth

Authorization: Basic base64(username:password)

Usage Guidelines

When to Use Base64?

  • ✅ Small binary data in JSON
  • ✅ Safe URL data transmission
  • ✅ Email attachments
  • ❌ Large file transfer (use binary)
  • ❌ Sensitive data protection (not encryption)

Performance Tips

  • Browser: btoa() / atob() native APIs
  • Node.js: Buffer.from() / .toString('base64')
  • For files > 1MB, prefer Blob URLs over Base64