What is Base64?
Base64 is a binary-to-text encoding scheme that represents binary data using a set of 64 printable ASCII characters. It's designed to carry binary data across channels that only reliably support text content, such as email systems and URLs.
The name "Base64" refers to its use of 64 distinct characters to represent data. This character set includes uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), and two additional characters (typically + and /), with = used for padding.
Why Base64 Exists
Many systems were designed to handle text, not arbitrary binary data. When you need to transmit binary data (images, files, encrypted content) through these text-based systems, problems arise:
- Character encoding issues: Different systems interpret byte values differently.
- Control characters: Binary data might contain characters that have special meaning (null bytes, line breaks).
- Data corruption: Some systems modify or strip certain byte values during transmission.
Base64 solves these problems by converting binary data into a safe subset of ASCII characters that pass through virtually any system unchanged.
How Base64 Works
The encoding process converts every 3 bytes (24 bits) of binary data into 4 Base64 characters (6 bits each):
The Base64 Alphabet
Index Char Index Char Index Char Index Char 0 A 16 Q 32 g 48 w 1 B 17 R 33 h 49 x 2 C 18 S 34 i 50 y 3 D 19 T 35 j 51 z 4 E 20 U 36 k 52 0 5 F 21 V 37 l 53 1 6 G 22 W 38 m 54 2 7 H 23 X 39 n 55 3 8 I 24 Y 40 o 56 4 9 J 25 Z 41 p 57 5 10 K 26 a 42 q 58 6 11 L 27 b 43 r 59 7 12 M 28 c 44 s 60 8 13 N 29 d 45 t 61 9 14 O 30 e 46 u 62 + 15 P 31 f 47 v 63 /
Encoding Example
Let's encode the text "Hi!" step by step:
Text: H i ! ASCII: 72 105 33 Binary: 01001000 01101001 00100001 Regroup into 6-bit chunks: 010010 000110 100100 100001 18 6 36 33 Base64: S G k h Result: "Hi!" → "SGkh"
Padding with =
When input length isn't divisible by 3, padding characters (=) are added:
"A" → "QQ==" (1 byte → 2 chars + 2 padding) "AB" → "QUI=" (2 bytes → 3 chars + 1 padding) "ABC" → "QUJD" (3 bytes → 4 chars, no padding)
Size Overhead
Base64 encoding increases data size by approximately 33%. This happens because:
- 3 bytes of binary data become 4 Base64 characters
- Each Base64 character is 1 byte (8 bits) but only carries 6 bits of information
- Ratio: 4/3 ≈ 1.33 (33% increase)
Common Use Cases
Email Attachments (MIME)
Email was designed for text. When you attach a file to an email, it's Base64 encoded to safely transmit through mail servers. The MIME standard uses Base64 for binary attachments.
Data URLs
Embed images directly in HTML or CSS without separate HTTP requests:
<img src="data:image/png;base64,iVBORw0KGgo..." />
.icon {
background-image: url(data:image/svg+xml;base64,PHN2Zy...);
}
API Data Transfer
When APIs need to include binary data in JSON (which only supports text), Base64 is the standard approach:
{
"filename": "document.pdf",
"content": "JVBERi0xLjQKJeLjz9..."
}
Basic Authentication
HTTP Basic Authentication encodes credentials in Base64 (though this is not encryption!):
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ= // Decodes to: username:password
Storing Binary in Databases
Some databases handle text better than binary. Base64 allows storing binary data in text fields when needed.
Base64 Variants
Several Base64 variants exist for different use cases:
Characters: A-Z, a-z, 0-9, +, / (padding: =)
Replaces + with - and / with _ for URL compatibility. Often omits padding.
Standard Base64 with line breaks every 76 characters (for email).
Base64 is NOT Encryption
Important: Base64 is an encoding scheme, not encryption. Anyone can decode Base64 data instantly. It provides zero security.
- Don't use Base64 to hide sensitive data
- Base64-encoded passwords are NOT secure
- For security, use proper encryption (AES, RSA) before Base64 encoding
Working with Base64
JavaScript
// Encode string to Base64
const encoded = btoa('Hello, World!'); // "SGVsbG8sIFdvcmxkIQ=="
// Decode Base64 to string
const decoded = atob('SGVsbG8sIFdvcmxkIQ=='); // "Hello, World!"
// For Unicode strings, use TextEncoder/TextDecoder
const encodeUnicode = (str) => btoa(String.fromCharCode(
...new TextEncoder().encode(str)
));
Python
import base64
# Encode
encoded = base64.b64encode(b'Hello, World!') # b'SGVsbG8sIFdvcmxkIQ=='
# Decode
decoded = base64.b64decode('SGVsbG8sIFdvcmxkIQ==') # b'Hello, World!'
# URL-safe variant
url_safe = base64.urlsafe_b64encode(b'data')
PHP
// Encode
$encoded = base64_encode('Hello, World!'); // "SGVsbG8sIFdvcmxkIQ=="
// Decode
$decoded = base64_decode('SGVsbG8sIFdvcmxkIQ=='); // "Hello, World!"
Command Line
# Linux/Mac
echo -n 'Hello' | base64 # Encode
echo 'SGVsbG8=' | base64 -d # Decode
# Windows PowerShell
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes('Hello'))
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('SGVsbG8='))
When NOT to Use Base64
- Large files: The 33% size increase matters. Use binary transfer when possible.
- Performance-critical applications: Encoding/decoding adds processing overhead.
- Security purposes: It's not encryption. Use proper cryptographic methods.
- When binary is supported: Modern protocols often support binary directly.
Key Takeaways
- Base64 converts binary data to text using 64 safe ASCII characters
- It increases data size by approximately 33%
- Common uses: email attachments, data URLs, JSON APIs, basic auth
- Base64 is encoding, NOT encryption—it provides no security
- URL-safe Base64 replaces + and / with - and _ for URL compatibility
- Every 3 bytes of input produces 4 Base64 characters
Encode and Decode Base64
Use our Base64 tool to encode text, decode Base64 strings, or convert files.
Open Base64 Tool →