Base64 Encoding Explained for Beginners
If you have ever inspected an HTTP request, read an email header, embedded an image in HTML, or worked with JWTs, you have encountered Base64 encoding. It appears everywhere in software β yet many developers use it without fully understanding what it does or why it exists. This guide demystifies Base64 from first principles.
1. What Problem Does Base64 Solve?
Computers store everything as binary data. When binary data needs to travel through systems designed for text (HTTP headers, email, XML, JSON), raw bytes can be misinterpreted or corrupted. Old email protocols like SMTP were designed for 7-bit ASCII text β sending a binary image directly would corrupt it because many byte values have special meanings in ASCII.
Base64 solves this by mapping arbitrary binary data to a safe, printable ASCII character set that can pass through any text-based medium without alteration.
2. The Base64 Character Set
A-Z (26 uppercase) β values 0β25 a-z (26 lowercase) β values 26β51 0-9 (10 digits) β values 52β61 + (plus) β value 62 / (slash) β value 63 = (equals) β padding (not a value)
Since 2^6 = 64, each Base64 character encodes exactly 6 bits. Every 3 input bytes (24 bits) become 4 Base64 characters β a 33% size overhead.
3. How the Algorithm Works
Encoding "Man" step by step:
M=77β01001101 a=97β01100001 n=110β01101110 Concatenated: 010011010110000101101110 Split 6-bit: 010011 | 010110 | 000101 | 101110 Values: 19 22 5 46 Base64: T W F u Result: "Man" β "TWFu"
Padding: if input is 1 byte, append ==; if 2 bytes, append =.
"M" β "TQ==" "Ma" β "TWE=" "Man" β "TWFu"
4. Common Use Cases
Email attachments (MIME)
MIME encodes binary attachments as Base64 so they survive text-only SMTP servers.
Data URIs in HTML/CSS
Embed small images inline: <img src="data:image/png;base64,iVBOR..."/> eliminating HTTP requests.
JWT tokens
JWT sections (header.payload.signature) are Base64URL-encoded, a URL-safe variant replacing + with - and / with _.
HTTP Basic Auth
Authorization: Basic encodes "user:password" as Base64.
Binary data in JSON/XML
Embed file contents (PDFs, images) inside JSON API payloads as Base64 strings.
5. Code Examples
JavaScript
const encoded = btoa('Hello, World!');
// SGVsbG8sIFdvcmxkIQ==
const decoded = atob('SGVsbG8sIFdvcmxkIQ==');
// Hello, World!Python
import base64
encoded = base64.b64encode(b"Hello, World!")
# b'SGVsbG8sIFdvcmxkIQ=='
decoded = base64.b64decode('SGVsbG8sIFdvcmxkIQ==')
# b'Hello, World!'6. Security Warning
β οΈ Base64 is NOT encryption
Base64 is fully reversible with zero key β anyone can decode it instantly. Never use it to hide passwords, API keys, or sensitive data. Use proper encryption (AES-256, RSA) for that purpose.
7. Conclusion
Base64 is a fundamental encoding scheme for transferring binary data through text-based systems. Understanding its algorithm, 33% size overhead, and the critical distinction from encryption helps you use it correctly. Try our free Base64 encoder/decoder β all processing is client-side, zero uploads.