Skip to main content
← All Articles ยท ยท 8 min read

Base64 Encoding Explained: When to Use It and When to Avoid It

Base64 encoding explained: when to use it in APIs, image data URIs, and auth tokens. Real-world examples and common pitfalls for web developers.

base64encodingwebapisecurity

Base64 Encoding Explained: When to Use It and When to Avoid It

Base64 shows up in a surprising number of places in web development: HTTP Basic Auth headers, embedding images in CSS, JWT tokens, file uploads via API, and more. But itโ€™s also frequently misused โ€” people use it to โ€œencryptโ€ data (itโ€™s not encryption) or to hide values from users (itโ€™s trivially reversible). Understanding what it actually does and why it exists saves you from these mistakes.

What is Base64?

Base64 is an encoding scheme that converts binary data into ASCII text. It represents binary data using only 64 printable characters: A-Z, a-z, 0-9, +, /, and = for padding.

Why 64? Because these characters are safe to transmit in any text-based protocol โ€” email, HTTP headers, XML, JSON, URLs.

Binary:  01001000 01100101 01101100 01101100 01101111
Text:    Hello
Base64:  SGVsbG8=

Base64 is NOT encryption. Base64 is NOT compression. Itโ€™s just a way to represent binary data as text. Anyone can decode it instantly.

Why Base64 Exists

Before modern protocols handled binary data natively, many systems (SMTP email, early HTTP, terminal emulators) were designed for 7-bit ASCII text. Sending binary data (images, attachments, executable files) directly would corrupt the data.

Base64 solved this: โ€œI need to send binary data through a text-only channel, so Iโ€™ll represent it using only safe printable ASCII characters.โ€

Today, most protocols can handle binary data natively, so Base64 is less essential but still widely used for convenience and compatibility.

How Base64 Works

Base64 takes 3 bytes of input and produces 4 characters of output:

Input:   H     e     l
Binary:  01001000 01100101 01101100
Groups:  010010 000110 010101 101100
Index:   18     6      21     44
Chars:   S      G      V      s

Since every 3 input bytes become 4 output characters, Base64 increases data size by 33%.

The = padding at the end appears when the input isnโ€™t divisible by 3:

  • 1 byte remainder โ†’ == padding
  • 2 bytes remainder โ†’ = padding
  • 0 bytes remainder โ†’ no padding

JavaScript Implementation

// Browser: built-in btoa/atob
const encoded = btoa('Hello, World!');
// 'SGVsbG8sIFdvcmxkIQ=='

const decoded = atob('SGVsbG8sIFdvcmxkIQ==');
// 'Hello, World!'

// btoa only works with ASCII/Latin-1
// For Unicode text, you need to encode first:
function encodeUnicode(str) {
  return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (_, p1) =>
    String.fromCharCode('0x' + p1)
  ));
}

// Node.js: Buffer
const encoded = Buffer.from('Hello').toString('base64');
// 'SGVsbG8='

const decoded = Buffer.from('SGVsbG8=', 'base64').toString('utf8');
// 'Hello'

// Binary data (like an image)
const imageBuffer = fs.readFileSync('image.png');
const base64Image = imageBuffer.toString('base64');

Python Implementation

import base64

# Encode
encoded = base64.b64encode(b'Hello, World!')
# b'SGVsbG8sIFdvcmxkIQ=='

# Decode
decoded = base64.b64decode('SGVsbG8sIFdvcmxkIQ==')
# b'Hello, World!'
decoded_str = decoded.decode('utf-8')
# 'Hello, World!'

# From a file
with open('image.png', 'rb') as f:
    image_data = f.read()
encoded_image = base64.b64encode(image_data).decode('ascii')

# URL-safe Base64 (replaces + with - and / with _)
url_safe = base64.urlsafe_b64encode(binary_data).decode('ascii')
url_safe_decoded = base64.urlsafe_b64decode(url_safe)

Real-World Use Cases

HTTP Basic Authentication

Basic Auth encodes username:password in Base64:

const credentials = `${username}:${password}`;
const encoded = btoa(credentials);

fetch('https://api.example.com/data', {
  headers: {
    'Authorization': `Basic ${encoded}`
  }
});

This is NOT secure if used over HTTP. Over HTTPS itโ€™s acceptable, but tokens (Bearer auth) are preferred for modern APIs. The Base64 here doesnโ€™t add security โ€” itโ€™s just the protocol format.

Data URIs (Embedding Images in HTML/CSS)

<!-- Small icons or thumbnails embedded directly in HTML -->
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." alt="icon">
/* Background image inline in CSS */
.icon {
  background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0c...');
}

When to use: for small images (< 2KB) in performance-critical scenarios where you want to eliminate an extra HTTP request. For larger images, just use a URL โ€” the base64 size penalty (33% larger) makes it slower.

JWT Tokens

JWT tokens use Base64URL encoding (URL-safe variant without padding):

// A JWT looks like: header.payload.signature
const jwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';

