Base64 Encoding Explained: Everything You Need to Know
Understand Base64 encoding, how it works, when to use it, and common use cases in web development.
Base64 is a binary-to-text encoding scheme that represents binary data using 64 ASCII characters. It's essential for transmitting binary data over text-based protocols like HTTP, email, and JSON.
How Base64 Works
Base64 takes 3 bytes (24 bits) of input and splits them into 4 groups of 6 bits each. Each 6-bit value maps to a character: A-Z, a-z, 0-9, +, and /. The result increases data size by approximately 33%.
Common Use Cases
- Data URIs: Embedding images in CSS/HTML using data:image/png;base64,...
- Email attachments: MIME encoding uses Base64 for binary attachments
- HTTP Basic Auth: Credentials are Base64-encoded in the Authorization header
- JWT tokens: Header and payload sections are Base64URL-encoded
- Binary data in JSON: Sending binary data through JSON APIs
Base64 vs Base64URL
Standard Base64 uses + and / which have special meaning in URLs. Base64URL replaces + with - and / with _, making it safe for URLs. JWT tokens use Base64URL encoding.
Security Note
Base64 is encoding, not encryption. It provides zero security. Never use Base64 to protect sensitive data. Use proper encryption (AES) or hashing (bcrypt, SHA-256) for security.
