Base64 Encoding: How and Why It Works
2025-09-25
Introduction
Ever noticed strange strings like aGVsbG8gd29ybGQ= in JSON APIs or JWT tokens? Thatโs Base64 encoding, a scheme for safely representing binary data in text form.
๐ Experiment online: Free Online Utils โ Base64 Encoder/Decoder
Why Do We Need Base64?
Many protocols (HTTP headers, SMTP email) were designed for ASCII text, not binary. Base64 encodes binary data into ASCII characters [A-Z][a-z][0-9]+/ plus = for padding.
The Algorithm
- Take 3 bytes (24 bits).
- Split into 4 groups of 6 bits.
- Map each 6-bit group to a Base64 character.
- If input isnโt multiple of 3, add
=padding.
Example: "hello"
- Binary โ groups โ "aGVsbG8="
Code Examples
Python
import base64
print(base64.b64encode(b"hello world").decode())
# aGVsbG8gd29ybGQ=
Javascript
btoa("hello world"); // "aGVsbG8gd29ybGQ="
Variants
- Base64 URL-safe: replaces + โ -, / โ _.
- Base32: limited alphabet, used in DNS.
- Base58: removes lookalike chars, used in Bitcoin addresses.
Applications
- Email attachments (MIME).
- JSON Web Tokens (JWT).
- Data URLs in HTML/CSS.
- Storing small blobs in databases.
Limitations
- Size overhead (~33% larger).
- Not encryption (just encoding).