Base64 Encoding Explained
What is Base64?
Base64 is a binary-to-text encoding scheme that converts binary data into a string of ASCII characters. It uses 64 characters: A-Z, a-z, 0-9, +, and /. It is used to embed binary data in text-based formats like JSON, HTML, and email.
How It Works
Every 3 bytes of input are encoded into 4 Base64 characters, increasing size by ~33%. Padding (=) is added when the input length is not a multiple of 3.
Input: Hello Binary: 01001000 01100101 01101100 01101100 01101111 Base64: SGVsbG8=
Common Use Cases
- Embedding images in CSS or HTML as data URIs
- Encoding binary data in JSON API payloads
- Email attachments (MIME encoding)
- Basic HTTP authentication headers
Common Mistakes
- Using Base64 for encryption: Base64 is encoding, not encryption. Anyone can decode it.
- URL-unsafe characters: Standard Base64 uses + and / which need URL-encoding. Use Base64URL variant instead.
- Ignoring size increase: Base64 adds ~33% overhead. Don't use for large files unnecessarily.
Code Examples
// JavaScript
btoa("Hello World") // "SGVsbG8gV29ybGQ="
atob("SGVsbG8gV29ybGQ=") // "Hello World"
// Node.js
Buffer.from("Hello").toString("base64") // "SGVsbG8="
Buffer.from("SGVsbG8=", "base64").toString() // "Hello"Try our Base64 Encoder/Decoder to convert text or data instantly.
FAQ
Is Base64 secure?
No. Base64 is easily reversible, it provides obfuscation, not security.
What is Base64URL?
A URL-safe variant that replaces + with - and / with _, and omits padding. Used in JWTs and URL parameters.