Base64 Encoding Explained: Why It Exists and When to Use It
Base64 solves a deceptively simple problem: how do you transport binary data through a system that was designed for text? The web, email, and many APIs were originally built on protocols that only reliably handle 7-bit ASCII characters. When you need to send binary data — images, certificates, arbitrary bytes — through these channels, you need an encoding that converts binary to a safe subset of text. Base64 is that encoding.
The name comes from the 64 characters used: A–Z (26), a–z (26), 0–9 (10), + (1), / (1). Every 3 bytes of binary input becomes 4 Base64 characters. If the input isn't divisible by 3, padding characters (=) are added to complete the last group.
Why Base64 Was Invented
SMTP (email) was designed to transfer 7-bit ASCII text. The 8th bit of each byte was used for line signalling on some older networks, meaning binary data would be corrupted in transit. MIME (Multipurpose Internet Mail Extensions), introduced in 1992, added Base64 as the standard way to encode binary attachments in email. Every time you receive a PDF or image attachment in your email, it was Base64-encoded during transfer and decoded by your email client.
The same problem exists in HTTP. While modern HTTP handles binary just fine, many APIs, configuration files, and data interchange formats are text-based (JSON, XML, YAML). When you need to embed binary data in JSON — say, a file upload or a cryptographic key — Base64 is the standard approach.
Data URIs: Embedding Binary in HTML and CSS
Data URIs use Base64 to embed binary resources directly in HTML or CSS. The format is:
data:[mediatype][;base64],[data] For example, to embed a small PNG icon directly in an <img> tag:
<img src="data:image/png;base64,iVBORw0KGgoAAAANS..." /> This eliminates an HTTP request for the resource. The trade-off is a ~33% size increase and the inability to cache the resource independently. Data URIs are appropriate for small inline icons (under ~5KB) but counterproductive for larger images.
JWTs and Base64url
JSON Web Tokens (JWTs) use a variant called Base64url. Standard Base64 uses + and / — characters that are meaningful in URLs. Base64url replaces them with - and _ respectively, making the token safe to use directly in a URL without percent-encoding.
A JWT has three Base64url-encoded parts separated by dots: header.payload.signature. The header and payload are not encrypted — they're just Base64url-encoded JSON that anyone can decode. The signature verifies authenticity. This is a common security misconception: JWTs are not secret by default. Sensitive data should not be stored in a JWT payload unless the token is also encrypted (JWE).
To decode a JWT's payload, use the Base64 Decoder — it handles Base64url automatically.
API Authentication Headers
HTTP Basic Authentication encodes credentials in Base64: the header value is Basic [Base64(username:password)]. For example, username api_user with password secret becomes Basic YXBpX3VzZXI6c2VjcmV0. This is sent in the Authorization header. Critically, this is not secure over plain HTTP — Base64 is trivially reversible. Always use HTTPS when sending any credentials, including Basic auth.
Email Attachments (MIME)
MIME encodes email attachments as Base64 with 76-character lines followed by CRLF line endings (per RFC 2045). If you've ever looked at raw email source, you've seen multi-kilobyte blocks of Base64 under Content-Transfer-Encoding: base64. This line-length limit was a compatibility requirement for some legacy SMTP servers. Modern Base64 implementations (RFC 4648) don't require line breaks — they're just a MIME convention.