Developer Tools

Base64 Encoding Explained (And When Not to Use It)

How base64 encoding really works: the 6-bit alphabet, why the padding sign is always there, and the five cases where base64 is the wrong tool — with exact math.

4 min read·Updated 2026-04-23

If a string of text ends in =, odds are you are looking at base64. It is the encoding that lets you push binary data — images, audio, encryption keys — through channels that only understand printable text. E-mail attachments, data URLs in CSS and the body of many JSON APIs all lean on it. This guide walks through how the algorithm actually works, what the padding sign is for, and the cases where base64 is the wrong tool entirely.

What base64 is (and is not)

Base64 takes three bytes of input and turns them into four characters drawn from a fixed 64-character alphabet: A-Z, a-z, 0-9, plus + and /. It is an encoding, not encryption and not hashing. Encoding is fully reversible by design: whoever holds the encoded string can recover the original bytes. That simple fact explains most misuse of base64 in the wild.

How the algorithm works

Three 8-bit bytes are 24 bits in total. Split those bits into four 6-bit groups. Each 6-bit group is a number from 0 to 63 that maps to exactly one character. Following the word cat through the process:

"cat" — bytes: 0x63 0x61 0x74
binary: 01100011 01100001 01110100
6-bit groups: 011000 110110 000101 110100
decimal: 24 54 5 52
chars: Y 2 F 0

So base64("cat") is Y2F0. 24 maps to the 24th letter of the alphabet (Y), 54 wraps into the digits (0-9 start at 52, so 54 is 2), and the rest behaves the same way. Run any short string through the Base64 Encoder/Decoder and you can verify the mapping yourself.

Why the padding sign is always =

When the input is not a multiple of three bytes, the last group has only one or two bytes. The encoder pads the leftover bits with zeros and appends one or two = characters so the output stays a multiple of four. Two bytes of input produce one padding sign (GRpads to R1I=), one byte produces two (R to Rw==). The = characters decode as zero bits and mark where real data ends — they are never part of the content itself.

Base64 vs hashing vs encryption

The three are confused constantly, and the difference is the whole story:

OperationReversible?Keys?Typical job
Base64 encodingYes, instantlyNoneCarry binary in text formats
Hashing (SHA-256)No, by designNoneFingerprints, password storage
Encryption (AES)Yes, with the keySecret keyConfidentiality

The same input run through all three produces completely different outputs, and only base64 reveals its input to anyone who decodes it.

What base64 is genuinely good for

  • E-mail attachments, because SMTP only transports 7-bit ASCII.
  • Data URLs such as data:image/png;base64,... for tiny assets that should not create an extra HTTP request.
  • Binary frames nested inside JSON or XML payloads.
  • HTTP Basic Authorization, where username:password is carried base64-encoded.
  • Storing public keys in text-based formats like JWK exports.

Five times base64 is the wrong answer

  • Passwords. Base64 is reversible in seconds. Store salted, slow hashes — that is what the Hash Generator is for — never base64.
  • Any confidentiality need. Without a key, base64 protects nothing. Rely on real encryption.
  • Compression. Base64 expands data by about 33%. For large payloads, gzip first; encoding already-compressed data just adds the overhead.
  • Large images inline. A base64 data URL inflates page weight by a third and bloats the HTML. Serve the file normally instead.
  • Hiding content from scanners. The PNG or zip file signatures survive encoding, so detection is trivial.

Turn an image into a data URL

The Image to Base64 converter produces the exact data:image/...;base64, string a browser will accept in an img or CSS background — handy for logos in newsletters, where external requests get blocked. Use it with judgement: paste the result into the Base64 Encoder/Decoder only if you genuinely need to inspect the byte content. The Hash Generator remains the tool to reach for when the goal is verification, not transmission.

Frequently asked questions

Does base64 compress data or make it smaller? The opposite. Three bytes become four characters, a permanent 33% overhead, plus whatever padding the tail needs. Only use it when text-transport actually requires it.

Why do decoded strings sometimes contain garbage characters? You decoded bytes that are not valid UTF-8 text — an image, a zipped archive or a signature. Decode to bytes, not to a string, when the input is known to be binary.

Can I rely on "encode once, decode anywhere"? Mostly, with two caveats: some old tools insert line breaks every 76 characters that other decoders choke on, and base64url variants swap + and / for - and _. The core alphabet is otherwise universal.

Tools used in this guide

Related guides