Base64 encoding and decoding guide
What Base64 is
Base64 is a way of writing any sequence of bytes using only 64 safe, printable characters: the letters A to Z and a to z, the digits 0 to 9, and the symbols + and /. It exists because many systems, such as email bodies, JSON strings and URLs, were designed for text and can mangle raw binary data. Encoding the bytes as Base64 lets them pass through unharmed. It is an encoding, not encryption, and anyone can reverse it.
How the encoding works
The input bytes are taken three at a time, which is 24 bits. Those 24 bits are split into four groups of 6 bits, and each group is mapped to one character of the alphabet. So three bytes become four characters, and the output is about one third larger than the input.
Padding
When the input length is not a multiple of three, the last group is short. The encoder pads the output with = signs so its length is always a multiple of four. One leftover byte gives two padding characters and two leftover bytes give one.
Worked example
| Text | Bytes | Base64 |
|---|---|---|
| Dog | 3 | RG9n |
| Do | 2 | RG8= |
| Dogs | 4 | RG9ncw== |
For "Dog" the bytes are 68, 111 and 103, which is the bit string 01000100 01101111 01100111. Regrouped into sixes it is 010001 000110 111101 100111, or 17, 6, 61 and 39, which map to R, G, 9 and n.
UTF-8 and special characters
Base64 works on bytes, not characters, so text must first be turned into bytes. This tool uses UTF-8, the dominant encoding on the web. A plain letter takes one byte, an accented letter such as é takes two, a euro sign takes three and most emoji take four. That is why "é" encodes to w6k= and the euro sign to 4oKs. Decoding reverses the process. If the bytes are not valid in the character set you chose, the invalid parts show as the replacement character (a question mark in a diamond), which is a sign that another character set is needed.
URL-safe Base64
The plus and slash characters have special meaning in URLs and file paths. The URL-safe alphabet from RFC 4648 replaces + with - and / with _, and many systems, including JSON Web Tokens, also drop the = padding. This tool writes and reads the standard alphabet only, so convert - back to + and _ back to / before decoding URL-safe text. Missing padding is accepted.
Common uses
- Embedding small images in HTML or CSS as data URIs.
- Carrying binary attachments in email.
- Passing credentials in HTTP Basic authentication headers, which are Base64 of user:password.
- Storing keys and certificates in text formats such as PEM.
- Encoding the segments of a JSON Web Token.
Privacy and limits
Everything runs in your browser and nothing is uploaded. Because Base64 is not encryption, never treat an encoded password or token as protected. For large files, a dedicated file tool or command line utility will be faster than pasting into a text box.
Reading a decoded result
If you paste Base64 that came from an image, a document or a compressed file, the decoded bytes will not be text, and the result will be full of replacement characters. This tool is meant for text. If the encoded data is a JSON Web Token, decode each of its dot-separated segments on its own (after swapping - and _ for + and /) and you will see the header and payload as JSON. Remember that the signature segment is binary, so it will not decode to readable text.
Line breaks and spacing
Some systems wrap Base64 into lines of 76 characters, as MIME email does. The decoder ignores spaces and line breaks, so wrapped input can be pasted as it is. The encoder produces a single unbroken line, which is what most web applications expect.