Base64 encoding: how binary data becomes plain text for the web

Base64 translates binary files like images, audio, and encrypted keys into 64 safe ASCII characters for email, JSON, and web embedding.

Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format by translating it into a radix-64 representation. It uses 64 printable characters: A-Z (26), a-z (26), 0-9 (10), and two symbols (+ and /, or - and _ in URL-safe variants).

Base64 is not encryption and provides zero security. It is purely an encoding mechanism designed to safely transport binary data through systems that are designed to handle text, such as email protocols (MIME), JSON APIs, or HTML data URIs.

How the Base64 conversion process works

The algorithm takes 3 bytes of binary data (24 bits) and splits them into 4 groups of 6 bits each. Each 6-bit group represents a number from 0 to 63, which maps directly to a character in the Base64 index table. For example, 6 bits of all ones (111111) equals 63, which maps to '/'.

Because 3 bytes become 4 characters, Base64 encoding increases data size by exactly 33.3% (plus padding). If the input byte length is not divisible by 3, '=' padding characters are added to the end of the encoded string so the output length is always a multiple of 4.

Common web development use cases

Inline Images (Data URIs): HTML and CSS allow embedding small images directly into stylesheets or documents using `data:image/png;base64,...`. This eliminates an extra HTTP request, though it increases the file size by 33%. It is ideal for tiny icons or critical SVGs.

APIs and Cryptography: Web APIs frequently transmit binary payloads like PDF documents, cryptographic keys, or JWT (JSON Web Token) signatures inside JSON objects as Base64 strings. URL-safe Base64 replaces '+' with '-' and '/' with '_' to prevent URL parameter parsing errors.