What Is Base64 Encoding? Simple Explanation and Examples

If you've looked inside an email attachment, a web page's code or an API request, you may have seen long strings like SGVsbG8sIFdvcmxkIQ==. That's Base64. This guide explains what it is, why it's used, and the most common misunderstanding about it.

The problem Base64 solves

Computers store everything, including images, PDFs and text, as bytes. Many older systems, such as email and some data formats, were designed to carry only plain, readable text. Sending raw bytes through them could corrupt the data. Base64 solves this by converting any bytes into a limited set of 64 safe characters:

  • A–Z (26)
  • a–z (26)
  • 0–9 (10)
  • + and / (2)

The = sign is used as padding at the end when needed.

How it works (simplified)

  1. Take the data 3 bytes (24 bits) at a time.
  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.

Because every 3 bytes become 4 characters, Base64 makes data about 33% larger.

Example

The text Hi! is three bytes. In Base64 it becomes SGkh. The text Hello, World! becomes SGVsbG8sIFdvcmxkIQ==. The two = signs are padding because the input wasn't a multiple of 3 bytes.

Try it yourself with our Base64 Encoder/Decoder. It supports Filipino and other non-English characters (UTF-8), such as "ñ".

Where Base64 is used

  • Email attachments: files are Base64-encoded inside the email message (MIME).
  • Data URLs: small images can be embedded directly in HTML or CSS, like data:image/png;base64,iVBOR....
  • APIs and JSON: binary data such as images or signatures is often sent as a Base64 string inside JSON.
  • HTTP Basic Authentication: the username and password are joined and Base64-encoded in a header. This is why Basic Auth must always be used with HTTPS.
  • Tokens: parts of JSON Web Tokens (JWTs) use a URL-safe variant of Base64.

Base64 is NOT encryption

This is the most important thing to remember. Base64 is encoding, a reversible change of format that anyone can undo instantly. It provides no security at all. Never use Base64 to "hide" passwords, personal information or secrets. For protecting data, use real encryption (such as AES) and for passwords, use a proper hashing algorithm (such as bcrypt or Argon2).

URL-safe Base64

The + and / characters have special meanings in web addresses. A variant called Base64URL replaces them with - and _ and often drops the padding. You'll see it in JWTs and some APIs. If decoding fails, check which variant you have.

Using Base64 in code

// JavaScript (browser)
btoa("Hi!")        // "SGkh"
atob("SGkh")       // "Hi!"

# Python
import base64
base64.b64encode(b"Hi!")   # b'SGkh'

Note that JavaScript's btoa only handles plain Latin characters. For other characters, encode the text as UTF-8 bytes first, which our tool does automatically.

More guides