Base64 Decode Guide
Convert Base64 encoded strings back to readable text
What is Base64 Decoding?
Base64 decoding is the reverse process of Base64 encoding. It converts Base64-encoded ASCII text back to its original binary or text format. This is essential for reading data that was encoded for transmission or storage.
How Base64 Decoding Works
The decoding process reverses the encoding steps:
- Takes 4 Base64 characters
- Converts each character to its 6-bit value
- Combines 4 × 6 bits = 24 bits (3 bytes)
- Removes padding (=) and returns original data
Examples
Basic Text Decoding
SGVsbG8gV29ybGQ=
Hello World
Email Address
dXNlckBleGFtcGxlLmNvbQ==
user@example.com
Authentication Credentials
dXNlcm5hbWU6cGFzc3dvcmQ=
username:password
Common Use Cases
1. Reading Email Attachments
Decode Base64-encoded attachments from email messages:
// Email MIME part
Content-Transfer-Encoding: base64
SGVsbG8gV29ybGQ=
// Decode to get original file2. Processing Data URIs
Extract image or file data from data URIs:
// Extract Base64 from data URI
const dataURI = "data:image/png;base64,iVBORw0KGgo...";
const base64 = dataURI.split(',')[1];
const decoded = atob(base64); // Binary image data3. API Response Processing
Decode Base64 data received from APIs:
{
"filename": "report.pdf",
"content": "JVBERi0xLjQKJeLjz9MK..."
}
// Decode content to get actual file4. Reading JWT Tokens
Decode JWT (JSON Web Token) payloads:
// JWT format: header.payload.signature
const jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0...";
const parts = jwt.split('.');
const payload = JSON.parse(atob(parts[1]));
console.log(payload); // {"sub":"1234567890"}Programming Examples
JavaScript
// Browser (built-in)
const decoded = atob('SGVsbG8gV29ybGQ=');
console.log(decoded); // Hello World
// Node.js
const decoded = Buffer.from('SGVsbG8gV29ybGQ=', 'base64').toString('utf-8');
console.log(decoded); // Hello WorldPython
import base64
encoded = "SGVsbG8gV29ybGQ="
decoded = base64.b64decode(encoded)
print(decoded.decode('utf-8')) # Hello WorldPHP
$encoded = "SGVsbG8gV29ybGQ=";
$decoded = base64_decode($encoded);
echo $decoded; // Hello WorldJava
import java.util.Base64;
String encoded = "SGVsbG8gV29ybGQ=";
byte[] decodedBytes = Base64.getDecoder().decode(encoded);
String decoded = new String(decodedBytes);
System.out.println(decoded); // Hello WorldError Handling
Invalid Base64 Strings
Not all strings are valid Base64. Watch for these issues:
- Invalid characters: Only A-Z, a-z, 0-9, +, /, = are allowed
- Wrong length: Must be a multiple of 4 (with padding)
- Incorrect padding: = padding must be at the end
// JavaScript error handling
try {
const decoded = atob('Invalid@Base64!');
} catch (error) {
console.error('Invalid Base64 string:', error.message);
}Security Considerations
🔒 Security Warnings
- Not encryption: Base64 is easily reversible - don't trust it for security
- Validate decoded data: Always sanitize/validate after decoding
- Injection attacks: Decoded data may contain malicious code
- Size bombs: Small Base64 can decode to huge files (ZIP bombs)
Best Practices
- Validate input: Check if string is valid Base64 before decoding
- Handle errors: Use try-catch blocks for decoding operations
- Character encoding: Specify UTF-8 when converting to strings
- Memory limits: Be cautious with large Base64 strings
- URL-safe variant: Use Base64url decoder for URL-encoded data
Common Errors and Solutions
"Invalid character" Error
Solution: Remove whitespace and newlines from Base64 string:
const base64 = input.replace(/\s/g, '');
const decoded = atob(base64);"Invalid length" Error
Solution: Ensure proper padding:
// Add padding if missing
while (base64.length % 4 !== 0) {
base64 += '=';
}Try It Yourself
Use our free Base64 decoder to convert encoded strings instantly:
Related Encoding Tools
- Base64 Encode - Encode text to Base64
- URL Decode - Decode URL-encoded text
- HTML Entity Decode - Decode HTML entities