What is Base64 Encoding and How Does It Work?
Base64 is a binary-to-text encoding algorithm specified in RFC 4648. It is designed to safely transmit arbitrary binary data (images, cryptographic keys, authentication tokens) across text-only protocols like HTTP, SMTP email, and JSON without data corruption or escaping issues.
The Radix-64 Algorithm Step-by-Step
1. Every 3 bytes of raw binary data (24 bits) is partitioned into 4 groups of 6 bits each.
2. Each 6-bit index (ranging from 0 to 63) maps to an ASCII character in the 64-character table: A-Z (0-25), a-z (26-51), 0-9 (52-61), + (62), and / (63).
3. If the input byte length is not divisible by 3, trailing padding characters (=) are appended.
Standard vs. URL-Safe Base64
| Variant | Index 62 Character | Index 63 Character | Padding | Common Usage |
|---|---|---|---|---|
| Standard Base64 | + |
/ |
= |
MIME email attachments, Basic HTTP Auth |
| URL-Safe Base64 | - (hyphen) |
_ (underscore) |
Optional / none | JWT (JSON Web Tokens), URL query parameters |