Skip to main content
riqo.ioTools for big ideas

Base64: binary data as text

How Base64 maps bytes to a restricted ASCII alphabet, why it expands data and which variant fits each protocol.

Why Base64 exists

Many protocols and formats historically handle text more reliably than arbitrary bytes. Base64 represents binary data with a restricted ASCII alphabet so that bytes can cross text-oriented channels without being mistaken for control data or delimiters.

It neither compresses nor conceals information. Base64 is a reversible representation for compatibility and transport, not a security mechanism.

24-bit blocks and a 64-symbol alphabet

Base64 normally reads three bytes, or 24 bits, and splits them into four 6-bit groups. Each group indexes one of 64 alphabet symbols, producing four text characters for every three input bytes.

That ratio creates roughly one-third size overhead before padding, line wrapping or protocol envelopes. Native binary transport is therefore usually preferable for large payloads.

Padding and incomplete final groups

When the final input contains fewer than three bytes, standard Base64 uses one or two = padding characters to keep output in four-character groups. Padding indicates how many meaningful bytes were present in the final quantum.

Some protocols omit padding when length can be inferred from context. Producers and consumers should agree on this choice, particularly when encoded text is signed or compared byte-for-byte.

Standard Base64 and Base64url

The standard alphabet contains + and /, which are inconvenient in URLs and filenames. Base64url replaces them with - and _, reducing the need for another escaping layer in web contexts.

Both variants represent the same bits but are not always interchangeable as strings. Document the alphabet and padding policy at system boundaries.

Text, UTF-8 and binary data

Encoding a string means first obtaining bytes from a character encoding, usually UTF-8. The same abstract text encoded with different character sets can therefore yield different Base64 output.

Images, archives and cryptographic material are already binary and should not be interpreted as text. After decoding, bytes must be handled according to their original format rather than automatically decoded as UTF-8.

What Base64 does not provide

Anyone who has the encoded value can decode it. Base64 provides no confidentiality, authenticity or integrity; those properties require suitable cryptographic mechanisms such as authenticated encryption or a MAC.

Avoid unnecessary layers of repeated encoding. State the upstream and downstream representation clearly and use Base64 only where a text representation of bytes solves a real transport constraint.