Base64 is an encoding scheme that converts arbitrary binary data into a sequence of 64 printable ASCII characters. It was created to safely transmit binary content through systems — like email — that only handle text.
The core trick: take 3 bytes (24 bits), split them into 4 groups of 6 bits, and map each 6-bit value (0–63) to a character from the Base64 alphabet (A–Z, a–z, 0–9, +, /). Every 3 bytes of input become exactly 4 characters of output — a 33% size overhead.
Base64 is not encryption. It provides no confidentiality — anyone can decode it instantly. Its purpose is purely representational: making binary data safe to embed in text contexts.
| Base64 | Hex | Base58 | URL%enc | |
|---|---|---|---|---|
| Overhead | 33% | 100% | ~37% | up to 200% |
| URL-safe variant | Yes | Yes | Yes | Native |
| Human readable | No | Partial | Partial | No |
| Padding chars | = (1–2) | None | None | None |
| Whitespace safe | Yes | Yes | Yes | No |
| Common use | Widely | Debug | Crypto | URLs |
// Standard Base64
const encoded = Buffer.from('Hello, World!').toString('base64');
// → 'SGVsbG8sIFdvcmxkIQ=='
const decoded = Buffer.from(encoded, 'base64').toString('utf8');
// → 'Hello, World!'
// URL-safe Base64 (no + / or padding)
const urlSafe = Buffer.from('Hello, World!')
.toString('base64url');
// → 'SGVsbG8sIFdvcmxkIQ'
// Encode binary (e.g. random bytes for an API key)
import { randomBytes } from 'crypto';
const apiKey = randomBytes(32).toString('base64url');
import base64
# Standard Base64
encoded = base64.b64encode(b'Hello, World!')
# → b'SGVsbG8sIFdvcmxkIQ=='
decoded = base64.b64decode(encoded).decode('utf-8')
# → 'Hello, World!'
# URL-safe Base64 (replaces + → -, / → _)
url_safe = base64.urlsafe_b64encode(b'Hello, World!')
# → b'SGVsbG8sIFdvcmxkIQ=='
# Strip padding for tokens
token = base64.urlsafe_b64encode(
secrets.token_bytes(32)
).rstrip(b'=').decode()
import "encoding/base64"
// Standard Base64
encoded := base64.StdEncoding.EncodeToString([]byte("Hello, World!"))
// → "SGVsbG8sIFdvcmxkIQ=="
decoded, err := base64.StdEncoding.DecodeString(encoded)
// → []byte("Hello, World!")
// URL-safe Base64 (safe in URLs and filenames)
urlSafe := base64.URLEncoding.EncodeToString([]byte("Hello, World!"))
// No padding variant (common for tokens)
raw := base64.RawURLEncoding.EncodeToString([]byte("Hello, World!"))
// → "SGVsbG8sIFdvcmxkIQ"
Base64 is trivially reversible by anyone. Never use it to "hide" passwords, secrets, or sensitive data. Use proper encryption (AES-GCM) or hashing (bcrypt) for those.
Standard Base64 uses + and / which are special in URLs. Use base64url (replaces them with - and _) for JWTs, tokens, and anything in a URL or filename.
Some systems strip = padding; others require it. When comparing or parsing Base64 tokens, normalize padding first. RawEncoding variants omit it entirely.
Base64 inflates data by ~33%. Avoid repeatedly encoding large binaries (e.g. images) in tight loops. Encode once, cache, or stream. Use binary protocols (protobuf, MessagePack) when size matters.