Base64 Encoding, Explained

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.

What is Base64?

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.

Why Base64 exists

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.

How Base64 works

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.

Padding

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.

Size overhead

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 Base64 by hand

Decoding runs the same steps backwards. Take SGk=:

  1. Look up each character's value: S is 18, G is 6 and k is 36. The = is padding and has no value.
  2. Write each value as 6 bits: 010010 000110 100100.
  3. Join the bits and cut them into 8-bit bytes: 01001000 01101001, with two bits (00) left over. The leftover bits are padding and are dropped.
  4. The bytes are 72 and 105, which are 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.

URL-safe Base64

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 _
  • trailing = padding is often dropped

The 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.

Common use cases

  • Data URIs — embed a small image directly in CSS or HTML: url(data:image/png;base64,iVBORw0...), saving an HTTP request for tiny assets.
  • HTTP Basic Auth — Authorization: Basic <base64("username:password")>.
  • Email (MIME) — encode attachments so they survive text-only mail transport.
  • JWTs — the header and payload are Base64url-encoded.
  • Embedding binary in JSON/XML — e.g. a small file blob inside an API response.

Base64 is not encryption

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.

A note on Unicode

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.

Base64 on the command line and in code

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.

Why a Base64 string won't decode

Most decoding failures come from a few causes:

  • Characters outside the alphabet. Remove a data URI prefix such as 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.
  • A length that can't be right. Base64 comes in groups of four characters. If the length leaves a remainder of 1 when divided by 4, the string was cut off or damaged, because one character holds only 6 bits, not enough for a byte.
  • The bytes aren't text. An image, PDF or zip file decoded as text shows garbage and � replacement characters. Decode images with an image decoder instead.
  • Line breaks and missing padding. These are not real problems. Email wraps Base64 at 76 characters and URL-safe Base64 drops the =, so a good decoder ignores whitespace and puts the padding back.

Encode and decode Base64 online

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.

Frequently asked questions

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.

Keep going

Base64 Encode / Decode

More guides

  • ZIP Files, Explained — What a ZIP file is, how ZIP compression works, why photos and videos barely shrink, how to unzip safely on any device, and how ZIP compares with 7z and RAR.
  • UPI QR Codes, Explained — How a UPI QR code works: the upi://pay link and its pa, pn, am, cu and tn fields, static vs fixed-amount codes, checking the payee name, and print sizes.
  • URL Encoding (Percent-Encoding), Explained — How URL percent-encoding works: reserved and unreserved characters, %20 vs + for spaces, encodeURIComponent vs encodeURI, UTF-8 bytes and double-encoding bugs.
  • QR Codes: Error Correction, Size and Logos, Explained — How QR error correction works, how large a logo can be, what size to print, which colors scan reliably, and when to use PNG or SVG so your code always scans.
  • Linux File Permissions and chmod, Explained — What chmod 755, 644 and 777 mean: owner, group and others, read, write and execute, octal and symbolic modes, umask, setuid, setgid and the sticky bit.
  • DMS and Decimal Degrees, Explained — How to convert DMS to decimal degrees and back, how DD, DMS and DDM differ, how many decimal places you need, and which formats Google Maps and GPS accept.

All Encode / Decode tools · All guides