Base64 Encoding: How It Works and When to Use It

Understand Base64 encoding and decoding — data URIs, API payloads, email attachments, and binary-to-text conversion.

6 min read Encoding · Web · APIs 6 sections + FAQ

Base64 is a binary-to-text encoding scheme that converts arbitrary binary data into a string of 64 printable ASCII characters. It does not encrypt — it only encodes. The goal is to represent binary data in a form that can safely travel through text-based channels.

You encounter Base64 everywhere: data URIs in CSS, JWT tokens, email attachments, API request bodies. Understanding how and when to use it prevents common mistakes like double-encoding or encoding large files that should be stored as URLs.

What is Base64 encoding

Base64 is defined in RFC 4648. It represents binary data using 64 characters: A–Z (26), a–z (26), 0–9 (10), + and / (2). The = character is used for padding. Every 3 bytes of binary input produce 4 Base64 characters — a 4/3 (33%) size overhead. The name "Base64" refers to the 64-character alphabet used, analogous to Base16 (hex) and Base2 (binary).

Free Tool Base64 Encoder / Decoder Encode and decode Base64 strings instantly in your browser

How Base64 works (the algorithm)

The encoding algorithm splits the input bytes into 6-bit groups (64 = 2^6). Each 6-bit group maps to one character in the alphabet. Three input bytes (24 bits) produce four 6-bit groups, hence four output characters. If the input length is not a multiple of 3, padding characters (=) are appended to make the output length a multiple of 4. Decoding reverses the process: each character maps back to 6 bits, groups of 4 characters decode to 3 bytes.

Base64 in data URIs

A data URI embeds file content directly in a URL. The format is data:[mediatype];base64,[data]. This allows images, fonts, and other assets to be inlined in HTML or CSS without separate HTTP requests. For small files (under 5KB), the eliminated request outweighs the 33% size overhead. For larger files, separate URLs with CDN caching are more efficient.

<!-- Inline image as data URI -->
<img src="data:image/png;base64,iVBORw0KGgo..." />

/* Inline font in CSS */
@font-face {
  src: url('data:font/woff2;base64,d09GMgAB...');
}
Free Tool Image to Base64 Encoder Convert any image to a Base64 data URI for inline embedding

Base64 in APIs and JSON payloads

JSON only supports text — binary data cannot be directly embedded. Base64 solves this: binary content (images, PDFs, audio) is encoded to a Base64 string and included as a JSON field. Common patterns: OpenAI Vision API accepts base64-encoded images, email sending APIs accept base64-encoded attachments, and webhook payloads often include base64-encoded file content. Always document whether your API endpoint expects raw binary or Base64 in HTTP headers.

Base64 in email (MIME)

Email was originally designed for 7-bit ASCII text. Binary attachments (images, PDFs, executables) must be encoded for transmission. MIME (Multipurpose Internet Mail Extensions) uses Base64 as the standard encoding for attachments. The Content-Transfer-Encoding: base64 header signals that the body is Base64-encoded. Most email clients handle this automatically, but understanding it helps debug malformed attachments.

Encoding vs encryption

Base64 is encoding, not encryption. It provides no security — any Base64 string can be trivially decoded. Never use Base64 to "hide" sensitive data. For security, use encryption (AES, RSA) which requires a key to decode. Base64 is sometimes confused with encryption because encoded strings look scrambled. The distinction matters critically for security-sensitive applications.

Free Tool JSON Formatter & Validator Format, validate and minify JSON data with syntax highlighting

Frequently Asked Questions

What is Base64 used for? +
Base64 is used to encode binary data as text so it can safely travel through text-based channels: data URIs in HTML/CSS, JSON API payloads, email attachments (MIME), JWT tokens, and HTTP Basic Authentication headers.
How much does Base64 increase file size? +
Base64 adds approximately 33% overhead. Every 3 bytes of binary become 4 Base64 characters. A 10KB file becomes ~13.3KB encoded. This is the trade-off for text compatibility.
Is Base64 the same as encryption? +
No. Base64 is encoding — it is completely reversible without any key. Anyone can decode a Base64 string. Encryption requires a secret key to decode. Never use Base64 for security.
What is URL-safe Base64? +
Standard Base64 uses + and / characters which have special meaning in URLs. URL-safe Base64 (defined in RFC 4648) replaces + with - and / with _, making the output safe for URL paths and query parameters without percent-encoding.
How do I decode a Base64 string? +
In JavaScript: atob(base64String) decodes a Base64 string to binary. In Node.js: Buffer.from(base64String, "base64").toString(). In Python: base64.b64decode(base64String). Online tools like the PixCode Base64 tool decode instantly.
What is the difference between Base64 and Base64URL? +
Base64URL (RFC 4648 Section 5) uses - instead of + and _ instead of /, and omits padding (=). It is used in JWT tokens, OAuth codes, and URL parameters where standard Base64 characters would need percent-encoding.
Can I encode any file with Base64? +
Yes. Base64 is format-agnostic — it encodes raw bytes regardless of the file type. The encoded output is always plain ASCII text. The MIME type or file extension tells the receiver how to interpret the decoded bytes.