BYTEKIT
dev tools
Home Blog Try Tool → About
⚡ 17 Free Tools
Encoding

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

📅 January 2025 ⏱ 7 min read ✍ ByteKit Team · Bengaluru, India

Base64 is an encoding scheme that converts binary data into a string of ASCII characters. It is one of those technologies that developers encounter constantly but rarely stop to understand deeply. This guide explains what Base64 is, exactly how the algorithm works, and the most common real-world use cases.

What is Base64?

Base64 is a binary-to-text encoding scheme that represents binary data using only 64 printable ASCII characters. These 64 characters are:

Plus the = character used for padding. The name "Base64" comes from the fact that it uses exactly 64 distinct characters to represent data.

The URL-safe variant (Base64Url) replaces + with - and / with _, making it safe to use in URLs and filenames without escaping.

Why Does Base64 Exist?

The fundamental problem Base64 solves is this: many systems and protocols were designed to handle text, not arbitrary binary data. Email (SMTP), HTTP headers, JSON, XML, and many older protocols can only safely transmit a limited set of ASCII characters. Binary data (images, files, executable code) contains bytes that are not printable ASCII characters and can corrupt these systems.

Base64 solves this by converting any binary data into a safe, printable text representation that can pass through any text-based system without corruption.

How Base64 Encoding Works

The algorithm works in three steps:

  1. Take 3 bytes (24 bits) of input
  2. Split into four 6-bit groups
  3. Map each 6-bit group to one of the 64 Base64 characters

Since each Base64 character represents 6 bits, three bytes of input (24 bits) become four Base64 characters. This means Base64 increases the data size by approximately 33% (4/3 ratio).

Let's trace through a concrete example. Encoding the string Man:

ASCII values:   M=77,  a=97,  n=110
Binary:         01001101 01100001 01101110

Split into 6-bit groups:
                010011 010110 000101 101110

Map to Base64:  T      W      F      u

Result: "TWFu"

Padding

When the input is not divisible by 3, padding characters = are added:

"M"   → "TQ=="
"Ma"  → "TWE="
"Man" → "TWFu"  (no padding)

Common Use Cases for Base64

1. Data URLs (Embedding Images in HTML/CSS)

Base64 allows you to embed images directly in HTML or CSS without a separate file request:

<!-- Inline image in HTML -->
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...">

/* Inline background in CSS */
.icon {
  background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucy...');
}

This is useful for small icons and images that need to load instantly without an additional HTTP request.

2. JWT Tokens

JSON Web Tokens use Base64Url encoding for both the header and payload portions:

// Header (decoded)
{"alg": "HS256", "typ": "JWT"}

// Header (Base64Url encoded)
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9

Note: JWT uses Base64Url (not standard Base64) to make the token safe for use in URLs without URL encoding.

3. Basic Authentication in HTTP

The HTTP Basic Auth scheme encodes credentials in Base64:

// Credentials: arjun:mypassword123
// Base64 encoded: YXJqdW46bXlwYXNzd29yZDEyMw==

Authorization: Basic YXJqdW46bXlwYXNzd29yZDEyMw==

Important: Basic Auth only encodes — it does not encrypt. Always use HTTPS with Basic Auth.

4. Email Attachments (MIME)

Email attachments are encoded in Base64 to pass safely through SMTP servers that were designed for text-only content. When you send a PDF attachment, your email client Base64-encodes it before transmission.

5. Storing Binary Data in JSON or XML

JSON and XML are text formats that cannot contain raw binary data. Base64 allows binary data (images, files, certificates) to be embedded in JSON APIs:

{
  "filename": "profile.jpg",
  "contentType": "image/jpeg",
  "data": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBD..."
}

6. Cryptographic Keys and Certificates

SSL/TLS certificates, RSA public keys, and cryptographic material are commonly stored and transmitted in Base64-encoded PEM format:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA...
-----END PUBLIC KEY-----

Base64 in Code

JavaScript

// Encode
const encoded = btoa("Hello, ByteKit!");
console.log(encoded); // "SGVsbG8sIEJ5dGVLaXQh"

// Decode
const decoded = atob("SGVsbG8sIEJ5dGVLaXQh");
console.log(decoded); // "Hello, ByteKit!"

// For Unicode strings (btoa only handles ASCII):
const encoded = btoa(unescape(encodeURIComponent("नमस्ते")));
const decoded = decodeURIComponent(escape(atob(encoded)));

Python

import base64

# Encode
encoded = base64.b64encode(b"Hello, ByteKit!")
print(encoded)  # b'SGVsbG8sIEJ5dGVLaXQh'

# Decode
decoded = base64.b64decode("SGVsbG8sIEJ5dGVLaXQh")
print(decoded)  # b'Hello, ByteKit!'

# URL-safe variant
url_safe = base64.urlsafe_b64encode(b"Hello, ByteKit!")

What Base64 is NOT

Summary

Base64 is a simple, universally supported encoding that converts binary data into safe ASCII text. It is widely used in JWT tokens, data URLs, HTTP authentication, email attachments, and API data transfer. Use our free Base64 Encoder and Decoder to encode or decode any string instantly in your browser.

🛠️
Try Our Free Base64 Encoder
No login, no upload, no tracking — runs entirely in your browser.
Open Base64 Encoder →