What is Base64? How it works and what it's for
See how Base64 turns 3 bytes into 4 characters, what the equals sign is for, when to use Base64URL, and why Base64 is not encryption.
You've probably seen strings like YW5hOnNlY3JldDEyMw== in an HTTP header, an image embedded in CSS or the middle of a login token. They look scrambled, but there's nothing secret about them. That's Base64, a way of writing any data using only letters, digits and two symbols.
Knowing what happens under the hood helps you read a token, debug an integration that spits out garbled characters and, above all, avoid using Base64 in the belief that it hides anything. To try the examples yourself, use the Base64 encoder and decoder.
What is Base64?
Base64 is an encoding that turns bytes into text using a 64-character alphabet: A–Z, a–z, 0–9, + and /. It exists because many channels, including email, JSON, URLs and HTTP headers, were designed for text and can mangle arbitrary bytes. Once encoded, any content becomes "safe" text that decodes back exactly.
The standard is defined in RFC 4648. The name comes from the alphabet size: 64 symbols, which means each character carries exactly 6 bits of information.
How 3 bytes become 4 characters
The algorithm works on blocks of 3 bytes (24 bits). It joins the bits, cuts them into four 6-bit pieces and replaces each piece with the matching character from the alphabet. Here's the word "Hi!":
SGkh.Index 18 is S (counting from zero, A is 0), 6 is G, 36 is k and 33 is h. Decoding runs the same steps in reverse: each character becomes 6 bits, and the bits are regrouped into bytes.
What is the equals sign (=) at the end for?
The = is padding. When the input length isn't a multiple of 3 bytes, the last block is incomplete, so Base64 adds one or two = signs to keep the output a multiple of 4 characters long. Padding carries no data. It only signals how many bytes were missing.
| Text | Bytes | Base64 |
|---|---|---|
Hi! | 3 | SGkh |
Hi | 2 | SGk= |
H | 1 | SA== |
When the extra size matters
Because every 3 input bytes become 4 output characters, an increase of about 33% plus any padding. A 300 KB image embedded as Base64 takes up roughly 400 KB in your HTML or CSS. That's why data URIs make sense for tiny icons but not for photos: besides being bigger, inlined images can't be cached by the browser as separate files.
Where you'll find Base64
- Data URIs:
data:image/png;base64,...embeds small files directly in HTML or CSS. - Email (MIME): attachments travel as Base64, wrapped at 76 characters per line.
- HTTP Basic authentication: the header
Authorization: Basic YW5hOnNlY3JldDEyMw==is justana:secret123encoded. - JWTs: the header and payload are JSON encoded as Base64URL. The header
{"alg":"HS256","typ":"JWT"}becomeseyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9, which is why almost every JWT starts witheyJ. - Keys and certificates: PEM files store binary data as Base64 between the
BEGINandENDlines.
Base64 in URLs and file names
The + and / characters have special meaning in URLs and file names. The Base64URL variant, also defined in RFC 4648, swaps + for - and / for _, and usually drops the = padding. Use it whenever the value ends up in a URL, a query parameter or a file name. A good decoder accepts both forms.
Base64 is not encryption
This is the most dangerous misconception. Base64 has no key or password, and anyone can reverse it in a second. Whoever intercepts Basic YW5hOnNlY3JldDEyMw== reads ana:secret123 straight away. That's why Basic auth is only acceptable over HTTPS, and why a JWT payload must never hold confidential data: it's signed, not encrypted.
If you need to hide content, use real encryption. If you need to check whether a file has been altered, you want a hash instead. See what SHA-256 is and how to verify files.
The JavaScript accent trap
The browser's btoa() function treats every character as a single byte (Latin-1). Feed it an emoji or a symbol like € and it throws an InvalidCharacterError. Accented letters are sneakier: btoa("café") works, but returns Y2Fm6Q== instead of Y2Fmw6k=, the correct Base64 for the UTF-8 text. The receiving system decodes it as UTF-8 and shows a broken character.
The fix is to turn the text into UTF-8 bytes with TextEncoder before encoding, and use TextDecoder on the way back. The Base64 converter already does this, so accents and emoji round-trip cleanly.
Encode and decode without installing anything
Paste your text into the converter, choose encode or decode, and the result appears as you type. It accepts Base64URL and unpadded input, ignores line breaks, can produce URL-safe output and tells you when the input is invalid. The converter works with text: if the Base64 you paste represents a binary file, such as an image or a PDF, it shows a warning instead of a result. All conversion happens in your browser.
Frequently asked questions
How can I tell if a string is Base64?
You can't be completely sure, but there are clues: only letters, digits, + and / (or - and _) appear, the length is usually a multiple of 4, and it may end with =. The real test is decoding it. Note that words like test are also valid Base64 in format, so the format alone proves nothing.
Why don't JWTs end with =?
Because the spec JWTs build on (RFC 7515) defines Base64URL without padding: trailing = characters are dropped, since dots separate each part and the length is known. To decode one by hand, add = back until the length is a multiple of 4. The converter on this site does that for you.
What's the difference between Base64, Base32 and hex?
All of them write bytes as text; they differ in alphabet size. Hex uses 16 symbols and doubles the size of the data. Base32 uses 32, grows data by about 60% and is case-insensitive, which is why it shows up in two-factor authentication secrets. Base64 is the most compact of the three, at about 33% extra.
Why do two tools give different Base64 for the same text?
Usually because of character encoding. Text with accents can be turned into bytes as UTF-8 or as Latin-1, and each choice produces different bytes and therefore different Base64. Hidden line breaks, trailing spaces and using the URL-safe variant also change the output, even when the visible text looks identical.