What Is Base64 Encoding and When Should You Use It?

By 2 min read

Base64 is a way to write any data, including images and binary files, using only 64 safe text characters: A to Z, a to z, 0 to 9, plus + and /. It is used to send binary data through systems built for text, such as email and JSON. Base64 is an encoding, not encryption: anyone can decode it.

How it works

  1. Take the data 3 bytes (24 bits) at a time.
  2. Split the 24 bits into four groups of 6 bits.
  3. Map each 6 bit group (0 to 63) to one of the 64 characters.

So every 3 bytes become 4 characters. "Man" encodes to TWFu, and "InovoTools" encodes to SW5vdm9Ub29scw==. The Base64 encoder and decoder works in both directions and supports UTF-8 text.

What the equals signs mean

When the data is not a multiple of 3 bytes, the last group is padded and = signs are added to show it. "Hi" (2 bytes) becomes SGk=, with one = sign. One or two = signs at the end are normal.

Why Base64 is bigger

Because 3 bytes become 4 characters, encoded data is about 33% larger than the original. A 300 KB image becomes about 400 KB of Base64 text. That is why inlining large images as Base64 in web pages can slow them down.

Common uses

  • Email attachments (MIME).
  • Small images embedded in HTML or CSS as data URLs.
  • Binary data inside JSON or XML.
  • Basic HTTP authentication headers.
  • Parts of JSON Web Tokens (JWTs), which use a URL-safe variant.

URL-safe Base64

Standard Base64 uses + and /, which have special meanings in URLs. The URL-safe variant replaces them with - and _ and often drops the padding. For other text in links, use percent-encoding with the URL encoder and decoder instead.

Base64 is not security

Base64 hides nothing: decoding it takes a second. Never use it to protect passwords or secrets; use proper encryption or hashing for that.

Questions and answers

Is Base64 encryption?

No. It is an encoding that anyone can reverse. It does not protect data.

Why does Base64 end with equals signs?

They are padding, added when the input length is not a multiple of 3 bytes.

More from the blog