Base64 is a binary-to-text encoding that represents arbitrary bytes using an alphabet of 64 printable ASCII characters, so that binary data can travel safely through systems designed only for text. In software interoperability, it is the common bridge that lets images, keys, certificates and attachments move through email, JSON, XML, URLs and configuration files without being corrupted by character set conversion or line handling.
Base64 is defined in RFC 4648, which also describes the URL-safe variant and related encodings such as Base32 and Base16. It is an encoding, not encryption: anyone can decode it, and its only purpose is to make data transportable, at the cost of roughly 33 percent size overhead.
How Base64 turns bytes into text
Base64 works by regrouping bits. The encoder takes the input in blocks of three bytes (24 bits) and splits each block into four groups of six bits. Each six-bit value, from 0 to 63, selects one character from the alphabet: A to Z for 0 to 25, a to z for 26 to 51, 0 to 9 for 52 to 61, then + and / for 62 and 63.
When the input length is not a multiple of three, the final block is incomplete. The encoder pads the missing bits with zeros and appends one or two equals signs (=) so that the output length is always a multiple of four. The decoder reverses the process exactly, which makes Base64 a lossless, deterministic transformation.
The following table shows how input length determines padding, using the ASCII strings as input.
| Input | Input bytes | Encoded output | Padding characters |
|---|---|---|---|
| Man | 3 | TWFu | None |
| Ma | 2 | TWE= | One |
| M | 1 | TQ== | Two |
| Hello | 5 | SGVsbG8= | One |
The size relationship follows directly from the mechanism: every three input bytes become four output characters, so encoded data is four thirds of the original size, plus up to two padding characters and any line breaks a particular format inserts.
Base64 variants and where they apply
Several Base64 variants exist because the characters +, / and = have special meanings in some contexts. They share the same bit-grouping mechanism but differ in the last two alphabet characters, the padding rule and line length. Mixing variants is a frequent cause of decoding failures between systems.
| Variant | Characters 62 and 63 | Padding | Typical use |
|---|---|---|---|
| Standard Base64 | + and / | Required | General data exchange, JSON payloads |
| Base64url | – and _ | Often omitted | URLs, file names, JSON Web Tokens |
| MIME Base64 | + and / | Required | Email attachments, lines of 76 characters |
| PEM encoding | + and / | Required | Certificates and keys, lines of 64 characters |
The URL-safe alphabet avoids percent-encoding in query strings and path segments. JSON Web Tokens, for example, use Base64url without padding for each of their three dot-separated parts. MIME and PEM, in contrast, wrap long output into fixed-length lines with CRLF line endings, which a strict decoder may reject unless whitespace is ignored.
Base64 compared with other encodings
Base64 is one of several ways to represent binary data as text, and it sits at a practical balance between density and safety. Denser schemes use more characters, some of which are unsafe in common formats; sparser schemes are easier to read but waste more space.
| Encoding | Bits per character | Size overhead | Strength |
|---|---|---|---|
| Hexadecimal (Base16) | 4 | 100 percent | Readable, case-insensitive, simple to debug |
| Base32 | 5 | About 60 percent | Case-insensitive, avoids confusing characters |
| Base64 | 6 | About 33 percent | Compact and universally supported |
| Ascii85 | About 6.4 | About 25 percent | Denser, used in PDF and PostScript |
| Percent-encoding | Varies | Up to 200 percent | Escapes only unsafe characters in URLs |
Base64 should also be distinguished from character encoding. UTF-8 and UTF-16 map text characters to bytes; Base64 maps bytes to a restricted set of characters. Encoding a string to Base64 therefore requires a prior decision about its byte representation, which is why encoding the same text from UTF-8 and UTF-16 produces different Base64 output.
Practical use in data exchange
Base64 is justified when a text-only channel must carry binary content. Email relies on it through MIME, because many mail transfer agents historically handled only 7-bit text. Web developers use it in data URLs to embed small images in HTML or CSS, and APIs use it to place file contents or cryptographic signatures inside JSON, which has no native binary type.
| Situation | Recommendation | Reason |
|---|---|---|
| Binary field inside JSON or XML | Use standard Base64 | The format has no raw binary type |
| Token in a URL or file name | Use Base64url without padding | Avoids reserved characters |
| Large file upload over HTTP | Send raw bytes with multipart or binary body | Avoids 33 percent overhead and extra memory |
| Protecting secrets | Use real encryption | Base64 provides no confidentiality |
| Small icons in CSS | Data URL only below a few kilobytes | Larger assets cache better as separate files |
Limitations and common mistakes
The most serious misconception is treating Base64 as a security measure. Credentials in HTTP Basic authentication are Base64-encoded, not protected, and they are readable to anyone who intercepts the request unless TLS is used. Other problems are practical and appear during integration between systems.
- Variant mismatch: a standard decoder rejects
-and_from Base64url input; normalize the alphabet before decoding. - Missing padding: some producers strip
=; restore it so the length is a multiple of four when the decoder requires it. - Line breaks and whitespace: MIME and PEM data contain newlines that strict decoders treat as invalid characters.
- Text encoding confusion: in browsers,
btoa()accepts only Latin-1 strings; encode text to UTF-8 bytes first. - Memory cost: encoding large files in memory creates a string one third larger than the data, plus intermediate copies.
Because Base64 output looks random, it is sometimes used to obscure data in logs or malware. Security tools therefore routinely decode Base64 strings during inspection, which is another reason the encoding should never be relied on to hide information.
A plain-language summary
Computers store pictures, files and keys as raw bytes. Some systems, such as email and many web formats, were built to carry only ordinary text. Raw bytes can get damaged when they pass through them.
Base64 solves this by rewriting the bytes using only safe letters, digits and a few symbols. It is like spelling a message with a limited set of tiles that every system understands. The receiver turns the tiles back into the exact original bytes. The price is size: the text version is about one third longer. Base64 does not hide or lock anything, because anyone can turn it back.
An example from practice
A logistics company runs a mobile app that lets drivers photograph signed delivery receipts. The back-end team designs a REST API that accepts each delivery confirmation as one JSON document containing the order number, a timestamp, GPS coordinates and the photo. Since JSON cannot hold raw bytes, the app encodes the compressed JPEG of about 180 kilobytes as standard Base64, producing a string of roughly 240 kilobytes.
During testing, the team finds that some receipts from an older scanner integration fail to decode. The cause is that the scanner software emits MIME-style Base64 with line breaks every 76 characters. The server is updated to strip whitespace before decoding. For larger scanned documents, the team later moves to a multipart upload, which removes the overhead and reduces mobile data usage by about a quarter.