What is Base64 Encoding? How It Works & When to Use It

Utilko Team 5 min read Developer

What Is Base64?

Base64 is a binary-to-text encoding scheme that converts binary data into a string of ASCII characters. It uses a set of 64 printable characters — the uppercase letters A–Z, lowercase a–z, digits 0–9, plus + and /, with = used for padding. The name literally means "base 64" because it represents data using 64 distinct symbols.

Base64 was originally designed so that binary data could be safely transmitted over channels that only support text, such as email (MIME) and older HTTP implementations.

How Does Base64 Work?

The encoding process follows a straightforward algorithm:

  1. Take three bytes (24 bits) of input data.
  2. Split them into four groups of 6 bits each.
  3. Map each 6-bit value to a character in the Base64 alphabet (A = 0, B = 1, ... / = 63).
  4. If the input length is not a multiple of three, pad the output with one or two = signs so the result length is always a multiple of four.

For example, the text Hi (two bytes: 0x48 0x69) becomes SGk= in Base64. Decoding reverses the process: each character is mapped back to its 6-bit value, the bits are reassembled into bytes, and the padding is stripped.

When to Use Base64

Base64 appears in many everyday scenarios:

  • Email attachments — MIME encodes binary files (images, PDFs) as Base64 so they can travel through text-only SMTP servers.
  • Data URIs — embedding small images directly into HTML or CSS with data:image/png;base64,... eliminates an extra HTTP request.
  • JSON payloads — since JSON only supports text, binary blobs like cryptographic keys or file uploads are often Base64-encoded before being included in the payload.
  • Basic HTTP authentication — the Authorization: Basic header carries username:password encoded in Base64.
  • JWT tokens — the header and payload sections of a JSON Web Token are Base64url-encoded.

When NOT to Use Base64

Base64 is not encryption. It provides zero security because anyone can decode it instantly. Never use Base64 to hide passwords or sensitive data. Also, Base64 increases the size of the data by roughly 33%, so it is not ideal for large files where bandwidth matters.

Try It Now

Use our free Base64 Encoder/Decoder to convert text or files to and from Base64 instantly.

Base64 Encoder/Decoder →

Base64 in Different Languages

  • JavaScript (browser): btoa(string) to encode, atob(string) to decode.
  • Node.js: Buffer.from(string).toString('base64')
  • Python: import base64; base64.b64encode(data)
  • Command line: echo -n "Hello" | base64

Base64url vs Standard Base64

Standard Base64 uses + and /, which are special characters in URLs. Base64url replaces them with - and _ and omits padding, making the output safe to use in query strings, filenames, and JWT tokens without additional encoding.

Conclusion

Base64 is a simple yet essential encoding that bridges the gap between binary data and text-only systems. Understanding how it works helps you debug emails, JWTs, data URIs, and API payloads more effectively. Try our Base64 Encoder/Decoder to convert data in seconds.

Tools Mentioned in This Article