Base64 Encode/Decode

← Tools

What Is Base64 Encoding?

Base64 is a binary-to-text encoding scheme that represents binary data using a set of 64 printable ASCII characters. It was designed to carry binary data across channels that only reliably support text, such as email (MIME), URLs, JSON payloads, and HTTP headers.

The name "Base64" comes from the fact that the encoding uses a 64-character alphabet: A–Z, a–z, 0–9, +, and /, with = used for padding. This alphabet was carefully chosen because every character in it is safe in virtually all text-based transmission systems without requiring additional escaping.

Base64 is defined in RFC 4648 and has been a foundational internet standard since the early days of MIME email in the 1990s. Today it's used everywhere from embedding images in CSS to transmitting cryptographic keys in API requests.

How the Algorithm Works

The Base64 encoding process converts every three bytes (24 bits) of input into four Base64 characters (6 bits each). Here's the step-by-step process:

Step 1: Group input bytes. Take the input data three bytes at a time. Three bytes give you 24 bits of data to work with.

Step 2: Split into 6-bit groups. Divide the 24 bits into four groups of 6 bits each. Each 6-bit group can represent a value from 0 to 63.

Step 3: Map to characters. Each 6-bit value is looked up in the Base64 alphabet table to produce a printable character. For example, 0 maps to A, 25 to Z, 26 to a, and 63 to /.

Step 4: Handle padding. If the input length isn't a multiple of three, padding is applied. One remaining byte produces two Base64 characters plus ==; two remaining bytes produce three Base64 characters plus =. The padding ensures the encoded output length is always a multiple of four.

Because every 3 bytes become 4 characters, Base64 encoding increases data size by approximately 33%. This overhead is the trade-off for text-safe representation.

Common Use Cases

Data URIs. You can embed small images, fonts, or files directly in HTML and CSS using the data: URI scheme, which Base64-encodes the file content. This eliminates an extra HTTP request at the cost of increased document size. It's most effective for small assets under ~5 KB.

Email attachments. The MIME standard uses Base64 to encode binary attachments (images, PDFs, zip files) so they can travel through email systems that only handle 7-bit ASCII text. Every attachment you've ever sent was Base64-encoded behind the scenes.

API authentication headers. HTTP Basic Authentication encodes the username:password string in Base64 and sends it in the Authorization header. Note that this is encoding, not encryption—always use HTTPS to protect credentials in transit.

JSON Web Tokens (JWT). JWTs use a URL-safe variant of Base64 (Base64url) to encode the header and payload segments. Decoding these segments reveals the JSON claims inside the token.

Storing binary in text formats. When you need to embed binary data (cryptographic keys, certificates, serialized objects) in JSON, XML, YAML, or environment variables, Base64 provides a clean text representation that won't break parsers or introduce encoding issues.

Base64 vs Base64url

Standard Base64 uses + and / as the 62nd and 63rd characters in its alphabet. These characters have special meaning in URLs (+ is interpreted as a space in query strings, / is a path separator), which causes problems when Base64 data is used in URLs, filenames, or cookies.

Base64url (defined in RFC 4648, Section 5) solves this by replacing + with - and / with _. Padding (=) is often omitted since the original length can be inferred. JWTs, OAuth tokens, and many modern APIs use Base64url exclusively.

When working with tokens or URL parameters, always verify which variant is expected. Mixing them up produces data corruption that can be difficult to diagnose since the output looks almost correct.

Best Practices & Things to Avoid

Base64 is not encryption. This is the most common misconception. Base64 is a reversible encoding—anyone can decode it instantly. Never use Base64 to protect sensitive data. If you need confidentiality, use proper encryption (AES, ChaCha20, etc.) and only then Base64-encode the ciphertext for transport if needed.

Mind the size overhead. The ~33% size increase means a 1 MB image becomes ~1.33 MB when Base64-encoded. For large files, this overhead makes Base64 impractical compared to direct binary transfer. Data URIs are best reserved for small icons, SVGs, and tiny assets.

Handle line breaks correctly. Some Base64 implementations insert line breaks every 76 characters (per the MIME standard). Others produce a single unbroken string. When decoding, make sure your decoder handles both formats, or strip line breaks before decoding.

Use built-in functions. Every major language has native Base64 support: btoa()/atob() in JavaScript, base64_encode()/base64_decode() in PHP, the base64 module in Python. Don't roll your own implementation.

Related Tools

Base64 encoding is often one link in a longer data-processing chain. These tools cover adjacent tasks:

  • URL Encoder/Decoder — When you need to percent-encode a Base64 string for safe inclusion in a URL query parameter.
  • Hash Generator — Generate SHA-256 or SHA-512 hashes of data. Unlike Base64, hashing is one-way and used for integrity verification.
  • JWT Parser — Decode JSON Web Tokens, whose header and payload segments are Base64url-encoded JSON.

Frequently Asked Questions

Is my data sent to a server when I encode or decode?

No. All Base64 encoding and decoding happens in your browser using JavaScript. Your data stays on your machine and is never transmitted.

Why does my Base64 string end with = or ==?

The equals signs are padding. Base64 output must be a multiple of 4 characters. If the input byte count isn't divisible by 3, one or two padding characters are appended to reach the correct length.

Can I Base64-encode binary files like images?

Yes. Any binary data can be Base64-encoded. This is how data URIs embed images in HTML/CSS and how email attachments work. Just remember the 33% size increase.

Is Base64 the same as encryption?

Absolutely not. Base64 is a fully reversible encoding with no secret key. Anyone can decode Base64 data. It provides zero security. For protecting sensitive information, use proper encryption algorithms.

What characters are safe in Base64 output?

Standard Base64 uses A–Z, a–z, 0–9, +, /, and = (for padding). Base64url replaces + with - and / with _, and often omits padding. Both variants are safe for text-based transport.