Base64 Encode Guide
Convert text and binary data to Base64 format for safe transmission and storage
What is Base64 Encoding?
Base64 encoding converts binary data into ASCII text format using 64 printable characters (A-Z, a-z, 0-9, +, /). It's widely used for transmitting binary data over text-based protocols like email, JSON, and XML.
How Base64 Encoding Works
Base64 encoding converts every 3 bytes (24 bits) of binary data into 4 ASCII characters:
- Takes 3 bytes (24 bits) of input
- Splits into 4 groups of 6 bits each
- Maps each 6-bit group to a Base64 character
- Adds padding (=) if needed to make output length a multiple of 4
Examples
Basic Text Encoding
Hello World
SGVsbG8gV29ybGQ=
Email Address
user@example.com
dXNlckBleGFtcGxlLmNvbQ==
Special Characters
Hello! @#$%
SGVsbG8hIEAjJCU=
Common Use Cases
1. Email Attachments
Email protocols (SMTP) only support ASCII text. Base64 encoding allows binary files to be sent as text:
Content-Type: image/png; name="logo.png"
Content-Transfer-Encoding: base64
iVBORw0KGgoAAAANSUhEUgAAAAUA...2. Data URIs in HTML/CSS
Embed images directly in HTML or CSS without separate files:
<img src="data:image/png;base64,iVBORw0KGgo..." />
.logo {
background-image: url('data:image/svg+xml;base64,PHN2Zy...');
}3. API Authentication
HTTP Basic Authentication uses Base64 to encode credentials:
// Encode username:password
const credentials = btoa('username:password');
// Result: dXNlcm5hbWU6cGFzc3dvcmQ=
// HTTP header
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=4. JSON Data Transmission
Send binary data in JSON format:
{
"filename": "document.pdf",
"content": "JVBERi0xLjQKJeLjz9MKMSAwIG9iago8P..."
}5. Storing Binary in Databases
Store images or files in text-based database fields:
INSERT INTO files (name, data)
VALUES ('avatar.png', 'iVBORw0KGgoAAAANSUhEU...');Programming Examples
JavaScript
// Browser (built-in)
const encoded = btoa('Hello World');
console.log(encoded); // SGVsbG8gV29ybGQ=
// Node.js
const encoded = Buffer.from('Hello World').toString('base64');
console.log(encoded); // SGVsbG8gV29ybGQ=Python
import base64
text = "Hello World"
encoded = base64.b64encode(text.encode('utf-8'))
print(encoded.decode('utf-8')) # SGVsbG8gV29ybGQ=PHP
$text = "Hello World";
$encoded = base64_encode($text);
echo $encoded; // SGVsbG8gV29ybGQ=Important Notes
⚠️ Security Warning
- Base64 is NOT encryption - it's just encoding
- Anyone can decode Base64 data easily
- Never use Base64 alone for password storage
- Use proper encryption (AES, RSA) for sensitive data
Size Increase
Base64 encoding increases data size by approximately 33% because it converts 3 bytes into 4 characters.
- Original: 3 bytes = 24 bits
- Encoded: 4 characters = 32 bits
- Overhead: 33% increase
Best Practices
- Use for text protocols: Ideal when transmitting binary over text-based systems
- Consider alternatives: For large files, direct binary transfer may be more efficient
- Handle padding: Ensure proper = padding for compatibility
- UTF-8 encoding: Always encode text as UTF-8 before Base64 encoding
- URL-safe variant: Use Base64url for URLs (replaces + with - and / with _)
Try It Yourself
Use our free Base64 encoder to convert your text instantly:
Related Encoding Tools
- Base64 Decode - Decode Base64 back to text
- URL Encode - Encode text for URLs
- HTML Entity Encode - Encode HTML special characters