Base64 Encoding Cheatsheet
A quick reference for Base64 encoding rules, alphabet, variants (standard vs URL-safe), padding, and common use cases.
Try the Base64 Encoder / DecoderSections
The Base64 Alphabet
Base64 encodes every 3 bytes of input into 4 printable ASCII characters drawn from a 64-character alphabet.
| Index | Char | Index | Char |
|---|---|---|---|
0–25 | A–Z | 26–51 | a–z |
52–61 | 0–9 | 62 | + (standard) or - (URL-safe) |
63 | / (standard) or _ (URL-safe) | pad | = (padding character) |
Encoding Rules
| Rule | Detail |
|---|---|
Group into 3-byte chunks | Take 3 bytes (24 bits) of input at a time |
Split into four 6-bit groups | Each 6-bit group indexes into the 64-char alphabet |
Padding with = | If input length is not divisible by 3, pad output with = to make it divisible by 4 |
1 remaining byte | Produces 2 Base64 chars + == padding |
2 remaining bytes | Produces 3 Base64 chars + = padding |
Output size | ⌈n / 3⌉ × 4 characters — always a multiple of 4 |
Standard vs URL-Safe
| Variant | Char 62 | Char 63 | Padding | Use Case |
|---|---|---|---|---|
Standard (RFC 4648 §4) | + | / | = required — MIME, PEM, general encoding | |
URL-safe (RFC 4648 §5) | - | _ | = often omitted — JWTs, URL query params, filenames |
Common Use Cases
| Use Case | Notes |
|---|---|
Data URIs | data:image/png;base64,<encoded> — embed images in HTML/CSS |
JWTs | Header and payload are Base64URL-encoded (no padding) |
HTTP Basic Auth | Authorization: Basic base64(username:password) |
Email attachments | MIME uses standard Base64 with 76-char line wrapping |
Binary in JSON | Encode binary blobs before placing in JSON strings |
Secrets in env vars | Base64 makes binary keys safe to store as strings |
Quick Examples
| Input (ASCII) | Base64 Output |
|---|---|
M | TQ== |
Ma | TWE= |
Man | TWFu |
hello | aGVsbG8= |
hello world | aGVsbG8gd29ybGQ= |
{"alg":"HS256"} | eyJhbGciOiJIUzI1NiJ9 |