What Is Base64?
Base64 is a way of writing binary data using only 64 printable characters. It exists because many systems were built to carry text and break when they meet raw bytes, so the bytes are converted into something safe to pass through.
Why it exists
Email, URLs, JSON and HTML were designed around text. Sending raw binary through them risks characters being altered, truncated or interpreted as control codes.
Base64 removes that risk by using only letters, digits, plus and slash, which survive every text transport intact. The cost is size: the output is about a third larger than the input.
Encode or decode Base64 text in the browser.
Open the Base64 Encoder and Decoder →How the encoding works
Three bytes, 24 bits, are split into four groups of six bits. Each six-bit value, 0 to 63, maps to one character in the alphabet: A to Z, a to z, 0 to 9, then + and /.
Because 24 bits divide evenly into four groups, the output length is always a multiple of four. If the input is not a multiple of three bytes, padding equals signs are added to complete the last group.
The alphabet
| Value | Character | Value | Character |
|---|---|---|---|
| 0 to 25 | A to Z | 26 to 51 | a to z |
| 52 to 61 | 0 to 9 | 62 | + |
| 63 | / | Padding | = |
Two variants replace + and / to make the output safe in URLs: base64url uses - and _ instead. That is why a token you see in a URL sometimes has no plus or slash.
Where you meet it
- Data URIs, where an image is embedded directly in HTML or CSS.
The size cost
Four characters represent three bytes, so the encoded form is 4/3 of the original, plus padding. A 1 MB image becomes about 1.37 MB of text.
Compression is usually applied before encoding, and text compresses well, so the on-the-wire cost after gzip is often much smaller than the raw ratio suggests.
Decoding
Decoding reverses the process: map each character back to six bits, join them, and discard the padding. Any decoder does this, and most languages have it built in.
If a string will not decode, check for line breaks inserted by email systems, for the URL-safe alphabet, and for missing padding. Those three account for nearly every failure.
Padding and why it is needed
The equals signs at the end are not decoration. They tell the decoder how many bytes were in the final incomplete group, so it knows where to stop.
Some systems strip padding, and some decoders accept the stripped form. If you are generating output for another system, follow its documentation rather than assuming.
Common mistakes
- Thinking Base64 compresses data.
- Assuming it hides the content.
- Mixing the standard and URL-safe alphabets.
- Forgetting padding where it is required.
- Encoding binary data that could be sent as binary.
Encoding in code
In JavaScript, btoa and atob work on strings and are limited to Latin-1 characters; for arbitrary bytes use a buffer. In Python, base64.b64encode takes bytes and returns bytes.
In every language the rule is the same: encode bytes, not characters. Encoding a Unicode string directly is the most common source of mangled output.
When not to use it
If the transport can carry binary, send binary. Base64 adds a third to the size for no benefit when the channel is already binary-safe.
Similarly, do not base64 a large file just to store it in a text column if a blob column exists. The database will do it better.
A quick sanity check
If a string consists only of letters, digits, plus, slash and trailing equals, and its length is a multiple of four, it is probably Base64. Decode it and look at the first few bytes before assuming what it contains.
Recognising it in the wild
A long string of letters, digits, plus and slash, ending in one or two equals signs, is almost certainly Base64. Decoding it is a single command and needs no key.
That is why it appears in tokens, data URIs and email headers, and why it should never be used for anything secret.
Frequently asked questions
Does Base64 make data larger?
Yes, by about a third.
Is Base64 the same as encryption?
No. It is reversible by anyone with no key at all.
What is base64url?
The same encoding with - and _ instead of + and /, for safe use in URLs.