One Click Answers

What is Base64 and how does it work?

Base64 is a way of writing any data, such as text or an image, using only 64 ordinary characters: the letters A to Z and a to z, the digits 0 to 9, plus and slash. That makes it safe to pass through systems that only handle plain text.

Base64 examples

Text and its Base64 form
TextBase64
ManTWFu
MaTWE=
HiSGk=
HelloSGVsbG8=
Hello WorldSGVsbG8gV29ybGQ=

How it works

The input is read three bytes at a time. Those 24 bits are split into four groups of 6 bits, and each group picks one of the 64 characters. So every 3 bytes become 4 characters, which makes the result about a third larger than the original.

Why the equals signs?

When the input is not a multiple of three bytes, the output is padded with equals signs so its length is a multiple of four. Two input bytes give one equals sign at the end, and one input byte gives two.

Where you meet it

  • Small images embedded directly in a web page or a style sheet.
  • Attachments inside email.
  • Tokens and keys that are passed as text.
  • Data sent in a web address, using a variation called URL-safe Base64.

The URL-safe variation

Plus and slash have special meanings in web addresses, so the URL-safe form swaps them for a hyphen and an underscore and often drops the padding. The same bytes then look slightly different, so use the variant the other side expects.

Base64 Encoder / Decoder: Encode text to Base64 or decode Base64 back to text, including the URL-safe form.

How we know

Sources

Published by OneClickAnswers.

You might also wonder...

Try it yourself

Just want the short version? What is Base64?

← All articles

Got another question?

Free, no account. Answers show how they were worked out.