Base64 is a way of writing arbitrary binary data using only 64 safe, printable characters (A-Z, a-z, 0-9, + and /). You use it when binary bytes must travel through a channel built for text, such as JSON, email or a URL. It is an encoding, not encryption: anyone can reverse it instantly, so it hides nothing.

Why Base64 exists

Many older text-based systems, including email, were designed around printable ASCII. Raw bytes can contain values that these systems mangle, strip or treat as control characters. Base64 solves this by converting every byte sequence into a restricted alphabet that survives almost any text pipeline unchanged. The receiver decodes it back to the exact original bytes.

How the 6-bit grouping works

A byte has 8 bits. Base64 reads the input in blocks of 3 bytes (24 bits) and re-slices those 24 bits into four groups of 6 bits. Each 6-bit group is a number from 0 to 63, which indexes into the Base64 alphabet: 0-25 are A-Z, 26-51 are a-z, 52-61 are 0-9, 62 is + and 63 is /.

Worked example: "Man"

The ASCII bytes for M, a, n are 77, 97 and 110, or in binary:

M        a        n
01001101 01100001 01101110

Regroup the same 24 bits into 6-bit chunks:

010011 010110 000101 101110
  19     22      5     46
  T      W       F      u

The result is TWFu. Three input bytes became four output characters.

Padding with "="

Input rarely divides evenly into 3-byte blocks. When the last block has only one or two bytes, the encoder pads the missing bits with zeros and adds = characters so the output length stays a multiple of four.

Take hello (5 bytes). The first three bytes, "hel", become aGVs. The remaining two bytes, "lo", are 16 bits: 01101100 01101111. Split into 6-bit groups, that is 011011 000110 1111, and the last group is padded with two zero bits to 111100. The values are 27, 6 and 60, which map to b, G and 8. One byte short of a full block would give two = signs; two bytes short gives one. So hello encodes to aGVsbG8=.

You can check any result with the Base64 encoder and decoder. Avoid pasting secrets into any online tool, ours included, and use your operating system's built-in Base64 command for sensitive data.

The size overhead

Every 3 bytes of input produce 4 characters of output, so Base64 text is about one third larger than the original: 3,000 bytes become 4,000 characters. Padding and any line breaks add a little more. This is the price of using a restricted alphabet, and it matters whenever size does, for example in web page payloads or API responses.

Where Base64 is used

  • Email (MIME): attachments are Base64-encoded so binary files pass through mail systems designed for text.
  • JSON and XML: neither format has a native binary type, so small binary values such as signatures, keys or thumbnails are often stored as Base64 strings.
  • Data URIs: a file can be embedded directly in HTML or CSS, for instance data:image/png;base64,iVBORw0....
  • HTTP Basic authentication: the Authorization header carries username:password in Base64. This is formatting only, so it must always travel over HTTPS. See the guide to HTTP headers for context.
  • JSON Web Tokens: the header and payload of a JWT are Base64URL-encoded JSON, which is why anyone can read them. A JWT decoder does exactly this.

The URL-safe variant

Standard Base64 uses + and /, and the padding =. All three have special meaning in URLs and file names, and would need percent-encoding (see URL encoding explained). The URL-safe variant, defined in RFC 4648, replaces + with - and / with _, and often drops the padding. The bytes fb ff fe show the difference: standard Base64 gives +//+, while the URL-safe form gives -__-. Decoders must know which variant they are reading.

Encoding is not encryption

Encryption needs a secret key; without it the data should be unreadable. Base64 has no key and no secret: the algorithm is public and the mapping is fixed. A string like c2VjcmV0 looks scrambled, but anyone can paste it into a decoder and read secret. Never use Base64 to protect passwords, API keys or personal data. If you need confidentiality, use real encryption; if you need to store a password, use a dedicated password hashing function.

UTF-8 pitfalls

Base64 operates on bytes, not characters. Text must first be turned into bytes, and the character encoding you choose changes the result. In UTF-8, the word café is five bytes because é takes two (c3 a9), and it encodes to Y2Fmw6k=. Decode those bytes as Latin-1 and you get garbled text. In JavaScript, the browser functions btoa() and atob() only handle characters up to code point 255 and throw errors on other text, so non-Latin text should be converted to UTF-8 bytes (for example with TextEncoder) before encoding. Always agree on UTF-8 at both ends.

When not to use Base64 for images

Inlining an image as a data URI saves one HTTP request, which can help for a tiny icon of a few hundred bytes. For anything larger it usually hurts:

  • The file grows by about a third, before compression.
  • The image cannot be cached separately; it is re-downloaded with every copy of the HTML or CSS that contains it.
  • It sits inside the HTML or CSS, which can delay rendering of the page while the browser parses it.

With HTTP/2 and HTTP/3 the saved request matters far less. For photos and larger graphics, serve a properly compressed file instead; the image optimization guide covers formats and sizes. SVG icons can often be inlined as plain markup without Base64 at all.

Common mistakes

  • Treating Base64 as a security measure for secrets.
  • Mixing the standard and URL-safe alphabets, which produces decode errors or wrong bytes.
  • Stripping padding from a string and then feeding it to a decoder that requires it.
  • Encoding text without fixing the character encoding, leading to broken accented or non-Latin characters.
  • Embedding large images as data URIs and then wondering why pages got slower.

Frequently Asked Questions

Is Base64 encryption?

No. It is a reversible format change with no key. Anyone can decode it, so it offers no confidentiality.

Why does Base64 end with = signs?

They are padding. They fill the output to a multiple of four characters when the input length is not a multiple of three bytes.

How much bigger does Base64 make my data?

About 33 percent larger, since 3 bytes become 4 characters, plus a little for padding or line breaks.

Can I decode Base64 without knowing the original file type?

You can always recover the bytes, but the format is not recorded. A data URI or MIME header normally states the type; otherwise you must infer it from the content.