Base64
By IETF (RFC 4648)
Base64 is a binary-to-text encoding scheme that represents arbitrary binary data using only 64 printable ASCII characters, letting binary content be safely embedded in contexts designed for text, such as email bodies, URLs, or JSON…
Definition
Base64 is a binary-to-text encoding scheme that represents arbitrary binary data using only 64 printable ASCII characters, letting binary content be safely embedded in contexts designed for text, such as email bodies, URLs, or JSON strings. Every three bytes of input binary data expand to four Base64 characters, which is why Base64-encoded data is roughly one-third larger than its original binary form.
Overview
Base64 addresses a problem that predates the modern web: many text-oriented protocols and formats, from early email systems to JSON documents, cannot safely carry arbitrary binary bytes, since certain byte values are reserved as control characters or delimiters, and some transport layers only guarantee reliable delivery of printable text. Base64 solves this by mapping every possible byte sequence onto a restricted alphabet of 64 characters, all of which are safe to transmit through virtually any text-based channel. The encoding works by grouping input bytes into chunks of three (24 bits), then splitting each chunk into four 6-bit groups, each of which maps to one of 64 characters, drawn from uppercase letters, lowercase letters, digits, and two additional symbols (traditionally `+` and `/`, with `-` and `_` used in the URL-safe variant). When the input length is not a multiple of three, padding characters (`=`) fill out the final group so the encoded output always has a length that's a multiple of four. Base64 differs from general-purpose compression or serialization formats in that it does not reduce data size, it increases it by roughly 33%, and its sole purpose is representational: converting bytes to text-safe characters, not making data smaller or adding structure. It is often used alongside formats like JSON or XML, which have no native binary type, embedding Base64 strings as values where raw binary would otherwise be disallowed. In practice, Base64 appears throughout modern computing: encoding image data directly into HTML or CSS as data URIs, attaching binary files to emails via MIME, embedding binary tokens or keys inside JSON API payloads, and encoding cryptographic signatures or certificates into text-based formats like PEM. Developers reach for Base64 whenever binary content needs to pass through a text-only pipe without corruption. Base64's main trade-off is the roughly one-third size increase over raw binary, which matters for large payloads such as images or files, making it a poor choice for storing or transmitting large binary content at scale compared to keeping it in native binary form. Base64 also provides no security whatsoever, despite sometimes being mistaken for an encryption or obfuscation technique; it is fully and trivially reversible by anyone, so it must never be used as a substitute for actual encryption when confidentiality matters. Developers occasionally conflate the two because Base64 output looks unreadable at a glance, but that appearance is purely a side effect of encoding, not any deliberate obfuscation of the underlying content.
Key Concepts
- Maps arbitrary binary data onto 64 printable ASCII characters
- Encodes three input bytes into four output characters
- Padding characters fill out encoded lengths not divisible evenly
- URL-safe variant substitutes characters unsafe in URLs
- Increases data size by roughly one-third versus raw binary
- Provides no confidentiality; it is not a form of encryption
- Widely supported natively across programming language standard libraries
- Commonly used inside data URIs, MIME email, and JSON payloads