What is Base64?
Binary to Text Magic
Safe Travel for Data
Base64 is a way to turn binary data (images, files) into safe, printable text (A-Z, 0-9). It's used because some systems (like email) are designed for text and might break if you send raw binary bits. It makes files ~33% larger but ensures they arrive safely.
# In this article
1. The Core Concept
Computers store everything as 1s and 0s (binary). But the internet used to rely heavily on systems that were designed only for text (like ASCII).
If you try to open an image file in Notepad, you see garbage characters. If you tried to email that "raw" image data in the 90s, the email servers might interpret a random byte as a "End of File" command and cut off your attachment.
Base64 solves this by determining a set of 64 safe characters that are common to all character sets and won't confuse any legacy system.
2. The 64 Characters
To represent any possible byte of data using only text, Base64 uses this specific index table:
| Index | Char | Index | Char | Index | Char | Index | Char |
|---|---|---|---|---|---|---|---|
| 0-25 | A-Z | 26-51 | a-z | 52-61 | 0-9 | 62, 63 | + / |
It uses A-Z, a-z, 0-9, plus + and /.
3. Step-by-Step Example
Let's trace exactly how the text "Man" becomes "TWFu".
Step 1: Convert to ASCII Values
Step 2: Convert to 8-bit Binary
Total 24 bits.
Step 3: Re-group into 6-bit chunks
Base64 consumes 6 bits at a time (2^6 = 64). We split the 24 bits into 4 groups of 6.
4. Why the "=" Sign?
The algorithm needs groups of 3 bytes (24 bits) to work perfectly. But what if your file is only 1 byte long?
- Padding: We add
=characters to tell the decoder "ignore these last few bits, they are just empty filler". - Rule: The output length of a Base64 string must always be a multiple of 4.
5. URL Safe Base64
Standard Base64 uses + and /. These are dangerous in URLs because / is a directory separator and + can be interpreted as a space in some scenarios.
Base64URL replaces + with - (minus) and / with _ (underscore), and removes the = padding completely.
This is heavily used in JWTs (JSON Web Tokens) to safely pass user session data in URL parameters.
6. Pros & Cons
✅ Pros
- • Sends binary data through text-only systems (Email, JSON, XML).
- • Embeds small images directly in CSS/HTML (fewer HTTP requests).
- • Standardized and supported everywhere.
❌ Cons
- • Size Increase: Increases file size by ~33%. (4 chars for every 3 bytes).
- • Computation: Requires CPU encoding/decoding on both ends.
- • No Security: It can be decoded by anyone easily.
7. It is NOT Encryption
⚠️ Important Warning
Base64 is encoding, not encryption.
Never use Base64 to "hide" passwords or secrets. It takes milliseconds to decode. It is like writing your diary in Pig Latin — it's not secret, just obfuscated.