How Base64 Encoding Works
Base64 encoding works by dividing input data into 3-byte (24-bit) chunks and converting each chunk into four 6-bit Base64 characters.
The Base64 Alphabet:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
Padding: If the input length is not divisible by 3, padding characters (=) are added at the end.
Example: “M” (77 in ASCII) = 01001101 in binary = TQ==
Common Base64 Encoding Examples
Here are some common Base64 encoding examples:
| Original Text | Base64 Encoded | Notes |
|---|---|---|
Hello |
SGVsbG8= |
Simple text with padding |
World |
V29ybGQ= |
Five-character word |
Hello World! |
SGVsbG8gV29ybGQh |
Includes a space and punctuation |
Base64 |
QmFzZTY0 |
No padding needed |
user:password |
dXNlcjpwYXNzd29yZA== |
Basic Auth style value |
{"ok":true} |
eyJvayI6dHJ1ZX0= |
Small JSON payload |
Common Use Cases for Base64
- Email Attachments: Base64 is used in MIME email format to encode binary attachments
- Data URLs: Embed small images in HTML/CSS using data:image/png;base64,…
- API Authentication: Many APIs use Base64 for Basic Auth (username:password)
- Configuration Files: Store binary data or special characters in JSON/XML configs
- Web Development: Encode data payloads in AJAX requests
Base64 decoding errors and safe usage tips
Base64 is an encoding format, not encryption. Anyone with the encoded value can decode it, so do not use Base64 alone to protect passwords, private keys, tokens, or confidential data.
If a Base64 value does not decode, check for missing characters, copied line breaks, URL-safe variants that use - and _, or missing = padding at the end.
- Use Base64 when binary data must travel through a text-only system.
- Avoid Base64 for large images in production pages because it increases size and can reduce caching efficiency.
- Use URL encoding instead when the goal is only to make query parameters safe inside a URL.