Base64 explained: how it turns binary into text, the alphabet and padding, the URL-safe variant, size overhead, use cases, and why it isn't encryption.
By Udit Agarwal · Updated October 18, 2026
Base64 is a way of writing any binary data with 64 safe text characters: A–Z, a–z, 0–9, + and /. Every 3 bytes of input become 4 characters, so Man encodes to TWFu and the result is about a third larger than the original. It shows up in data URIs, email attachments, JWTs and auth headers, anywhere binary data needs to travel through a text-only channel. It's simple once you see the mechanics, and understanding it clears up a common and dangerous misconception: Base64 is not a form of security.
Base64 is a binary-to-text encoding scheme. It represents arbitrary binary data using only 64 printable ASCII characters, so the result is safe to place in text-based formats and protocols. It's defined in RFC 4648.
The standard alphabet is:
A–Z, a–z (52 characters)0–9 (10 characters)+ and / (2 characters)That's 64 symbols, hence the name, plus =, which is used only for padding. Each character stands for a number: A–Z are 0–25, a–z are 26–51, 0–9 are 52–61, + is 62 and / is 63.
Plenty of systems were designed to carry text, not raw bytes: email bodies, URLs, JSON string fields, XML, HTTP headers. Sending raw binary through them can corrupt the data — a stray byte might be interpreted as a control character, a line break, or a delimiter. Base64 sidesteps the problem by re-expressing binary as safe text on the way in, and decoding it back on the way out.
It's worth being precise with terminology: Base64 is an encoding, not a cipher, not compression, and not a hash. It's fully reversible and adds size.
Base64 processes input 3 bytes at a time. Three bytes are 24 bits; those 24 bits are split into four 6-bit groups, and each group (a value from 0–63) maps to one character in the alphabet.
So 3 bytes of input become 4 characters of output. Walking through the text Man:
| Char | ASCII | 8-bit binary |
|---|---|---|
M |
77 | 01001101 |
a |
97 | 01100001 |
n |
110 | 01101110 |
Concatenated, that's 010011010110000101101110. Regrouped into 6-bit chunks: 010011 010110 000101 101110 → values 19 22 5 46 → T W F u. So Man encodes to TWFu.
When the input length isn't a multiple of 3, the output is padded with = so it stays a multiple of 4:
| Input | Bytes | Base64 |
|---|---|---|
Man |
3 | TWFu |
Ma |
2 | TWE= |
M |
1 | TQ== |
That trailing = carries no data — it's only there so the length is a clean multiple of four.
Because every 3 bytes become 4 characters, Base64 output is about 33% larger than the original. That's the trade-off for text safety, so compress or minify elsewhere when size matters, and don't Base64-encode large assets you could link to directly.
To work out the exact length, padded Base64 for n bytes is 4 × ⌈n ÷ 3⌉ characters. A 100 KB image (102,400 bytes) becomes 136,536 characters, about 133 KB. In the other direction, a padded string of L characters decodes to 3 × L ÷ 4 bytes, minus one byte for each = at the end. Email adds a line break every 76 characters on top of that, so a MIME attachment grows by roughly 37%, not 33%.
Decoding runs the same steps backwards. Take SGk=:
S is 18, G is 6 and k is 36. The = is padding and has no value.010010 000110 100100.01001000 01101001, with two bits (00) left over. The leftover bits are padding and are dropped.H and i in ASCII.So SGk= decodes to Hi. One = at the end means the last group of four characters carried 2 bytes, and == means it carried 1.
The standard + and / characters have special meaning in URLs and filenames. The URL-safe variant (RFC 4648 §5, used by JWTs among others) swaps them:
+ becomes -/ becomes _= padding is often droppedThe encoded data is identical. Only those two characters change, so the string can sit in a URL, filename or cookie untouched. The two bytes 251 and 255, for example, encode to +/8= in standard Base64 and to -_8 in URL-safe Base64 with the padding dropped.
url(data:image/png;base64,iVBORw0...), saving an HTTP request for tiny assets.Authorization: Basic <base64("username:password")>.This is the most important point: Base64 is fully reversible by anyone, with no key. It hides nothing and provides zero confidentiality.
Use Base64 to transport data, never to protect it. If you see a password or token "hidden" in Base64, treat it as plaintext. For confidentiality, encrypt the data first, then Base64-encode the ciphertext if you need it in text form.
Base64 encodes bytes, not characters. To encode text that contains non-ASCII characters (emoji, accents, non-Latin scripts), first convert it to bytes with UTF-8, then Base64-encode those bytes. Skipping the UTF-8 step is a classic source of "it works for English but breaks for everything else" bugs.
macOS and Linux ship a base64 command:
echo -n 'Hi' | base64
echo 'SGk=' | base64 --decode
The first line prints SGk= and the second prints Hi. Watch the -n: plain echo adds a newline, so echo 'Hi' | base64 prints SGkK, because the newline byte was encoded too. That one hidden character is behind many "my Base64 doesn't match" reports, and it ends up inside passwords and API keys that were encoded this way.
In JavaScript, browsers have btoa() and atob(), but they work on Latin-1 characters, not UTF-8. btoa('é') returns 6Q==, while the UTF-8 bytes of é encode to w6k=, and btoa('✓') throws an InvalidCharacterError. To encode text safely, turn it into UTF-8 bytes with TextEncoder first. In Node.js, Buffer.from(text).toString('base64') uses UTF-8 by default, and Python's base64.b64encode() and base64.urlsafe_b64encode() take bytes, so you choose the text encoding yourself.
Most decoding failures come from a few causes:
data:image/png;base64, before decoding. Base64 that has been URL-encoded shows %2B, %2F or %3D where +, / and = should be, so run it through the URL Encoder / Decoder first.=, so a good decoder ignores whitespace and puts the padding back.The Base64 Encoder / Decoder converts text to Base64 and back as you type. Encoding turns your text into UTF-8 bytes first, so emoji, accents and non-Latin scripts round-trip correctly, and the URL-safe switch swaps + and / for - and _ and drops the padding. Decoding accepts both alphabets, ignores spaces and line breaks, and restores missing = padding, so a token copied from a URL or a wrapped email attachment decodes without cleanup. The tool works on text. For pictures, the Base64 Image Encoder / Decoder builds data URIs and turns them back into image files. Everything runs in your browser, so the data you paste stays on your device.
Is Base64 secure or encrypted? No. It's a reversible encoding with no key. Anyone can decode it instantly.
Why does Base64 end with = or ==? That's padding, added when the input length isn't a multiple of 3 so the output length is a multiple of 4.
Why is my Base64 string longer than the original? Encoding inflates size by roughly 33% — that's inherent to the 3-bytes-to-4-characters mapping.
What's the difference between Base64 and Base64url? Base64url replaces +// with -/_ (and usually drops padding) so the string is safe in URLs and filenames.
Why does my encoded emoji or accented text break? Encode the text as UTF-8 bytes first; Base64 operates on bytes, not characters.
How do I decode Base64 on the command line? On macOS or Linux, run echo 'SGk=' | base64 --decode. When encoding, use echo -n so the trailing newline isn't encoded along with your text.
Can I Base64-encode an image or a file? Yes. Base64 can encode any bytes. Images are usually wrapped in a data URI such as data:image/png;base64,… so a browser can display them straight from HTML or CSS.
Authorization: Basic headers.All Encode / Decode tools · All guides