Base64 explained: what it is and when to use it
Published · Updated
Base64 is a way of writing any data, including images and other binary files, using only 64 safe text characters. Systems that were built for plain text, such as email, web addresses and JSON, can then carry the data without damaging it. You meet it in data URLs, authentication headers, email attachments and tokens, and it is often mistaken for encryption, which it is not.
Try it now: Base64 Encode / Decode, JWT Decoder.
How does Base64 work?
Base64 takes the data three bytes at a time. Three bytes are 24 bits, and it cuts those into four groups of 6 bits. Each 6-bit group is a number from 0 to 63, and each number is written as one character from a fixed alphabet: the letters A to Z, a to z, the digits 0 to 9, plus and slash.
Take the word Man. Its three bytes, in bits, are 01001101 01100001 01101110. Regrouped in sixes they are 010011, 010110, 000101 and 101110, which are 19, 22, 5 and 46, and the alphabet turns those into T, W, F and u. So Man becomes TWFu. Try it in the Base64 encoder and decoder.
What is Base64 padding?
When the data is not a multiple of three bytes, the last group is short and the output is filled up with equals signs so its length is a multiple of four. One leftover byte gives two equals signs and two leftover bytes give one.
| Text | Base64 |
|---|---|
Man | TWFu |
Ma | TWE= |
M | TQ== |
Hello | SGVsbG8= |
Hello, World! | SGVsbG8sIFdvcmxkIQ== |
How does Base64 handle text that is not plain English?
Base64 encodes bytes, not letters, so non-English text is first turned into bytes, almost always with UTF-8. The letter é is two bytes and encodes as w6k=. The emoji 😀 is four bytes and encodes as 8J+YgA==. If you decode and the text looks garbled, the bytes were probably not UTF-8 text at all, for example an image.
Why is Base64 output bigger?
Every 3 bytes become 4 characters, so Base64 makes data about a third larger. A file of 100 bytes becomes 136 characters: 34 groups of three bytes, rounding up, each written as 4 characters. That cost is the price of being safe in text-only places.
Where is Base64 used?
- Data URLs, which put a small image straight into a page. A PNG image always starts data:image/png;base64,iVBORw0KGgo and continues from there.
- Email attachments, which use Base64 in lines of 76 characters.
- Binary data inside JSON or XML, which cannot hold raw bytes.
- HTTP Basic authentication, where the username and password are joined with a colon and encoded: user:pass becomes dXNlcjpwYXNz.
- JSON Web Tokens, certificates and keys in PEM files.
What is URL-safe Base64?
Plus and slash have special meaning in web addresses and file names. The URL-safe form swaps them for a hyphen and an underscore and often leaves out the equals signs. The three bytes FB FF BF are +/+/ in standard Base64 and -_-_ in the URL-safe form. JSON Web Tokens use the URL-safe form, so a JWT decoder reads Base64 first and then the JSON inside it. JWTs are a common example, covered in the JWT guide.
Is Base64 encryption?
Anyone can decode Base64 in a second, with no key. The Basic authentication value above shows the username and password to anyone who sees it, which is why Basic authentication is only acceptable over a secure connection. Never use Base64 to hide something. To protect data, encrypt it, and then use Base64 only if you need to write the encrypted bytes as text.
When should I not use Base64?
Avoid Base64 for large images in web pages. The extra third slows the page, and the image can no longer be cached on its own. Where a system can accept binary data directly, such as a file upload, send the bytes as they are.