What Is Base64 Encoding?
Base64 is a binary-to-text encoding scheme that represents arbitrary binary data using only 64 printable ASCII characters — the uppercase letters A-Z, lowercase a-z, digits 0-9, plus the two symbols + and /. A = character is used as padding when the input length is not a multiple of 3 bytes. Together these 65 symbols (64 data + 1 padding) form the standard Base64 alphabet defined in RFC 4648.
The purpose of Base64 is transport: many older protocols (email SMTP, HTTP headers, URL query strings, JSON) were designed to carry only text. If you need to move binary data — an image, a PDF, an encrypted blob — through one of these text-only channels, you must first encode it as text. Base64 is the near-universal choice because every byte maps to a predictable, ASCII-safe character.
How Base64 Encoding Works Under the Hood
Base64 takes 3 bytes (24 bits) at a time and splits them into four 6-bit groups. Each 6-bit group indexes into the 64-character alphabet, producing one Base64 character. So 3 bytes of input become 4 characters of output — the source of the classic 4:3 size expansion (33% larger).
When the input length is not divisible by 3, the encoder pads the final block with zero bits and appends = characters to the output so the final length is always a multiple of 4. A single trailing = means the input was 2 bytes short of a full block; two trailing == means the input was 1 byte short of a block.
Base64 Encoding in Different Languages
JavaScript (browser)
// ASCII / Latin-1 only — will throw on emojis or Chinese
const encoded = btoa('Hello, World!');
console.log(encoded); // "SGVsbG8sIFdvcmxkIQ=="
// UTF-8 safe (recommended for real apps)
function utf8ToBase64(str) {
const bytes = new TextEncoder().encode(str);
const binString = String.fromCharCode(...bytes);
return btoa(binString);
}
console.log(utf8ToBase64('Hello 👋 世界'));
// "SGVsbG8g8J+RiyDkuJbnlYw="
// URL-safe variant
function utf8ToBase64Url(str) {
return utf8ToBase64(str)
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
}Node.js
// Node has a built-in Buffer for Base64
const encoded = Buffer.from('Hello, World!').toString('base64');
console.log(encoded); // "SGVsbG8sIFdvcmxkIQ=="
// From a file
import { readFileSync } from 'fs';
const fileB64 = readFileSync('./image.png').toString('base64');
// URL-safe variant
const urlSafe = Buffer.from('data').toString('base64url'); // Node 16+Python
import base64
# Encode a string
encoded = base64.b64encode('Hello, World!'.encode('utf-8'))
print(encoded.decode('ascii')) # "SGVsbG8sIFdvcmxkIQ=="
# Encode a file
with open('image.png', 'rb') as f:
file_b64 = base64.b64encode(f.read()).decode('ascii')
# URL-safe variant
url_safe = base64.urlsafe_b64encode(b'data').decode('ascii')Command Line
# macOS / Linux
echo -n "Hello, World!" | base64
# SGVsbG8sIFdvcmxkIQ==
# Encode a file
base64 image.png > image.b64
# Decode back
echo "SGVsbG8sIFdvcmxkIQ==" | base64 -dCommon Use Cases for Base64 Encoding
- Data URLs — Embed images or fonts directly in HTML/CSS:
data:image/png;base64,iVBORw0KGgo... - JSON APIs — Send binary files (images, PDFs, signed blobs) inside JSON request bodies
- HTTP Basic Auth — The
Authorization: Basic ...header uses Base64-encodeduser:pass - JWT tokens — All three JWT segments (header, payload, signature) are Base64URL encoded
- MIME email — Attachments in email are Base64-encoded so they survive text-based SMTP transport
- Config files — Store binary keys (SSH, TLS certs, secrets) as text in YAML, TOML, or ENV files
Base64 vs Base64URL — Which One Should I Use?
Standard Base64 uses + and /, both of which have special meaning in URLs (space and path separator). If you paste a standard Base64 string into a URL query parameter, it may get corrupted by URL-encoding. Base64URL solves this by substituting:
+→-/→_- The trailing
=padding is usually stripped
Use standard Base64 for data URLs, MIME email, HTTP Basic Auth, and general binary transport. Use Base64URL for JWT tokens, URL query strings, filenames, and anywhere the encoded string will appear in a URL.
Base64 Is NOT Encryption
This is the most important thing to know: Base64 provides zero security. Any programming language, browser DevTools, or online decoder will reverse the encoding instantly. If you Base64-encode a password or API key and put it in a config file, anyone with read access can decode it in one line of code.
For sensitive data: use a real encryption library (AES-256 via libsodium or Web Crypto SubtleCrypto), a secret manager (AWS Secrets Manager, HashiCorp Vault, Doppler), or environment variables loaded at runtime. Never rely on Base64 to hide anything.
Key Facts
- Alphabet:
- A-Z, a-z, 0-9, +, / (65 chars incl. = padding)
- Size overhead:
- Encoded output is ~33% larger than input
- Reversible:
- Yes — every Base64 string can be decoded back exactly
- Security:
- None — Base64 is encoding, not encryption
- Standard:
- RFC 4648 (also RFC 2045 for MIME)
- Privacy:
- All processing in-browser — data never leaves your device
Related Base64 Tools
- Base64 Encode String — focused guide on string-to-Base64 workflows
- Text to Base64 Converter — plain-text conversion with UTF-8 support
- Base64 Encode in JavaScript — btoa(), TextEncoder, and Node.js Buffer patterns
- Base64 Encode in Python — base64.b64encode with worked examples
- Base64 Decode — reverse the encoding back to original data
- Base64 Image Converter — encode images to data URLs