Featured image: A computer program displayed on a computer screen. Photo by Al Nahian on Pexels (Free to use).

Base64 is one of those technologies everyone uses and almost no one can define. It shows up in JSON Web Tokens, in data: URLs, in email attachments, in the middle of API payloads — and it generates the same confusion everywhere it appears: is this encryption? Is it compression? Why does my text get 33% longer? The answer to all three: Base64 is neither encryption nor compression. It is a translation layer that lets arbitrary binary data survive a trip through text-only channels, and the 33% overhead is the price of that survival.

This guide explains what Base64 actually does, walks through the encoding step by step, and shows where you will meet it in real systems. For hands-on conversion with correct UTF-8 handling, use the free Base64 Encoder & Decoder on this site — it runs entirely in your browser.

What Base64 Is, in One Minute

Base64 is a binary-to-text encoding: it represents arbitrary bytes using only 64 printable ASCII characters. The alphabet:

A–Z (26) + a–z (26) + 0–9 (10) + "+" and "/" (2) = 64 characters

= is used as padding at the end. Every three input bytes (24 bits) become four output characters (4 × 6 bits = 24 bits), which is why the output is always about 33% larger than the input — 4 characters where 3 bytes used to be.

The encoding is fully reversible and deterministic: the same bytes always produce the same text, and any valid Base64 text decodes back to the original bytes exactly. That reversibility is the whole point — and it is also why Base64 is not a form of encryption. Anyone who recognizes the alphabet can decode it by hand.

Why Base64 Exists: The Text-Channel Problem

Computer systems are surprisingly bad at agreeing on what a byte means. A byte value of 0x89 is “the first byte of a PNG header” in one place, “a control character that terminates a string” in another, and “something to strip out” in a third. Channels designed for text — JSON strings, XML documents, email bodies, URL parameters, database text columns — make no guarantee that arbitrary bytes survive intact: they may reject bytes outside a safe range, reinterpret them in the wrong character encoding, or stop at the first null byte.

Base64 solves this by mapping every possible byte value into the 64 “universally safe” characters that every text system has agreed to pass through unchanged. The cost is size; the benefit is that the data arrives identical to how it left.

How the Encoding Works, Bit by Bit

Take the three bytes 01101101 01100001 01101110 (the ASCII for “man”). The algorithm:

  1. Concatenate the three bytes into 24 bits: 011011010110000101101110.
  2. Split into four 6-bit groups: 011011, 010110, 000101, 101110.
  3. Map each 6-bit value (0–63) to its alphabet character: 27 → b, 22 → W, 5 → F, 46 → u. Result: bWFu.

What about inputs that are not a multiple of three bytes? The encoder pads with zero bits and marks the shortage with =:

  • One byte remaining → 2 output characters + ==
  • Two bytes remaining → 3 output characters + =

This is why Base64 text often ends in = or == — the padding tells the decoder exactly how many real bytes the last group contained. Some modern implementations (notably Base64url) omit the padding entirely and still decode correctly, because the length of the string already encodes the information.

Base64 vs Base64url: The URL-Safe Variant

Standard Base64 has a problem in URLs and filenames: +, /, and = are meaningful characters there. + becomes a space in query strings, / breaks path segments, and = is interpreted as an assignment in some contexts.

Base64url — standardized in RFC 4648 — replaces + with -, / with _, and drops the padding. It is the variant used in JWTs, in modern data-URL conventions, and anywhere encoded data must survive a URL trip unmodified. If you see Base64 text with dashes and underscores and no trailing =, you are looking at Base64url.

The practical rule: use standard Base64 for storage and email-style transport; use Base64url for URLs, query parameters, and tokens. Decoding software often accepts both, but producing the right variant avoids an entire class of bugs.

Base64 Is Not Encryption

The most persistent misunderstanding, so it deserves its own section. Three differences matter:

  1. No key. Encryption requires a secret key; Base64 has none. Anyone can decode it.
  2. Reversible by definition. Encryption is designed to be irreversible without the key. Base64 is designed to be reversible by everyone, always.
  3. Different purpose. Encryption protects confidentiality. Base64 protects integrity through transport — it keeps bytes from being mangled, not from being read.

Treating Base64 as security is a real-world vulnerability class: credentials “protected” by Base64 appear in code, in logs, and in Basic-auth headers where anyone can decode them. When a system uses Authorization: Basic base64(user:pass), the Base64 is transport hygiene for the HTTP layer — the actual protection must come from TLS underneath.

Where You Meet It in Practice

JSON Web Tokens. Every JWT is header.payload.signature, with the first two parts Base64url-encoded JSON. Decode them with the JWT decoder — you will see the encoding is the entire mechanism that makes a binary-safe token out of JSON text.

Data URLs. data:image/png;base64,iVBOR... embeds binary files directly inside HTML and CSS. The technique is convenient for small assets but bloats documents by the same 33% — and browsers must decode before rendering.

