1. What is Base64? Definition & Purpose
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format using a radix-64 representation. It's designed to carry data stored in binary format across channels that only reliably support text content.
๐ Info
๐ Historical Context
Base64 originated from the need to send binary data (like images or attachments) over email systems (MIME) that were designed only for 7-bit ASCII text. Today, it's used everywhere from data URIs to JWT tokens.
2. How Base64 Works: The Algorithm
The Base64 encoding algorithm works by converting 3 bytes (24 bits) of binary data into 4 ASCII characters (each 6 bits).
[01001011] [01101111] [01101101]
[010010] [110110] [111101] [101101]
18โS, 54โ2, 61โ9, 45โt
// Complete example: Encoding "Man"
Input: M a n
ASCII: 77 97 110
Binary: 01001101 01100001 01101110
6-bit: 010011 010110 000101 101110
Decimal: 19 22 5 46
Base64: T W F u
Output: "TWFu"
3. Base64 Alphabet & Character Set
Standard Base64 Alphabet: A-Z (26), a-z (26), 0-9 (10), + (1), / (1) = 64 characters
4. Padding Mechanism (= sign)
When the input data length is not a multiple of 3 bytes, Base64 adds padding to make the output a multiple of 4 characters.
Case 1: 1 byte (8 bits) remaining
Input: "M" (1 byte) Binary: 01001101 6-bit groups: 010011 01 After padding with zeros: 010011 010000 Decimal: 19 16 Base64: T Q Padding: == Output: "TQ=="
Case 2: 2 bytes (16 bits) remaining
Input: "Ma" (2 bytes) Binary: 01001101 01100001 6-bit groups: 010011 010110 0001 After padding: 010011 010110 000100 Decimal: 19 22 4 Base64: T W E Padding: = Output: "TWE="
5. Mathematical Explanation with Examples
// Encoding formula: Base64(x) = base64_alphabet[ (x >> (6*shift)) & 0x3F ]
// Example: Encoding "Hello"
Step 1: "Hello" โ ASCII: 72 101 108 108 111
Step 2: Group into 3-byte chunks
Chunk 1: 72, 101, 108
Chunk 2: 108, 111 (incomplete, will need padding)
Chunk 1 binary: 01001000 01100101 01101100
6-bit groups: 010010 000110 010101 101100
Decimal: 18 6 21 44
Base64: S G V s
Chunk 2 binary: 01101100 01101111
Add padding: 01101100 01101111 000000
6-bit: 011011 000110 111100 000000
Decimal: 27 6 60 0
Base64: b G 8 A
Padding: == (since only 2 bytes in last group)
Final: "SGVsbG8="
6. Base64 Variants: Standard, URL-safe, MIME, UTF-7
| Variant | Character Set | Padding | Use Case |
|---|---|---|---|
| Standard (RFC 4648) | A-Z a-z 0-9 + / | = | General purpose |
| Base64URL | A-Z a-z 0-9 - _ | Optional (often omitted) | URLs, JWT tokens |
| MIME (RFC 2045) | A-Z a-z 0-9 + / | = | Email attachments |
| UTF-7 | A-Z a-z 0-9 + / | None | Legacy email systems |
7. Common Use Cases with Real Examples
๐จ Data URIs (Embedding Images in HTML/CSS)
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA...">
๐ JWT Tokens
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U
JWT uses Base64URL (no padding, - instead of +, _ instead of /)
๐ง Email Attachments (MIME)
Content-Transfer-Encoding: base64 VGhpcyBpcyBhbiBleGFtcGxlIGF0dGFjaG1lbnQ=
๐ Basic Authentication Headers
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== // "Aladdin:open sesame" encoded
๐พ Storing Binary in JSON/XML
{
"id": 123,
"thumbnail": "iVBORw0KGgoAAAANSUhEUgAAAAUA..."
}
8. Size Overhead & Performance
Encoding Overhead Table
| Input Size | Base64 Size | Overhead |
|---|---|---|
| 3 bytes | 4 chars | 33% |
| 1 KB | ~1.33 KB | 33% |
| 1 MB | ~1.33 MB | 33% |
| 10 MB | ~13.33 MB | 33% |
Performance Characteristics
- Base64 encoding adds ~33% data overhead
- Encoding/decoding is very fast (~50-100 MB/s on modern CPUs)
- CPU cost is minimal compared to network/disk I/O
- For large files (>100 MB), consider streaming encoding
9. Implementation Guide (15+ Languages)
// JavaScript (Browser & Node.js)
// Encode
const encoded = btoa("Hello World"); // "SGVsbG8gV29ybGQ="
const decoded = atob(encoded); // "Hello World"
// Unicode support
const unicodeEncoded = btoa(unescape(encodeURIComponent("เคจเคฎเคธเฅเคคเฅ")));
const unicodeDecoded = decodeURIComponent(escape(atob(unicodeEncoded)));
// Python
import base64 encoded = base64.b64encode(b"Hello World").decode() # "SGVsbG8gV29ybGQ=" decoded = base64.b64decode(encoded).decode() # "Hello World" # URL-safe variant url_safe = base64.urlsafe_b64encode(b"Hello World")
// Java
import java.util.Base64;
String encoded = Base64.getEncoder().encodeToString("Hello World".getBytes());
byte[] decoded = Base64.getDecoder().decode(encoded);
// URL-safe
String urlSafe = Base64.getUrlEncoder().encodeToString(data);
// PHP
$encoded = base64_encode("Hello World"); // "SGVsbG8gV29ybGQ="
$decoded = base64_decode($encoded); // "Hello World"
// Go
import encoding/base64
encoded := base64.StdEncoding.EncodeToString([]byte("Hello World"))
decoded, _ := base64.StdEncoding.DecodeString(encoded)
// Rust
use base64::{Engine as _, engine::general_purpose};
let encoded = general_purpose::STANDARD.encode("Hello World");
let decoded = general_purpose::STANDARD.decode(&encoded).unwrap();
10. Base64 vs Base32 vs Base16 (Hex)
| Scheme | Alphabet Size | Efficiency | Overhead | Use Case |
|---|---|---|---|---|
| Base16 (Hex) | 16 | 4 bits/char | 100% | Debugging, color codes |
| Base32 | 32 | 5 bits/char | 60% | Case-insensitive, human-readable |
| Base64 | 64 | 6 bits/char | 33% | Most efficient, standard encoding |
| Base85 (Ascii85) | 85 | ~6.55 bits/char | ~25% | PDF files, high efficiency |
11. Security Considerations
โ ๏ธ Warning
โ ๏ธ Important: Base64 is NOT Encryption!
Base64 is encoding, not encryption. Anyone can decode it instantly. Never use Base64 for protecting sensitive data like passwords or API keys.
๐ก Pro Tip
๐ Security Best Practices
- Base64 adds no confidentiality - use encryption (AES, RSA) for secrets
- Beware of padding oracle attacks in some implementations
- Validate Base64 strings before decoding to avoid memory issues
- Use constant-time comparison for decoded values when security-sensitive
- For large inputs, use streaming decoders to avoid memory bombs
12. Base64URL: The Web-Safe Variant
Standard Base64 uses + and / characters which have special meaning in URLs. Base64URL replaces them with - and _ and often omits padding.
Standard: "Hello World" โ "SGVsbG8gV29ybGQ="
Base64URL: "Hello World" โ "SGVsbG8gV29ybGQ" (no padding, no / or +)
// Conversion function
function toBase64Url(base64) {
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
Try Base64 Encoder/Decoder
Convert text and files to Base64 with our professional tool
Launch Base64 Tool โ