Base64 Encoding: What It Is and When to Use It
Base64 converts binary data into ASCII text. It's not encryption — it's encoding. Anyone with the Base64 string can decode it back. Base64 is used when binary data needs to be in a text context:
How Base64 Works
Base64 takes 3 bytes of binary data (24 bits) and splits them into 4 groups of 6 bits. Each 6-bit group maps to a character in the alphabet A-Za-z0-9+/. = pads the end. This means every 3 bytes becomes 4 characters — a 33% size increase.
Common Use Cases
- Data URLs —
data:image/png;base64,...embeds images in CSS/HTML without HTTP requests - Email attachments — SMTP only handles text, so attachments are Base64-encoded
- JWT tokens — the payload in a JWT is Base64URL-encoded
- API keys in headers — Basic Auth sends
user:passas Base64
When NOT to Use Base64
- Image optimization — Base64 images in CSS increase HTML size by 33%. Large images should be loaded as files
- Security — Base64 is encoding, not encryption. Never use it to "hide" data
- Large files — 33% size overhead. Send binary via multipart upload instead