Tips & Tricks

Base64 Encoding Explained: What It Is and When to Use It

By AllToolz4U Team · · Updated · 4 min read

Base64 Encoding Explained: What It Is and When to Use It

If you have worked with APIs, email, tokens or web pages with embedded images, you have seen long strings of letters and numbers ending in one or two equals signs. That is Base64. It looks mysterious, so people often assume it is a form of security. It is not, and understanding what it really does will save you from a few classic mistakes.

What Base64 is

Base64 is a way to represent binary data (bytes) using only 64 safe, printable characters: the letters A to Z and a to z, the digits 0 to 9, and the symbols + and /. The = sign is used as padding at the end.

The reason it exists is practical. Many systems were built to carry plain text and can mangle or reject arbitrary bytes. Email is the classic example: it was designed for text, so attachments are converted to Base64 before sending. By turning any data into harmless text characters, Base64 lets it travel through text-only channels without corruption.

How it works, simply

  1. Take the data as bytes and split it into groups of three bytes (24 bits).
  2. Split those 24 bits into four groups of 6 bits.
  3. Each 6-bit group is a number from 0 to 63, which maps to one of the 64 characters.
  4. If the last group has fewer than three bytes, add = padding so the output length is a multiple of four.

Example: the text Man becomes TWFu. The word Hi becomes SGk= (one padding character because only two bytes were left).

It makes data bigger

Three bytes become four characters, so Base64 output is about 33% larger than the original. A 300 KB image becomes roughly 400 KB of text. That overhead is the price of being able to send the data as text, so use Base64 where you need it, not everywhere.

Base64 is not encryption

This is the most important point. Base64 is an encoding, not encryption. There is no key and no secret. Anyone can decode a Base64 string instantly, using any online tool or one line of code.

  • Do not use Base64 to "hide" passwords, API keys or personal data.
  • HTTP Basic Authentication sends the username and password as Base64. That is only acceptable over HTTPS, because the encoding itself offers no protection.
  • If you need confidentiality, encrypt the data with a proper algorithm first. You may then Base64-encode the encrypted result so that it can be stored or sent as text.

Where you will meet Base64

WhereWhat it does there
Email attachments (MIME)Carries files through text-based mail systems
Data URIs in HTML or CSSEmbeds a small image directly in the page: data:image/png;base64,...
JSON Web Tokens (JWT)The header and payload are Base64URL encoded, which is readable by anyone who has the token
APIs and JSONSends binary files such as images or documents inside a text-based JSON field
Configuration and secrets in tools like KubernetesValues are Base64 encoded, which is formatting, not protection

Standard Base64 versus Base64URL

Standard Base64 uses + and /, which have special meaning in URLs and file names. The URL-safe variant replaces them with - and _ and often omits the padding. JWTs and many web APIs use this form. If a decoder complains about a string, check which variant it is.

Common pitfalls

  • Text and character sets. Base64 encodes bytes, not letters. Text must first be converted to bytes with a known encoding, normally UTF-8. If the sender and receiver disagree, accented characters and emoji come out wrong.
  • Line breaks. Some systems wrap Base64 every 76 characters. A decoder that does not expect line breaks may fail, so strip them if needed.
  • Missing padding. Cut-off strings or removed = signs can cause decode errors.
  • Embedding big images. A Base64 image inside a web page cannot be cached separately and adds a third to the size. For anything larger than a tiny icon, use a normal image file.
  • Treating it as validation. A string that looks like Base64 may be anything. Always validate decoded content before using it.

When to use it and when not to

Use it when you must move binary data through a text-only channel, such as a JSON API field, an email body or a URL parameter (with the URL-safe variant).

Avoid it when you can send the raw bytes directly (file uploads, binary protocols), when size matters, or when you want to keep something secret.

A few examples to check your understanding

TextBase64Why
ManTWFuThree bytes give exactly four characters
HiSGk=Two bytes, so one padding character
HelloSGVsbG8=Five bytes, one padding character

You can decode any of these in your browser console with atob("SGVsbG8="), which returns the original text. The matching function for encoding is btoa("Hello"). Keep in mind that these two functions work with simple characters only; text with accents or emoji must be converted to UTF-8 bytes first.

How to encode and decode safely

Every programming language has Base64 built in, and most code editors and browsers can do it too. Online encoders are fine for quick checks of harmless data. Do not paste secrets, tokens or private files into a website you do not trust; a decoder that runs in your browser or on your own machine is safer.

Frequently Asked Questions

Is Base64 encryption?

No. Base64 is a reversible encoding with no key or password. Anyone can decode it, so it provides no confidentiality.

Why does Base64 make files bigger?

It represents every three bytes with four text characters, so the output is about 33 percent larger than the original data.

What does the equals sign at the end mean?

It is padding. When the data does not divide evenly into groups of three bytes, one or two equals signs are added so that the output length is a multiple of four.

What is Base64URL?

A URL-safe variant that uses - and _ instead of + and /, and usually omits the padding. It is used in JSON Web Tokens and many web APIs.

Keep reading