Skip to content
Developer 4 min read

Base64 Encoding Explained

What Base64 is, why it exists, common use cases (data URIs, JWT, email), Base64 vs Base64url, and why it is NOT encryption.

πŸ’‘ Best for: developers working with APIs, email systems, JWT tokens, or embedding images and files directly in HTML and CSS.

What is Base64?

Base64 is an encoding scheme that converts binary data into a string using 64 printable ASCII characters (A–Z, a–z, 0–9, +, /). It was designed to safely transmit binary data over text-based systems like SMTP email and HTTP headers that weren't built to handle raw bytes.

The name comes from the 64 characters used. Each character encodes 6 bits (2⁢ = 64). The output is always about 33% larger than the original.

How encoding works

  1. Take the binary input (bytes of an image, string, or file).
  2. Group into 3-byte chunks (24 bits each).
  3. Split each 24-bit chunk into four 6-bit groups.
  4. Map each 6-bit group to one of the 64 Base64 characters.
  5. Pad with = characters if the input length isn't divisible by 3.

Example: the string Hi! encodes to SGkh. You can verify this in ToolBite's Base64 encoder.

Common use cases

  • API authentication β€” Basic Auth sends username:password encoded in Base64 in the Authorization header.
  • JWT tokens β€” Header and payload sections are Base64url-encoded (a URL-safe variant).
  • Data URIs β€” Embedding small images directly in HTML: src="data:image/png;base64,iVBORw..."
  • Email attachments β€” MIME uses Base64 to encode binary attachments for SMTP transit.
  • Binary in JSON β€” JSON is text-only; binary data (files, images, keys) is Base64-encoded before storing in JSON fields.

Base64 is NOT encryption

⚠️ Base64 is reversible by anyone, instantly, with no key required. It provides zero security. Never use it to "hide" passwords, API secrets, or sensitive data.

Seeing a Base64 string doesn't mean the data is encrypted β€” it's just encoded for safe transport. Anyone can decode it in one second. For actual security use proper encryption (AES-256) or hashing (SHA-256, bcrypt for passwords).

Base64 vs Base64url

Comparison

  • Base64 uses + and / β€” these are special in URLs and need percent-encoding.
  • Base64url replaces + with - and / with _ β€” safe in URLs and HTTP headers without escaping.
  • JWT tokens use Base64url. Data URIs use standard Base64.

JavaScript quick reference

  • Encode: btoa(str) β€” works for ASCII strings only.
  • Decode: atob(str) β€” returns the original string.
  • For Unicode: btoa(unescape(encodeURIComponent(str))).
  • For files/binary: use FileReader.readAsDataURL().

Quick reference facts

  • Base64 output is ~33% larger than input.
  • Strings end with 0, 1, or 2 padding = characters.
  • Base64 β‰  encryption β€” never use it for security.
  • JWT tokens use Base64url (not standard Base64).

Tools used in this guide

Related guides & tools