Every dayText & Encoding

Base64 Encoder &
decoder.

Turn text or a file into Base64, or decode it, all inside your browser.

How it's worked out

Base64 writes any data with 64 safe characters: A to Z, a to z, 0 to 9, plus + and /. RFC 4648 sets the rules. The tool first turns your text into bytes with UTF-8, so an accented letter or an emoji survives the trip. The letter é, for example, is two bytes and comes out as w6k=.

Then it takes the bytes three at a time. Three bytes are 24 bits, and 24 bits split into four groups of 6. Each 6-bit group is a number from 0 to 63, and that number picks one character from the list.

characters\displaystyle \text{characters}=4×⌈bytes3⌉\displaystyle {}= 4 \times \left\lceil \frac{\text{bytes}}{3} \right\rceil

The brackets mean "round up". When the last group is short, zero bits fill the gap and = signs mark the missing characters. One byte over gives two = signs and two bytes over give one: f is Zg==, fo is Zm8=, foo is Zm9v.

The URL-safe form swaps + for - and / for _, since + and / mean something in a web address. RFC 7515, used by JSON Web Tokens, also drops the = signs, so f becomes Zg. Decoding reverses every step, and the tool reads either alphabet, with or without padding.

Worked examples

Ana checks a short greeting group by group

Ana is learning how Base64 works. "Good morning." is 13 bytes, so it makes 5 groups. The first group is Goo, the bytes 47 6F 6F. Split into 6-bit values they are 17, 54, 61, 47, which pick R29v.

The last group holds only the full stop, one byte. Two zero bits round it up to two characters, and two = signs fill the rest, so it writes Lg==. The full answer is R29vZCBtb3JuaW5nLg==, which is 20 characters, 54% longer than the input.

Open this example in the tool

Dev builds an HTTP Basic login header

Dev is testing an API that uses HTTP Basic login. The rule is to join the user name and password with a colon, then encode that. With the sample from the standard, Aladdin and open sesame, the 19 bytes become QWxhZGRpbjpvcGVuIHNlc2FtZQ==.

The header is then the word Basic, a space and that value. Anyone who sees the request can decode it in one step, which is why the standard calls the scheme not secure unless the connection is encrypted with HTTPS.

Open this example in the tool

Lee reads the header of a JSON Web Token

Lee copies the first part of a token, the text before the first dot. It is 36 characters with no = signs, as tokens leave them off. It decodes to 27 bytes of text: {"alg":"HS256","typ":"JWT"}.

The alg field names the signing method the token uses. The other parts decode the same way, one at a time. If a part decodes to bytes that are not text, the tool shows them in hex instead.

Open this example in the tool

Reading your result

The length grows by about a third. Every 3 bytes become 4 characters. A short input grows more, because the last group is always padded to 4: "Good morning." grew by 54%. A 1 MB file grows by 33.3%.

Base64 length for common sizes, before line breaks
InputBase64 characters
1 KB1,368
100 KB136,536
1 MB1,398,104

Line breaks add a little more. MIME email keeps lines to 76 characters, so a 100 KB attachment runs to 1,797 lines, each with its own line break. PEM files, such as certificates, use lines of exactly 64 characters between BEGIN and END lines.

Decoded bytes are not always text. If the bytes are not valid UTF-8, the tool shows them in hex. That usually means the Base64 held a file, such as a picture or a zip, or text saved in an older encoding.

A data URL puts a file inside a page. It starts with data:, the file type and ;base64, then the Base64 itself (RFC 2397). It saves one request for a tiny icon, but the extra third in size makes it a poor choice for large images.

Common mistakes

  • Treating Base64 as a lock. It has no key. RFC 4648 says base encoding gives no confidentiality, so never use it to hide a password or private data.
  • Mixing the two alphabets. A value with - or _ is URL-safe, and one with + or / is standard. Some decoders accept only one kind, so a token may fail in a tool that expects the other.
  • Stripping the = signs and the length with them. Some decoders need the padding. If a value is refused, add = signs until its length is a multiple of 4.
  • Encoding text in the wrong character set. This tool uses UTF-8, as the web does. Text saved in an older code page gives different bytes, so the Base64 will not match.
  • Putting secrets in a share link. The tool keeps your text in the link fragment so that it opens again for you. Do not send a link that holds a token or a password.

Questions people ask

The = signs only appear when the input length is not a multiple of 3. A padded value is always a multiple of 4 characters long, and you will never see three = signs. Values made for web addresses often leave the padding off, because the decoder can work out the length without it.

Sources