Skip to content
FreeAIDevTools

Base64 and Base64URL Encoder and Decoder

Updated

How does Base64 encoding work? Base64 represents binary data with 64 safe ASCII characters (A to Z, a to z, 0 to 9, + and /), which makes it about 33 percent larger. The URL-safe variant swaps + and / for - and _, which is what JWTs use. The tool below encodes and decodes both variants, with full UTF-8 support, in your browser.

The Inference Report · Every Tuesday

Know when AI prices change.

One short email every Tuesday: new models, price moves and free tiers that were cut or added. Five minutes, no fluff.

No spam. Unsubscribe anytime.

What is Base64?

Base64 is the binary-to-text encoding defined in RFC 4648. It packs 3 bytes of binary input (24 bits) into 4 ASCII characters drawn from a fixed 64-character alphabet: uppercase A-Z, lowercase a-z, digits 0-9, and the symbols + and /. When the input length is not a multiple of 3, the encoder pads the tail with one or two = characters so the output length is always a multiple of 4. A worked example: the two-byte string “Hi” is hex 48 69, which Base64 encodes to SGk=. That output is three real characters plus one pad. That single pad is the signal that the original payload was 2 bytes, not 3.

How Base64 works

The 33% size overhead is structural, not a quality knob. The 4/3 ratio falls directly out of the encoding rule: every 3 input bytes become 4 output characters, so the output is always 4/3 the size of the input before padding rounds up to the next multiple of 4. There is no lossless way to compress that ratio away while staying within the printable-ASCII guarantee that makes Base64 useful in the first place.

RFC 4648 section 5 defines a URL-safe variant that substitutes - for + and _ for /, because the standard alphabet's + and / both carry meaning inside URLs. The URL-safe variant also commonly drops the trailing = padding, since = is a reserved character in query strings. JWTs (RFC 7519), OAuth state parameters, and any token destined for a URL path or query string travel as URL-safe Base64. Concrete numbers: a 100-byte file encodes to 136 characters of standard Base64 (34 blocks of 4, the last ending in ==) and to 134 characters of URL-safe Base64 once the two trailing = are dropped. Small per token, but it adds up on busy auth endpoints.

Common pitfalls

Three failure modes account for nearly every Base64 bug we see in production code. The encoding is simple, the API surface is small, and yet the same three traps catch new engineers and senior engineers alike. Each one is cheap to avoid once named.

  • Treating Base64 as a security mechanism. It is encoding, not encryption: anyone holding the string recovers the plaintext with one function call, no key, no secret. Tokens, passwords, and API keys must be encrypted (AES-256-GCM, libsodium, WebCrypto) before they go anywhere a third party can read them. Base64 around the ciphertext is fine; Base64 instead of the ciphertext is a vulnerability.
  • Mixing the standard and URL-safe alphabets between encoder and decoder. A value encoded with +/ and decoded with the -_ table either fails or, with lenient decoders that skip unknown characters, quietly returns the wrong bytes. Pick one variant per system boundary and document it. (This tool's decoder accepts both alphabets, with or without padding.)
  • Embedding multi-megabyte images as data: URIs to save one HTTP request. The 33% overhead inflates the HTML, the parser blocks while it ingests the giant string, the browser image cache cannot dedupe the asset across pages, and Largest Contentful Paint suffers. A CDN URL with a long Cache-Control header wins on every repeat visit.

When to use this tool

We reach for the encoder in three concrete situations. The first is inlining a small SVG icon, typically under 5KB, into CSS as a data: URI: the round-trip cost of a separate request usually outweighs the 33% size penalty for assets that small, and the icon ships with the stylesheet that uses it. The second is producing a Basic Auth header value: HTTP's Authorization: Basic scheme expects username:password Base64-encoded, and we paste the literal credential in once, copy the encoded result into a config file, and move on. The third is sanity-checking the middle segment of a JWT during debugging: JWTs are three Base64URL-encoded parts joined by dots, and pasting the payload segment into the decoder reveals the claims as JSON without firing up a full JWT debugger or its npm dependency tree. Paste that JSON into the JSON formatter to read nested claims, and check the exp and iat claims in the epoch converter: RFC 7519 defines both as seconds since 1970-01-01 UTC.

Frequently asked

Is Base64 the same as encryption?

No. Base64 is a reversible encoding defined in RFC 4648: anyone with the string can decode it in one call, no key needed. Use it to move binary data through text-only channels such as email, JSON bodies or URLs, never to hide anything. For confidentiality, use authenticated encryption such as AES-256-GCM via WebCrypto or libsodium.

Why is my encoded output longer than the input?

Base64 turns every 3 bytes into 4 characters, so output is about 33 percent larger before padding. The encoder then pads with = so the length is a multiple of 4: one leftover byte gives two characters plus ==, two leftover bytes give three characters plus =.

What's the difference between standard and URL-safe Base64?

Standard Base64 uses + and / as its last two characters, and both mean something in URLs. The URL-safe alphabet from RFC 4648 section 5 swaps them for - and _ and usually drops the = padding. JWTs, OAuth state values and anything else that ends up in a URL use the URL-safe form, and mixing the two between encoder and decoder corrupts data.

Can I encode binary files like images?

Not in this tool, which encodes text only. In general, small inline icons are a reasonable use: a 4KB PNG becomes a string of about 5.4KB that you can drop into CSS as url('data:image/png;base64,...'). Because of the 33 percent overhead, we only inline assets under about 5KB, where saving a request actually matters.

Should I Base64-encode large images for my website?

No. Inlining a 500KB hero image adds about 670KB to the HTML, delays parsing, bypasses the browser's image cache and hurts Largest Contentful Paint. A normal image URL with a long Cache-Control lifetime is faster on the first visit and free on every repeat visit.

Sources: RFC 4648, Base64 and Base64URL · RFC 7515, Base64URL in JWS and JWT · MDN on btoa(). How we check our data