What is Base64 encoding?
Base64 is a binary-to-text encoding scheme that represents binary data using a set of 64 printable ASCII characters: A-Z, a-z, 0-9, +, and /, with = used for padding. It was originally designed to safely transmit binary data over channels that only support text, such as email (MIME) and early HTTP.
The encoding works by taking every three bytes (24 bits) of input and splitting them into four 6-bit groups. Each 6-bit value maps to one character in the Base64 alphabet. If the input length isn't a multiple of 3, the output is padded with one or two = characters. The result is always a text string that can be safely embedded in JSON, XML, HTML, URLs, and other text-based formats without corruption.
Base64 is defined in RFC 4648, which also specifies the URL-safe variant (using - and _ instead of + and /). Despite being universally supported, Base64 is not a compression or security mechanism — it increases data size by approximately 33% and provides no confidentiality.
Common uses for Base64
Embedding images in CSS/HTML — small images (icons, logos) can be inlined as data:image/png;base64,... URIs, eliminating an HTTP request at the cost of a larger document size.
Encoding binary data in JSON/XML — since JSON has no binary type, binary payloads (images, files, certificates) are typically Base64-encoded before embedding in a JSON field.
Email attachments — MIME encoding uses Base64 to attach binary files to email messages, which are fundamentally text-based protocols.
HTTP Basic Authentication — credentials are sent as Authorization: Basic base64(username:password). This is encoding, not encryption — always use HTTPS alongside it.
JWTs and cryptographic tokens — JSON Web Tokens encode their header and payload as URL-safe Base64 strings, making them safe to transmit in HTTP headers and URLs.