Base64 Encoding Explained: What It Is & How to Encode/Decode

A complete breakdown of Base64 encoding with real-world examples

Developer ToolsApril 12, 20268 min read

What Is Base64 Encoding?

Base64 is a binary-to-text encoding scheme that converts binary data into a string of ASCII characters. It takes any binary input — images, files, encrypted data — and represents it using only 64 printable characters: A-Z, a-z, 0-9, +, and /, with = as padding.

The result is a string that is safe to include in JSON, XML, HTML, email, and URL contexts where raw binary data would break things. Base64 is not encryption — it's easily reversible — but it solves a critical problem: safely transmitting binary data through text-based protocols.

How the Base64 Algorithm Works

The encoding process follows a straightforward three-step algorithm:

  1. Convert to bytes — The input string is converted to its byte representation (UTF-8 encoding).
  2. Group into 24-bit chunks — Bytes are grouped into sets of three (24 bits total).
  3. Split into 6-bit segments — Each 24-bit group is divided into four 6-bit segments, each mapped to a Base64 character.

For example, encoding the word "Man":

Input:  M     a     n
ASCII:  77    97    110
Binary: 01001101 01100001 01101110
Groups: 010011 010110 000101 101110
Index:  19      22     5      46
Base64: T       W      F      u
Result: TWFu

The output TWFu is the Base64 encoding of "Man". When the input length isn't a multiple of three, padding characters (=) are added to make the output length a multiple of four.

Why Base64 Increases Data Size by 33%

Every 3 bytes of input become 4 characters of output. This 4:3 ratio means Base64-encoded data is always about 33% larger than the original. This overhead is the cost of making binary data text-safe. For most use cases (embedding small images, encoding tokens, email attachments), this increase is acceptable.

Common Use Cases for Base64

Data URIs in HTML and CSS

Small images can be embedded directly in HTML or CSS using Base64 data URIs, eliminating extra HTTP requests:

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

This is useful for icons, small logos, and email templates where external resources may be blocked.

API Authentication

HTTP Basic Authentication encodes credentials as base64(username:password) and sends them in the Authorization header. While this should always be combined with HTTPS (Base64 is not encryption), it's a standard authentication mechanism used by many APIs.

Email Attachments (MIME)

Email was originally designed for plain text (7-bit ASCII). MIME uses Base64 to encode binary attachments like images, PDFs, and documents so they can travel through the email system intact.

JSON Web Tokens (JWT)

JWTs use Base64URL encoding (a variant that replaces + with - and / with _) for the header and payload segments. Understanding Base64 is essential for debugging JWT tokens.

Storing Binary Data in Databases

Some databases or ORM configurations handle binary data poorly. Base64 encoding provides a workaround by storing binary data as text columns.

Base64 vs. Base64URL

Standard Base64 uses + and / which have special meaning in URLs. Base64URL replaces these with - and _ and removes padding. This variant is used in JWTs, URL-safe tokens, and other contexts where the encoded string appears in a URL query parameter.

How to Encode and Decode Base64

You can use RiseTop's free Base64 encoder/decoder for instant conversions. In code, every major language provides built-in support:

// JavaScript
btoa("Hello, World!")  // "SGVsbG8sIFdvcmxkIQ=="
atob("SGVsbG8sIFdvcmxkIQ==")  // "Hello, World!"

// Python
import base64
base64.b64encode(b"Hello, World!")  # b'SGVsbG8sIFdvcmxkIQ=='
base64.b64decode(b'SGVsbG8sIFdvcmxkIQ==')  # b'Hello, World!'

// Linux terminal
echo -n "Hello, World!" | base64
echo "SGVsbG8sIFdvcmxkIQ==" | base64 -d

Security Considerations

Base64 is not encryption. Anyone who sees Base64-encoded data can decode it instantly. Never use Base64 alone to protect sensitive information. However, Base64 is commonly used as part of encryption schemes — the encrypted binary output is Base64-encoded for transport. Also be aware that Base64-encoded strings don't have any integrity protection; a single character change produces a completely different decoded output.

Conclusion

Base64 encoding is one of the most fundamental encoding schemes in computing. Whether you're embedding images in HTML, working with JWT tokens, or handling email attachments, understanding how Base64 works and when to use it is essential. Try our online Base64 encoder for quick conversions, and keep the algorithm's 33% size overhead in mind when choosing it for large payloads.