What is Base64 Encoding? Encoding vs Encryption
Learn what Base64 encoding is, how it works, and why it's different from encryption.
Base64 is everywhere in web development, but it's often misunderstood. Let's clear up what Base64 actually is.
What is Base64?
Base64 is a way to represent binary data using only 64 printable ASCII characters. It converts any file—an image, a PDF, raw bytes—into a text string that can be safely transmitted anywhere.
The 64 characters are: A-Z, a-z, 0-9, +, /
This scheme was created to solve a real problem: email used to only support text. If you wanted to attach a binary file, it would get corrupted. Base64 solved this by converting the binary into safe text.
How Base64 Works
Base64 works by grouping binary data into 6-bit chunks and mapping each chunk to one of the 64 safe characters.
Here's "Hello" step by step:
Original text: H e l l o
Step 1: Convert to binary
- H = 01001000
- e = 01100101
- l = 01101100
- l = 01101100
- o = 01101111
Step 2: Group into 6-bit chunks
- 010010 (18) → S
- 000110 (6) → G
- 010101 (21) → V
- 101100 (44) → s
- 110110 (54) → 2
- 111101 (61) → 9
- 11 (3, padded) → D (with padding)
Step 3: Output
SGVsbG8=
The = at the end is padding to make the length a multiple of 4.
Base64 is NOT Encryption
This is the biggest misunderstanding. Base64 is encoding, not encryption.
- **Encoding** is reversible transformation for transmission. Anyone with Base64 text can instantly decode it.
- **Encryption** is transformation that requires a secret key. Only someone with the key can decrypt it.
If you Base64 encode a password, anyone can decode it. If you encrypt a password with a proper encryption algorithm, only the holder of the key can decrypt it.
When to Use Base64
Use Base64 when you need to:
- **Embed binary data in text**: Put an image directly in JSON
- **Transmit binary data safely**: Send files over channels that only support text
- **Store binary in URLs**: Encode a binary value for a query parameter
- **Encode credentials**: In HTTP Basic Auth (though this is not secure without HTTPS)
- **Data URIs**: Embed images directly in HTML/CSS
Example - embedding an image in JSON:
{
"name": "logo.png",
"data": "iVBORw0KGgoAAAANSUhEUgAAAAUA..."
}
When NOT to Use Base64
Do NOT use Base64 for:
- **Storing passwords**: Use a proper hash like bcrypt or scrypt
- **Securing data**: Encryption requires a key, Base64 doesn't
- **Hiding information**: Base64 is trivial to reverse
- **Compressing data**: Base64 actually increases size by 33%
Base64 Size Increase
Base64 encoding increases data size by about 33%.
- Original: 3 bytes
- Base64: 4 characters (plus padding)
If you're sending a 1MB image:
- Original: 1MB
- Base64: 1.33MB
This overhead is why efficient protocols use binary directly when possible. HTTP handles binary well. Only use Base64 when you need the data in text format.
Tools for Base64
DevToolkit has a Base64 encoder/decoder. Paste text or binary data and convert between representations instantly.
Summary
- Base64 transforms binary to safe text
- It's reversible, not encrypted
- Use it for transmission, not security
- It increases file size by 33%
- Never rely on Base64 for security