API payloads. JSON cannot carry raw binary, so images, certificates, and protobuf messages travel as Base64 strings inside JSON objects. This is the standard pattern in REST APIs and in many LLM tool-calling schemas that accept files.

Email (MIME). Attachments were historically encoded as Base64 because SMTP was a text-only protocol. Modern email still uses it under the hood — a pattern that survives everywhere legacy text channels do.

Common Pitfalls and How to Avoid Them

Whitespace and line breaks. Some encoders wrap output at 76 characters (MIME convention). A decoder that does not strip whitespace first will fail on line-wrapped input. Strip before decoding.

UTF-8 handling. The classic browser mistake: btoa("日本語") throws, because btoa only accepts Latin-1. The correct path encodes the string to UTF-8 bytes first, then applies Base64 — which is exactly what our Base64 tool does. The same bug appears in older Python 2 code and explains many “garbled Chinese characters in the middle of my payload” incidents.

Encoding text vs. encoding bytes. When you Base64-encode a string, you are really encoding the bytes of its character encoding. If the decoder assumes a different encoding (UTF-8 vs Shift-JIS vs Latin-1), the output mojibakes. Always know the encoding of what you encoded.

Confusing variants. Standard Base64 (+ / =) vs Base64url (- _ no padding) — decode the wrong one and you get “invalid input” or silently corrupted bytes, especially at the padding boundary.

Debugging Base64 in the Wild

When a Base64 blob misbehaves, the fastest loop: decode it, look at what you get, re-encode what you expected, and diff. A JWT that fails signature verification is usually not a broken signature — it is the header or payload being decoded with the wrong variant or wrong character encoding, which changes the bytes and therefore the signature check. Run the token through the JWT decoder first; if the JSON looks clean, the problem is elsewhere.

For data payloads, the pair that covers the whole workflow: Base64 Encoder & Decoder for the conversion, and the JSON formatter for inspecting the result when it is JSON. Neither uploads anything — both run locally.

Base64 in Code: Every Language Ships an Encoder

You almost never write the algorithm yourself — every platform ships an implementation, and the differences between them are where bugs hide:

LanguageEncodeDecodeUTF-8 note
JavaScript (browser)btoa(binaryString)atob(s)btoa is Latin-1 only — encode UTF-8 bytes first
Node.jsBuffer.from(s).toString('base64')Buffer.from(s, 'base64').toString()UTF-8 handled correctly
Pythonbase64.b64encode(b)base64.b64decode(s)Work on bytes, not str
Gobase64.StdEncoding.EncodeToStringbase64.StdEncoding.DecodeStringURL variant via URLEncoding
Javajava.util.Base64.getEncoder()java.util.Base64.getDecoder()URL variant via getUrlEncoder()
Shellbase64 / openssl base64 -ebase64 -d / openssl base64 -dWatch for 76-column line wrapping

The recurring failure modes across all of them:

  1. Encoding a string instead of its bytes. In Python 2, base64.b64encode("日本語") encoded the wrong thing entirely; the correct pattern is b64encode("日本語".encode("utf-8")). The Python 2/3 transition era produced a generation of double-encoded or mojibaked payloads that still surface in legacy integrations.
  2. Line wrapping on decode. base64 -d tolerates whitespace, but strict decoders do not. MIME-wrapped input must be unwrapped before decoding.
  3. Variant mismatch. JWT libraries expect Base64url; passing standard Base64 with +/= into a URL context produces + → space corruption. Always match the variant to the channel.
  4. Silent corruption on invalid input. Some decoders skip invalid characters instead of erroring — a "abc$def" input decodes to something rather than nothing. For security-sensitive decoding (tokens, signatures), use a strict decoder that rejects anything outside the alphabet.

Performance and Alternatives

Base64 is not the only binary-to-text game. The alternatives exist for the cases where its 33% overhead hurts:

  • Hex (Base16) — 2 characters per byte, 100% overhead, but trivially debuggable. Use for short digests and keys you want humans to compare visually; avoid for bulk data. (SHA-256 digests are conventionally hex — see the hash guide.)
  • Base85 (Ascii85) — 5 characters per 4 bytes, only 25% overhead. Used inside PDF and PostScript files, but the alphabet includes backslash and quotes, making it fragile in text contexts.
  • Base32 — 8 characters per 5 bytes, 60% overhead, case-insensitive, omits look-alike characters (0/1). The choice for human-typed codes: software licenses, TOTP secrets, recovery keys.

The decision rule is simple: Base64 for transport through text systems, Base64url for URLs, hex for short human-comparable values, Base32 for human-typed codes. Each variant optimizes for a different failure mode — pick the one whose failure mode matches your channel.

Frequently Asked Questions


Encoding details per RFC 4648. Base64url usage described as standardized in RFC 4648 and adopted by JWT (RFC 7519). MIME conventions per RFC 2045.