How the Base64 Calculator Works
Base64 is a way to write any data, such as text or a picture, with only 64 safe characters: the letters A to Z and a to z, the digits 0 to 9, and the signs + and /. It was made for places where only plain text can be sent, such as an email, a web address, a JSON file or an HTML page. The Base64 Encode / Decode tool changes text to Base64, and Base64 back to text.
Base64 is not encryption. It hides nothing. Anyone can decode it in a second, so never use it to protect a password or a secret.
The 64 characters
Each character stands for a number from 0 to 63. The alphabet has 64 characters (each block below is one of them), and the sign = is used at the end as padding.
████████████████████████████████████████████████████████████████ A-Z 0-25 (blue) a-z 26-51 (green) 0-9 52-61 (amber) + / 62-63 (red)
How the encoding works
- The text is changed to bytes (the tool uses UTF-8, so every letter and emoji has its bytes).
- The bytes are put together in groups of 3. Three bytes are 24 bits.
- The 24 bits are cut into 4 pieces of 6 bits. A piece of 6 bits is a number from 0 to 63.
- Each number is changed to its character in the alphabet.
Example, the text Man:
| Step | Value |
|---|---|
| Letters | M, a, n |
| Bytes (numbers) | 77, 97, 110 |
| Bits, 8 for each byte | 01001101 01100001 01101110 |
| Bits, 6 for each group | 010011 010110 000101 101110 |
| Numbers of the groups | 19, 22, 5, 46 |
| Characters | TWFu |
The chart shows the same 24 bits, as 3 bytes of 8 bits (first line) and as 4 groups of 6 bits (second line).
████████████████████████ ████████████████████████
Padding
If the number of bytes cannot be divided by 3, the last group is short. The encoder fills it with zero bits, and adds one or two = signs, so that the result always has a length that can be divided by 4. One byte left over gives two = signs, and two bytes left over give one.
Examples
| Text | Bytes in UTF-8 | Base64 | Characters |
|---|---|---|---|
| Man | 3 | TWFu | 4 |
| Ma | 2 | TWE= | 4 |
| M | 1 | TQ== | 4 |
| Hello World | 11 | SGVsbG8gV29ybGQ= | 16 |
| café | 5 | Y2Fmw6k= | 8 |
| € | 3 | 4oKs | 4 |
| 你好 | 6 | 5L2g5aW9 | 8 |
| 😀 | 4 | 8J+YgA== | 8 |
Letters with accents, the euro sign, Chinese characters and emoji need 2, 3 or 4 bytes each in UTF-8. This tool handles them correctly. Many simple encoders do not, and they stop with an error when the text has a character outside of the basic Latin letters.
How much bigger does it get?
Every 3 bytes become 4 characters, so Base64 is about a third bigger (133%) than the data, plus the padding. The blue bar is the data, and the green bar is the Base64 (each block is 3 bytes and 4 characters).
3 bytes █ 3 █ 4 characters 6 bytes ██ 6 ██ 8 characters 12 bytes ████ 12 ████ 16 characters 30 bytes ██████████ 30 ██████████ 40 characters 60 bytes ████████████████████ 60 ████████████████████ 80 characters 120 bytes ████████████████████████████████████████ 120 ████████████████████████████████████████ 160 characters
Decoding
Decoding is the same steps in the other direction: each character is changed to its number, the 6-bit pieces are put together, cut into bytes, and the bytes are read as UTF-8 text. The tool is forgiving:
- Spaces and line breaks are ignored, so you can paste Base64 that is wrapped over several lines.
- It accepts the standard form and the URL-safe form.
- A missing padding is added.
- If the bytes are not UTF-8 text (for example, they are a picture), the tool tells you, and shows one letter for each byte.
The URL-safe form
The signs + and / and the padding = have a special meaning in a web address. The URL-safe form of Base64 uses - instead of +, _ instead of /, and it usually leaves out the =. It is used in web tokens (JWT), links and file names. Choose it in the tool when you encode.
Where Base64 is used
- Small pictures inside a web page or a style sheet (data URLs).
- Attachments of an email.
- The parts of a web token (JWT).
- Sending binary data in a JSON file or a form.
What this tool does not include
- Encoding or decoding of files. It works with text.
- Encryption, or any kind of protection of the data.
- Texts of more than 200,000 characters to encode, or 1,000,000 characters of Base64 to decode.
The conversion is made in your browser, and your text is not sent to a server. Do not use Base64 to hide sensitive information.