How Base64 works
Base64, defined in RFC 4648, writes any sequence of bytes using only 64 safe characters: A to Z, a to z, 0 to 9, “+” and “/”. It exists so that binary data, or text in any script, can pass through systems built to carry plain ASCII, such as email bodies, JSON, XML, data URLs and HTTP headers.
The method takes the bytes three at a time. Three bytes are 24 bits, which split into four groups of 6 bits. Each 6-bit group is a number from 0 to 63, and that number picks a character from the alphabet: 0 to 25 are A to Z, 26 to 51 are a to z, 52 to 61 are 0 to 9, 62 is “+” and 63 is “/”.
Encoded length = 4 × ⌈n ÷ 3⌉ characters
Here n is the number of bytes and ⌈ ⌉ means round up. When n is not a multiple of 3, the last group is filled out with zero bits and the missing characters are shown as “=”: one “=” when the last group had 2 bytes, two when it had 1. Base64 output is therefore about a third larger than the input.
Worked example: encoding “Hi!”
- Text to bytes. In UTF-8, H, i and ! are the bytes 72, 105 and 33.
- Bytes to bits. 01001000 01101001 00100001.
- Regroup in sixes. 010010 000110 100100 100001.
- Read each group as a number. 18, 6, 36 and 33.
- Look up the alphabet. 18 is S, 6 is G, 36 is k (36 − 26 = 10, the eleventh lower-case letter) and 33 is h. The result is
SGkh.
With fewer bytes, padding appears: “Man” encodes to TWFu, “Ma” to TWE= and “M” to TQ==. Decoding runs the same steps backwards.
Text needs an encoding first
Base64 works on bytes, not letters, so text must first be turned into bytes. This page uses UTF-8, the encoding used by almost all of the web. Letters outside basic English take more than one byte: “é” is the two bytes C3 A9 and encodes to w6k=; the emoji 😀 is four bytes, F0 9F 98 80, and encodes to 8J+YgA==.
The browser’s built-in btoa function does not do this. It treats each character as a single Latin-1 byte, so it turns “é” into 6Q==, which other tools decode as an invalid UTF-8 sequence, and it throws an error on anything beyond Latin-1, such as “€” or an emoji. The same mismatch in reverse is why decoded text sometimes shows as “é” where “é” was meant: UTF-8 bytes read as Latin-1.
URL-safe Base64 and padding
“+” and “/” have special meanings in web addresses and file names, so RFC 4648 also defines a URL-safe alphabet that uses “-” for 62 and “_” for 63. The emoji above becomes 8J-YgA in URL-safe form with the padding left out, which is how JSON Web Tokens and many web APIs write it. Padding carries no information, since the length tells a decoder how many bytes are in the last group, so many formats omit it. This decoder accepts either alphabet, with or without padding.
Reading the error messages
Invalid character. Only the 64 alphabet characters, “=” at the end, and spaces or line breaks are allowed. A stray quote mark, a comma or the “data:” prefix of a data URL will stop decoding; remove everything before the comma in a data URL first.
Wrong length. Without padding, a valid string can be 0, 2 or 3 characters past a multiple of 4, but never 1, because one character carries only 6 bits, less than a byte. That usually means a character was lost when copying.
Not valid UTF-8. The Base64 itself is fine, but the bytes are not UTF-8 text. It may be an image, a compressed file, encrypted data or text in another encoding. The calculator then shows the bytes in hexadecimal instead.
Base64 is not encryption. Anyone can decode it. Never treat Base64 as a way to hide passwords or keys.