What Is Base64?
Base64 is an encoding scheme that converts binary data into a text string using a set of 64 characters: A-Z, a-z, 0-9, + and /. The name comes from the 64 characters used. A Base64-encoded string contains no special characters that might be misinterpreted as control characters or delimiters in text-based protocols.
It is crucial to understand what Base64 is not: it is not encryption. Base64-encoded data is trivially decodable by anyone who has it. It provides obfuscation but zero security. Do not use Base64 to "protect" sensitive information.
Why Base64 Exists: The Problem It Solves
Base64 was invented to solve a specific technical problem: many protocols and systems that were designed to handle text could not reliably transfer arbitrary binary data.
Email protocols like SMTP, designed in the 1980s, transmitted data as 7-bit ASCII characters. The upper half of the byte (values 128-255) was often stripped or corrupted in transit, and certain byte values (like 0x00, the null character) could be interpreted as end-of-message markers. Sending a binary file like a JPEG image directly over SMTP would corrupt it.
Base64 solves this by representing every possible binary byte value using only the 64 "safe" ASCII characters. Three bytes of binary data (24 bits) are encoded as four Base64 characters (6 bits each, 24 bits total). The resulting text is 33% larger than the original binary, but it is guaranteed to survive any text-based transport mechanism.
How Base64 Encoding Works
The algorithm is straightforward. Take three bytes of input, view them as a 24-bit binary number, and split them into four 6-bit groups. Each 6-bit value (0-63) maps to a specific character in the Base64 alphabet. If the input length is not divisible by 3, padding characters (=) are appended.
For example, the ASCII text "Man" in binary is 01001101 01100001 01101110. Combined into 24 bits: 010011010110000101101110. Split into four 6-bit groups: 010011 | 010110 | 000101 | 101110 = 19, 22, 5, 46. Looking up these values in the Base64 table gives: T, W, F, u. So "Man" encodes to "TWFu."
The = padding at the end of Base64 strings indicates that one or two fewer input bytes were available in the final group. One = means one padding byte, == means two.
Common Use Cases
**Data URLs.** HTML and CSS support the data: URI scheme, which embeds resource data directly in code rather than linking to an external file. A small image, font, or icon can be inlined as Base64: data:image/png;base64,iVBORw0KGgo... This eliminates an HTTP request, which can improve performance for small resources.
**Email attachments.** MIME (Multipurpose Internet Mail Extensions), the standard that allows email to carry attachments, uses Base64 to encode binary files. When you attach a PDF to an email, your email client Base64-encodes it for transit.
**JSON APIs.** JSON is a text format and cannot directly contain binary data. APIs that need to transfer binary content — images, audio clips, file uploads — often Base64-encode the binary and embed it as a JSON string value.
**Basic HTTP Authentication.** HTTP's Basic Authentication scheme sends credentials as Base64(username:password) in the Authorization header. This is not secure on its own (the encoding is reversible), but is used over HTTPS where the transport is encrypted.
**Embedding images in CSS.** Background images and icons can be inlined in CSS stylesheets as Base64 data URLs, allowing a website to work from a single CSS file without separate image requests.
**Storing binary data in text fields.** Databases with only text column types, or systems that must store configuration as JSON or XML, use Base64 to store binary data like cryptographic keys, certificates, or file contents.
URL-Safe Base64
Standard Base64 uses + and / characters, which have special meaning in URLs. A Base64 string embedded in a query parameter would break URL parsing. URL-safe Base64 (also called Base64url) replaces + with - and / with _ and typically omits padding. Most modern APIs that use Base64 in URLs use this variant.
When Not to Use Base64
Base64 increases data size by approximately 33%. For large files — videos, high-resolution images, large binary blobs — the size overhead is significant. Transferring a 10 MB binary file as 13.3 MB of Base64 text wastes bandwidth and memory.
For large binary transfers, use binary-safe protocols directly: multipart/form-data for HTTP uploads, binary WebSocket frames, or file streaming protocols. Base64 is an appropriate tool for small resources, credentials, and situations where binary transport is not available — not as a general-purpose binary transfer mechanism.
Decoding Base64 on the receiving end also takes CPU time. For high-frequency operations on large data, this overhead can be meaningful.