Base64 Encoder & Decoder
Encode text or files to Base64 and back, with correct UTF-8 handling and support for URL-safe Base64url and data URIs.
Paste your text here, or drag a file…
Drag & drop a file, or paste content above
Output will appear here
How Base64 works, and why output is 33% larger
Base64 solves one problem: moving arbitrary binary data through a channel that only reliably carries text. Email headers, JSON string fields, XML documents, URLs and HTTP headers all have this constraint.
The mechanism is simple. Take three bytes — 24 bits. Split those 24 bits into four groups of six. Each 6-bit group has 64 possible values, so map each to one character from the alphabet A–Z a–z 0–9 + /. Three input bytes always become four output characters.
That ratio is where the overhead comes from: 4 ÷ 3 = 1.333, so Base64 output is always about 33% larger than the input. A 3 MB image becomes 4 MB of Base64. This is not a tunable parameter; it is arithmetic.
When the input length is not a multiple of three, the final group is padded with = characters — one = if two bytes remain, two if one byte remains. Padding carries no data; it only makes the length a multiple of four, which some strict decoders require.
Base64 variants
Several alphabets exist. Mixing them up is the most common decode failure.
| Variant | Characters 62 & 63 | Padding | Used by |
|---|---|---|---|
| Standard (RFC 4648 §4) | + and / | Yes | MIME, most APIs, data URIs |
| Base64url (RFC 4648 §5) | - and _ | Usually omitted | JWTs, URL parameters, filenames |
| MIME (RFC 2045) | + and / | Yes | Email — line-wrapped at 76 characters |
| Base32 | A different alphabet entirely | Yes | TOTP secrets, case-insensitive contexts |
A JWT segment pasted into a strict standard-Base64 decoder typically fails on the missing padding or on a - / _ character. That is a variant mismatch, not corruption.
The UTF-8 trap in JavaScript
btoa() and atob() predate widespread Unicode and operate on Latin-1. Passing them non-ASCII text throws or corrupts silently.
btoa('café');
// InvalidCharacterError: String contains characters
// outside of the Latin1 range
btoa('日本語'); // same failure// Encode: text -> UTF-8 bytes -> Base64
function encode(text) {
const bytes = new TextEncoder().encode(text);
return btoa(String.fromCharCode(...bytes));
}
// Decode: Base64 -> UTF-8 bytes -> text
function decode(b64) {
const binary = atob(b64);
const bytes = Uint8Array.from(binary, c => c.charCodeAt(0));
return new TextDecoder().decode(bytes);
}
encode('café'); // 'Y2Fmw6k=' — correctconst toUrl = b64 =>
b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
const fromUrl = s => {
const b64 = s.replace(/-/g, '+').replace(/_/g, '/');
return b64.padEnd(Math.ceil(b64.length / 4) * 4, '=');
};# Encode / decode
echo -n 'hello' | base64
echo 'aGVsbG8=' | base64 -d
# -n matters: without it the trailing newline is encoded too,
# which is why a shell-produced value often fails to match
# one produced by an application.
# Files
base64 -i image.png -o image.txtWhen Base64 is and is not appropriate
- Small inline images (data URIs) —Reasonable for icons under about 2 KB, where saving a round trip beats the 33% size penalty. Above that, a separate cacheable file wins — a data URI cannot be cached independently of the document that contains it.
- Binary in JSON —The standard approach, because JSON has no binary type. Accept the 33% overhead or use a binary protocol.
- HTTP Basic auth —Required by the spec, and worth restating: Basic auth Base64 is not protection. The credentials are recoverable instantly. It is only safe over TLS.
- Large file uploads —A poor fit. Use multipart/form-data or a direct binary body — Base64 wastes a third of the bandwidth and forces the whole payload into memory on both ends.
- Hiding data —Never. Base64 is trivially reversible and recognisable on sight. Anything encoded "for security" is not secured.
About
The Base64 Encoder / Decoder converts plain text, binary data, or uploaded files to Base64-encoded ASCII strings, and decodes Base64 back to the original text. UTF-8 multibyte characters are handled correctly. Base64 is used extensively for embedding images in HTML/CSS as data URIs, encoding binary payloads in JSON APIs, and HTTP Basic Authentication (Authorization: Basic ...). The tool runs entirely in your browser — nothing is uploaded to any server. File upload, clipboard copy, and one-click download are all supported.
How to use
- 1 Type or paste text into the input to encode it to Base64.
- 2 To decode, paste a Base64 string and switch to Decode mode.
- 3 For files, drag and drop or click to browse and upload.
- 4 Copy the output to your clipboard with the copy button.
- What is Base64 encoding used for?
- Base64 encodes binary data as ASCII text so it can be safely transmitted in text-only contexts. Common uses include embedding images directly in HTML or CSS as data URIs, encoding binary payloads in JSON APIs, and encoding credentials in HTTP Basic Authentication headers (Authorization: Basic ...).
- Does Base64 encoding make data secure?
- No. Base64 is an encoding scheme, not encryption. Anyone can decode a Base64 string without a key. Do not use Base64 to protect sensitive data — use proper encryption instead. It is purely a transport format for converting binary data to printable ASCII characters.
- What is the difference between standard and URL-safe Base64?
- Standard Base64 uses + and / characters, which have special meanings in URLs and can cause issues in query strings. URL-safe Base64 replaces + with - and / with _, making the encoded string safe to use directly in URLs and filenames without percent-encoding.
The full guide
More in Encoders & Decoders
See all encoders & decoders.