const parts = jwt.split('.');
const header = JSON.parse(atob(parts[0]));
const payload = JSON.parse(atob(parts[1]));
// The signature (parts[2]) requires the secret key to verify

console.log(payload);
// { sub: '1234567890', name: 'John Doe', iat: 1516239022 }

The payload is Base64-encoded, not encrypted. Anyone can decode it. This is intentional โ€” JWT payloads are meant to be readable by the client. Never put sensitive data (passwords, payment info) in a JWT payload.

File Uploads via API (Multipart vs Base64)

Some APIs accept files as Base64 strings in JSON. This is convenient but not recommended for large files:

// Base64 approach โ€” convenient but adds 33% overhead
const file = document.getElementById('upload').files[0];
const reader = new FileReader();

reader.onload = async (e) => {
  const base64Data = e.target.result.split(',')[1]; // Remove "data:image/png;base64,"

  await fetch('/api/upload', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      filename: file.name,
      data: base64Data
    })
  });
};
reader.readAsDataURL(file);
// Multipart form data โ€” better for files
const formData = new FormData();
formData.append('file', file);

await fetch('/api/upload', {
  method: 'POST',
  body: formData  // No Content-Type needed โ€” browser sets it with boundary
});

For files under ~1MB, either approach is acceptable. For larger files, use multipart form data or presigned URLs (S3/GCS).

Email Attachments (MIME)

Email attachments are Base64-encoded. When you send an email with an attachment, the attachment is encoded like this in the MIME message:

Content-Type: image/png
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="screenshot.png"

iVBORw0KGgoAAAANSUhEUgAAA...
(rest of base64-encoded image)

URL-Safe Base64

Standard Base64 uses + and / characters, which have special meaning in URLs. URL-safe Base64 replaces them with - and _:

// Standard: may need URL-encoding
const standard = btoa(data);  // might contain + and /

// URL-safe: safe to use in URLs without encoding
function toBase64Url(str) {
  return btoa(str)
    .replace(/\+/g, '-')
    .replace(/\//g, '_')
    .replace(/=+$/, '');  // Remove padding
}

function fromBase64Url(str) {
  const padded = str + '='.repeat((4 - str.length % 4) % 4);
  return atob(padded.replace(/-/g, '+').replace(/_/g, '/'));
}

JWTs and many OAuth tokens use URL-safe Base64 without padding.

Common Mistakes

Mistake 1: Using Base64 for โ€œsecurityโ€

// Wrong: Base64 is trivially reversible
const "hidden" = btoa(password);  // Anyone can atob() this

// Right: use proper hashing or encryption
const hash = await bcrypt.hash(password, 12);

Mistake 2: Using standard Base64 in URLs

// Wrong: + and / break URLs
const token = btoa(userData);
const url = `https://example.com/verify?token=${token}`;

// Right: URL-safe Base64
const token = toBase64Url(userData);
const url = `https://example.com/verify?token=${token}`;

Mistake 3: Forgetting that Base64 increases size

// A 1MB file becomes 1.33MB in Base64
// Don't use Base64 for large file transfers

Key Takeaways

  • Base64 converts binary data to ASCII text โ€” it is NOT encryption or compression.
  • It adds ~33% size overhead.
  • Use cases: HTTP Basic Auth headers, data URIs, JWT tokens, email attachments.
  • Use URL-safe Base64 (with - and _ instead of + and /) when Base64 appears in URLs.
  • Never use Base64 to โ€œhideโ€ sensitive data โ€” itโ€™s trivially decoded.
  • For files in APIs, prefer multipart form data over Base64 JSON embedding.

FAQ

Is Base64 the same as encryption? No. Base64 is trivially reversible. Anyone can decode it without a key. It provides zero security. Use proper encryption (AES, RSA) or hashing (bcrypt, Argon2) for security.

Why does Base64 sometimes have == at the end? Thatโ€™s padding. Base64 works in groups of 3 bytes โ†’ 4 characters. When the input isnโ€™t divisible by 3, = characters pad the output to a multiple of 4 characters.

Whatโ€™s the difference between Base64 and Base64URL? Base64 uses + and /. Base64URL uses - and _ instead, and typically omits the padding =. Base64URL is safe to use in URLs without percent-encoding.

Should I use btoa/atob or Buffer in Node.js? Buffer is more reliable in Node.js, especially for binary data and Unicode. btoa/atob are available in Node.js 16+ but were designed for browsers and have limitations with non-Latin-1 strings.

Free Download

50 CLI Commands That Save Hours Every Week

The exact Git shortcuts, file search tricks, Docker commands, and productivity hacks that senior developers use daily. Compiled from 5+ years of shipping production code.

  • Git mastery (10 cmds)
  • File & search wizardry
  • Docker & DevOps tricks
  • Process & network tools
  • Productivity boosters
  • macOS & Linux install guide

No spam. 1 email/week max. Unsubscribe anytime.

Found this guide useful? Check out our free developer tools.