What is Base64? Encoding Explained
Base64 is a way of representing binary data — images, files, encrypted bytes, anything — using only 64 printable characters (A–Z, a–z, 0–9, + and /). It exists because many channels were designed for text: email, JSON, XML, URLs, HTTP headers and cookies can all carry a Base64 string safely where raw bytes would be corrupted or rejected.
This guide explains how the encoding works, when it is the right choice, the difference between Base64 and Base64URL, and the mistakes that produce garbled output — including the most important one: Base64 is not encryption.
How Base64 encoding works
Base64 takes the input three bytes (24 bits) at a time and splits them into four groups of six bits. Each six-bit value (0–63) is mapped to one character of the alphabet. Three bytes in, four characters out — which is why Base64 output is always about 33% larger than the input.
When the input length is not a multiple of three, the last group is padded with zero bits and one or two "=" characters are appended so the decoder knows how many real bytes to expect.
Text: Hi!
Bytes: 01001000 01101001 00100001
Groups: 010010 000110 100100 100001
Index: 18 6 36 33
Base64: S G k h → "SGkh"
Text: Hi (2 bytes → one "=" of padding)
Base64: SGk=Base64 is not encryption
Anyone can decode Base64 instantly with no key — it is a reversible encoding, like writing a number in hexadecimal. It hides nothing. A password, API key or token that is "protected" with Base64 is exposed to anyone who sees it. Use real encryption (AES, TLS) to protect data and Base64 only to transport it.
When to use Base64
- Embedding small images or fonts in HTML, CSS or JSON as data: URIs.
- Sending binary content inside a JSON or XML payload, which can only hold text.
- HTTP Basic authentication: the header carries Base64 of "username:password" (transport only — always over HTTPS).
- JWT and other tokens, which use the URL-safe variant for their header, payload and signature.
- Email attachments (MIME) and any legacy protocol that expects 7-bit text.
- Not for large files where a binary channel exists — the 33% overhead and the extra encoding step are pure waste.
Base64 vs Base64URL
Standard Base64 uses "+" and "/", both of which have special meaning in URLs, and "=" padding, which is often percent-encoded. Base64URL (RFC 4648 §5) swaps them for "-" and "_" and usually drops the padding, so the result can be used in a URL, a file name or a JWT without further escaping. A decoder that expects one variant will reject the other, which is the most common reason a "valid" token fails to decode.
Encoding text: watch the character set
Base64 encodes bytes, not characters, so text must be turned into bytes first — almost always UTF-8. Arabic, Hindi and emoji become multi-byte sequences; if the encoder uses UTF-8 and the decoder assumes Latin-1 (or the reverse) the output is mojibake. When two systems disagree about a Base64 string, the character set is the first thing to check.
Why decoding fails
- Wrong alphabet — a Base64URL string ("-", "_") fed to a standard decoder, or vice versa.
- Missing or extra padding — some encoders omit "=", and strict decoders require a length that is a multiple of four.
- Whitespace or line breaks inside the string — MIME wraps lines at 76 characters; strip them before decoding.
- Double encoding — a string that was Base64-encoded twice looks valid but decodes to more Base64.
- It was never Base64 — hex, a hash or a random token can look similar. Base64 uses exactly the 64 characters plus "=", and its length is a multiple of four when padded.
Frequently asked questions
Is Base64 secure?
No. It is a reversible encoding with no key. It provides no confidentiality whatsoever; use encryption for that.
Why does Base64 end with "=" or "=="?
Padding. The input is processed in groups of three bytes; when one or two bytes are left over, "==" or "=" is appended so the decoder knows the true length.
How much bigger is Base64 output?
About 33% — every 3 bytes become 4 characters. A 300 KB image becomes roughly 400 KB of text.
Can Base64 encode an image or a PDF?
Yes — Base64 works on any bytes. That is how data: URIs embed images in HTML and how files travel inside JSON APIs.
What is the difference between Base64 and hex?
Both represent bytes as text. Hex uses 16 characters and doubles the size (2 characters per byte); Base64 uses 64 characters and grows the size by a third. Hex is easier to read; Base64 is more compact.
What is the difference between Base64 and URL encoding?
URL (percent) encoding escapes only the characters that are unsafe in a URL, leaving ordinary letters untouched. Base64 re-encodes every byte. They are often combined: a Base64 string may need percent-encoding before it goes into a query parameter, unless the URL-safe variant is used.
Tools mentioned in this guide
Encode text or files to Base64 (standard or URL-safe) in your browser.
Decode Base64 or Base64url strings to text, with automatic padding repair.
Percent-encode text for safe use in URLs, query strings and form data.
Convert text to hexadecimal bytes (UTF-8) with custom separators and casing.
Convert text to binary (bits) and binary back to text.
Decode JWT header and payload, inspect claims and check expiration — entirely in your browser.