Robince Studio
Developer Tools

Base64 explained: how it works, with examples

Base64 turns any data into plain text made of 64 safe characters, so it can travel through systems built for text. It shows up in emails, data URLs, JSON web tokens and API credentials. It is an encoding, not a security feature, and this guide explains the difference.

3 min read Reviewed Published by Abdul Gani, Robince Studio

On this page
  1. Why Base64 exists
  2. How it works, in steps
  3. Padding and size
  4. Variants: standard and URL-safe
  5. Base64 is not encryption
  6. When to use it, and when not to
  7. Frequently asked questions
  8. Try these tools

Why Base64 exists

Many older protocols and formats, such as email and URLs, were designed for text and can mangle arbitrary bytes. Binary data such as an image or a file can contain bytes that look like control characters or line endings. Base64 solves this by representing any data using only letters, digits, plus and slash: 26 uppercase, 26 lowercase, 10 digits and 2 symbols make 64 characters.

You see it whenever binary data must live inside text: images embedded in HTML or CSS as data URLs, attachments in emails, keys and certificates in PEM files, and the pieces of a JSON Web Token.

How it works, in steps

  1. Take the input as bytes and group them in threes. Three bytes are 24 bits.
  2. Split those 24 bits into four groups of 6 bits. Each 6-bit value is a number from 0 to 63.
  3. Look up each number in the Base64 alphabet (A–Z, a–z, 0–9, +, /).
  4. If the last group has fewer than three bytes, add = signs at the end as padding so the output length is a multiple of four.

Example: the three letters "Man" are bytes 77, 97, 110. They form the 6-bit groups 19, 22, 5 and 46, which map to T, W, F and u. So "Man" encodes to TWFu.

Padding and size

Because three bytes become four characters, Base64 makes data about 33% larger. A 3,000-byte file becomes 4,000 characters, and 1,000 bytes become 1,336 characters, since the last group is padded.

The = padding tells a decoder how many bytes the last group held. "Ma" (two bytes) becomes TWE= and "M" (one byte) becomes TQ==. A longer example: "Hello" is SGVsbG8= and "Hello, World!" is SGVsbG8sIFdvcmxkIQ==.

InputBase64
MTQ==
MaTWE=
ManTWFu
HelloSGVsbG8=
Hello, World!SGVsbG8sIFdvcmxkIQ==
café (UTF-8)Y2Fmw6k=

Variants: standard and URL-safe

The standard alphabet uses + and /, which have special meanings in URLs and file names. The URL-safe variant, called Base64URL, uses - and _ instead and often leaves off the padding. The three bytes 251, 255, 254 encode to +//+ in standard Base64 and -__- in the URL-safe form. JSON Web Tokens use the URL-safe version.

Base64 is not encryption

Anyone can decode Base64 instantly, with no key. It hides nothing. Basic authentication headers in HTTP use Base64 only to carry the username and password as text, which is why they must always be sent over HTTPS. If you need secrecy, use real encryption; if you need to check integrity, use a hash or a signature.

The same applies to text with accents or other non-English characters: encode it to bytes first (usually UTF-8) and then to Base64, and decode in the reverse order. Skipping that step is the most common source of garbled results.

When to use it, and when not to

  • Good uses: small images inline in CSS or HTML, carrying binary data in JSON, email attachments, and tokens.
  • Poor uses: large images on a web page, where the 33% overhead and the loss of caching make a normal file better.
  • Never use it to "protect" passwords or secrets.
  • Prefer well-tested library functions over writing your own encoder.

Frequently asked questions

Is Base64 secure?

No. It is a reversible encoding that anyone can decode without a key. It is for compatibility, not for secrecy.

Why does Base64 end with equals signs?

They are padding. Base64 works in groups of three bytes, and when the input length is not a multiple of three, = characters fill the last group so the output length stays a multiple of four.

Why is the Base64 version longer?

Every 3 bytes become 4 characters, so the output is about one third larger than the input.

Can I Base64 encode an image?

Yes. The result can be used in a data URL such as data:image/png;base64,... which is handy for tiny icons, but for large images a normal file is more efficient.

Try these tools

Keep reading