Converters5 min read

Base64 Encoding Explained: How It Works and When to Use It

Understand what Base64 encoding is, how it converts binary data to text, common use cases in web development, and how to encode or decode Base64 instantly online.

Try the free online tool mentioned in this guide:Base64 Encoder / Decoder

What is Base64 encoding?

Base64 is a binary-to-text encoding scheme that represents binary data as a string of ASCII characters. It uses a set of 64 printable characters (A–Z, a–z, 0–9, +, /) to encode every 3 bytes of binary data into 4 ASCII characters.

The result is about 33% larger than the original binary data but can be safely transmitted over systems designed to handle text — like HTTP headers, JSON fields, URLs, and email attachments.

How Base64 works

The encoding process takes 3 bytes (24 bits) at a time and splits them into four 6-bit groups. Each 6-bit value (0–63) maps to a character in the Base64 alphabet. If the input is not divisible by 3, padding characters (=) are added to make the output length a multiple of 4.

javascript
// "Man" in ASCII is 77 97 110
// Binary: 01001101 01100001 01101110
// Split into 6-bit groups: 010011 010110 000101 101110
// Map to Base64 alphabet:   T      W      F      u
// Result: "TWFu"

btoa("Man") // → "TWFu"
atob("TWFu") // → "Man"

Common use cases in web development

Embedding images in HTML/CSS: Data URLs use Base64 to embed image bytes directly without a separate HTTP request. Useful for small icons and inline SVGs in critical CSS.

Authentication: HTTP Basic Auth encodes username:password as Base64 in the Authorization header. Note: this is encoding, not encryption — always use HTTPS.

JWT tokens: The header and payload sections of a JWT are Base64URL-encoded (a variant that uses - and _ instead of + and /).

Email attachments: MIME encoding uses Base64 to attach binary files (images, PDFs) in email messages.

Binary data in JSON: Since JSON only supports text, binary payloads (audio, images, file buffers) are often Base64-encoded before being embedded in a JSON field.

Base64 vs Base64URL

Standard Base64 uses + and / which are special characters in URLs. Base64URL replaces them with - and _ and omits the = padding. This makes Base64URL safe to use in URL query parameters and path segments without percent-encoding.

JWTs always use Base64URL. When decoding a JWT payload manually, use Base64URL decoding, not standard Base64.

javascript
// Standard Base64 might produce: "a+b/c=="
// Base64URL equivalent:          "a-b_c"

// JavaScript (Node.js / modern browsers)
Buffer.from("hello world").toString("base64url")
// → "aGVsbG8gd29ybGQ"

Encoding vs encryption

Base64 is not encryption. It is a reversible encoding that anyone can decode without a key. Never use Base64 to protect sensitive data. Always use proper encryption (AES, RSA) for secrets, and use Base64 only to make binary data text-safe for transport.

Frequently asked questions

Is Base64 a form of encryption?

No. Base64 is a reversible encoding — anyone can decode it without a key. It is used to make binary data safe for text-based systems, not to protect data.

Why does Base64 end with == sometimes?

Base64 works on groups of 3 bytes. If the input length isn't divisible by 3, = padding characters are added to the output to make it a multiple of 4 characters.

What is the difference between Base64 and Base64URL?

Base64URL replaces + with - and / with _ and omits = padding. This makes it safe to use in URLs and HTTP headers without percent-encoding. JWTs use Base64URL.

Can I encode a file to Base64 online?

Yes. MyDevTools Base64 encoder lets you paste text or load a file and produces the Base64 output in the browser without uploading anything to a server.

Try Base64 Encoder / Decoder for free

Encode text to Base64 or decode Base64 strings instantly, with UTF-8 support. Runs entirely in your browser. No install, no account required to try it.