Base64 shows up everywhere in web development: data URIs, email attachments, JWTs, API keys and basic-auth headers. It looks like scrambled text, but it is a simple, reversible way of writing binary data using only 64 safe characters. Once you see the mechanism, the odd details (the trailing equals signs, the size growth, the URL-safe variant) all make sense.
You can try every example below in our Base64 encoder and decoder, which runs in your browser.
The core idea: 3 bytes in, 4 characters out
Computers store data as bytes, and a byte can hold any value from 0 to 255. Many systems that carry text, such as email or JSON, cannot reliably transport arbitrary byte values. Base64 solves this by regrouping the bits. It takes three bytes (24 bits), splits them into four groups of six bits, and writes each group as one character from a 64-character alphabet: A to Z, a to z, 0 to 9, + and /.
Six bits can hold 64 different values, which is exactly the size of the alphabet. Here is a small example. The three letters Man are the bytes 77, 97 and 110. Written in binary and regrouped into six-bit chunks, they become the values 19, 22, 5 and 46, which map to the characters T, W, F and u:
Man -> TWFuWhy Base64 output is about 33% larger
Because every 3 bytes turn into 4 characters, the output is four thirds the size of the input. The exact length is the input length divided by three, rounded up, then multiplied by four:
| Input size | Base64 length | Growth |
|---|---|---|
| 3 bytes | 4 characters | +33% |
| 10 bytes | 16 characters | +60% (padding dominates) |
| 100 bytes | 136 characters | +36% |
| 1,000 bytes | 1,336 characters | +33.6% |
| 1 MiB (1,048,576 bytes) | 1,398,104 characters | +33.3% |
That growth is the main reason not to inline large images as Base64 data URIs. A 300 KB image becomes about 400 KB of text, and because it sits inside your HTML or CSS it cannot be cached separately from the page.
What the equals signs mean
Input lengths that are not a multiple of three leave a partial group at the end. Base64 pads the output with = so the length is always a multiple of four:
M -> TQ==
Ma -> TWE=
Man -> TWFuOne leftover byte produces two characters plus two =, and two leftover bytes produce three characters plus one =. The padding carries no data; it only tells a decoder how many bytes the final group holds. Some systems strip it, and most decoders accept the unpadded form.
URL-safe Base64
The standard alphabet includes + and /, which both have special meaning in URLs and file paths. The URL-safe variant, used by JWTs, swaps them for - and _ and usually drops the padding. The same three bytes (251, 255 and 254) show the difference:
standard: +//+
url-safe: -__-If a token or string contains - or _, it is the URL-safe form, and a strict standard decoder will reject it. Our decoder has a URL-safe option for exactly this case. Our guide to reading a JWT shows where it matters in practice.
Text, Unicode and Base64
Base64 encodes bytes, not characters, so text must first be turned into bytes with a character encoding, almost always UTF-8. That is why the same-looking text can produce different Base64 depending on how it was encoded:
| Text | UTF-8 bytes | Base64 |
|---|---|---|
| Hello, World! | 13 | SGVsbG8sIFdvcmxkIQ== |
| héllo (precomposed é) | 6 | aMOpbGxv |
| 🙂 | 4 | 8J+Zgg== |
A classic pitfall is the JavaScript function btoa(), which only accepts characters up to code 255 and throws an error on an emoji or most non-Latin text. Encode the string to UTF-8 bytes first. A tool that supports Unicode does this for you.
Base64 is not encryption
Anyone can decode Base64 in a second, with no key. It hides nothing. If you see credentials, tokens or personal data stored as Base64, treat them exactly as you would plain text. The HTTP Basic authentication header is a common example: it is just username:password in Base64, so it is only safe over HTTPS.
When Base64 is the right choice
- Embedding small images or fonts in CSS or HTML as data URIs, where saving a request is worth the size increase.
- Putting binary data into JSON or XML, which cannot carry raw bytes.
- Email attachments, which use Base64 under MIME and break the output into lines of 76 characters.
- Tokens such as JWTs, where the URL-safe form keeps the token safe to place in a URL or header.
For anything large, or when you control both ends of a connection, sending binary directly is more efficient.
Method and limits
The encoded values and lengths in this guide were produced by encoding the example inputs and checking the results against the standard alphabet. The growth percentages follow from the 3-to-4 rule. Different libraries treat line wrapping and missing padding differently, so check your platform's decoder if a value does not round-trip.
Quick summary
Base64 turns every 3 bytes into 4 characters, so output is about a third larger. Padding with = fills the last group, the URL-safe form swaps + and / for - and _, and none of it is encryption.
Try it: Try the Base64 tool.
Related tools
- Base64 Encoder & Decoder: Encode text to Base64 or decode Base64 back to text, with full Unicode and URL-safe support.
- JWT Decoder: Decode a JSON Web Token locally to read its header, payload and expiry without sending it anywhere.
- URL Encoder & Decoder: Percent-encode or decode URLs and query strings, and break a URL into its parts.