๐ŸชบToolNest
developer7 min read

What Is Base64 Encoding? A Complete Guide

Learn how Base64 encoding works, why it exists, and when you should (and shouldn't) use it โ€” with a step-by-step example.

TN

ToolNest Team

February 15, 2026

#base64#encoding#developer tools

What Is Base64 Encoding?

Base64 is a binary-to-text encoding scheme that represents binary data using only 64 printable ASCII characters. The name comes from the fact that each Base64 digit represents exactly 6 bits of data (2โถ = 64 possible values).

The 64 characters used are: Aโ€“Z (26), aโ€“z (26), 0โ€“9 (10), + and / (2), with = used as padding. This makes Base64-encoded data safe to transmit in contexts that only support text โ€” like email, URLs, and HTML attributes.

Why Does Base64 Exist?

Many data transport systems were originally designed to handle text only. Email protocols like SMTP, for instance, were built around 7-bit ASCII text. Sending a binary file (like a JPEG image or PDF) directly through these systems would corrupt the data because certain byte values have special meanings as control characters.

Base64 solves this by converting any binary data into a pure-text representation that travels safely through text-only channels without corruption.

Common contexts where Base64 is essential:

  • Email attachments โ€” MIME email attachments are Base64-encoded before sending via SMTP
  • Data URIs โ€” Embedding images directly in HTML/CSS: src="data:image/png;base64,iVBORw..."
  • JWT tokens โ€” The header and payload sections of a JSON Web Token are Base64url-encoded
  • API responses โ€” When binary data must be included in a JSON payload
  • Basic HTTP Authentication โ€” Credentials are Base64-encoded in the Authorization header

How the Algorithm Works

Base64 works by taking every 3 bytes (24 bits) of input and converting them into 4 Base64 characters (each representing 6 bits).

Step-by-step example โ€” encoding "Cat":

  1. Convert each character to its ASCII byte value:

    • C = 67 = 01000011
    • a = 97 = 01100001
    • t = 116 = 01110100
  2. Concatenate the bits: 010000110110000101110100

  3. Split into 6-bit groups: 010000 | 110110 | 000101 | 110100

  4. Convert each 6-bit group to a decimal index:

    • 010000 = 16 โ†’ Q
    • 110110 = 54 โ†’ 2
    • 000101 = 5 โ†’ F
    • 110100 = 52 โ†’ 0
  5. Result: Q2F0

You can verify this: in JavaScript, btoa("Cat") returns "Q2F0".

Padding: When the input length isn't divisible by 3, padding characters (=) are added to make the output length a multiple of 4.

Size Overhead

Base64 increases data size by approximately 33% โ€” 3 bytes become 4 characters. A 1 MB binary file becomes roughly 1.33 MB when Base64-encoded. This is an important consideration when deciding whether to use it.

When NOT to Use Base64

Base64 is commonly misunderstood as a form of encryption โ€” it is not. Anyone can decode Base64 instantly. Never use Base64 to "hide" sensitive data.

Avoid Base64 when:

  • Storing passwords โ€” Use bcrypt, Argon2, or scrypt instead
  • Protecting sensitive data โ€” Use actual encryption (AES, RSA)
  • Large binary files over HTTP โ€” Send binary directly with the correct Content-Type header; Base64 adds 33% overhead
  • Database storage of binary data โ€” Most databases have native binary column types (BYTEA in PostgreSQL, BLOB in MySQL)

Base64 vs Base64url

Standard Base64 uses + and / characters, which have special meanings in URLs and filenames. Base64url is a variant that replaces + with - and / with _, making it safe for use in URLs and filenames without encoding. JWT tokens use Base64url for this reason.

Practical Usage

JavaScript:

// Encode
const encoded = btoa("Hello, World!");
// Decode
const decoded = atob(encoded);

// For binary data (files, ArrayBuffers) use Buffer in Node.js:
const buf = Buffer.from(binaryData);
const base64 = buf.toString('base64');

Python:

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

Use our free Base64 Encoder/Decoder tool to encode and decode Base64 strings instantly โ€” no installation needed.

Share this article

Try the Free Tool