Code & data
What Is Base64 and When Should You Use It?
How Base64 turns bytes into plain text, why it makes data a third bigger, where it's used, and why it isn't encryption. With a worked example, step by step.
Updated · 4 min read
If you've ever seen a long string ending in == in an email source, a JWT or a config file, you've met Base64. It's a simple way to carry any data, including images and other binary files, through systems that were designed for text.
The problem it solves
Computers store everything as bytes, and any byte can have one of 256 values. Many of those values aren't printable, and some have a special meaning: they might end a string, start a new line or control a terminal.
Older text-based systems, email above all, could mangle such bytes on the way through. So we need a way of writing arbitrary bytes using only characters that survive everywhere. Base64 does exactly that.
How it works
The name says it all: the alphabet has 64 characters.
- A–Z (26)
- a–z (26)
- 0–9 (10)
+and/(2)
64 is 2⁶, so each character can represent exactly 6 bits. The encoder takes the input's bits and splits them into groups of six rather than eight.
A worked example
Let's encode the word Man.
- Each letter is one byte, written in ASCII: M = 77, a = 97, n = 110.
- In binary:
01001101 01100001 01101110. - Split into groups of six:
010011 010110 000101 101110. - As numbers: 19, 22, 5, 46.
- Look these up in the alphabet: 19 = T, 22 = W, 5 = F, 46 = u.
So Man becomes TWFu. You can look at the bytes behind any text with the text to binary converter, and try encoding with the Base64 encoder.
What are the = signs?
Base64 works on groups of three bytes. If the input doesn't divide evenly by three, the last group is padded so that the output length stays a multiple of four.
| Input | Bytes | Base64 |
|---|---|---|
Man |
3 | TWFu |
Ma |
2 | TWE= |
M |
1 | TQ== |
So one = means the last group had two bytes, and two mean it had one. This is why strings so often end with ==.
Why it makes data bigger
Three bytes become four characters, which is an increase of about 33%. A 3 MB image turns into about 4 MB of text. Line breaks can add a bit more.
This matters. Base64 is compact enough for small items, but it's a poor way to ship large files.
Where Base64 is used
Email attachments. The MIME standard uses it to send files through a text-based system.
Data URLs. Small images can be embedded directly in HTML or CSS:
<img src="data:image/png;base64,iVBORw0KGgo..." alt="Icon" />
JSON and XML. These text formats have no binary type, so an API might send a file or a key as a Base64 string.
JSON Web Tokens. The header and payload of a JWT use a URL-safe version of Base64.
HTTP Basic authentication. The username and password are Base64-encoded in a header.
Keys and certificates. PEM files, with their -----BEGIN CERTIFICATE----- lines, are Base64 inside.
Base64 is not encryption
This is the most important point, and it's the most frequent misunderstanding. Base64 has no key and no secret. Anyone can decode it in a second, so it provides no security at all.
- HTTP Basic authentication is only safe over HTTPS, as Base64 hides nothing.
- A password "encoded" in a config file is effectively in plain text.
- A JWT payload can be read by anyone who holds the token.
If you need to protect data, use real encryption.
Base64url
The standard alphabet contains + and /, and these have a special meaning in URLs. Base64url replaces them with - and _, and it usually leaves out the padding. It's the version used in JWTs and many web APIs.
If you try decoding one variant with a decoder for the other, you may get an error or wrong output. The Base64 encoder lets you work with text and files in your browser.
Base64 vs. URL encoding
These two get confused, but they do different jobs.
Base64 turns any data into a compact text string. URL encoding (percent-encoding) keeps text readable, and replaces only unsafe characters, so a space becomes %20. It's what you want for putting a value in a query string. The URL encoder handles that.
When not to use it
- Big images on web pages. Embedded data can't be cached separately, and it's a third larger. A normal image file is better, except for very small icons.
- Storing files in a database. Binary columns or object storage are usually more efficient.
- Anything that's meant to be secret.
- Sending data that's already text-safe. Encoding plain text only makes it longer.
Decoding problems
"Invalid character" errors are often caused by copying in spaces or line breaks, or using Base64url with a standard decoder.
Garbled text after decoding usually means a wrong character encoding, or that the output is binary. The bytes are correct, but they're not text. A decoded PNG, for example, will look like nonsense in a text box, and should be saved as a file.
Non-English characters break. In JavaScript, the old btoa() function only handles Latin-1 characters. Convert the text to UTF-8 bytes first:
const bytes = new TextEncoder().encode("héllo");
const encoded = btoa(String.fromCharCode(...bytes));
That works well for short strings. For very large data, process it in chunks.