·7 min read

Base64 Encoding Explained: When and Why to Use It

Base64 encoding is one of those things every developer encounters but few deeply understand. Let us change that.

What is Base64?

Base64 is a binary-to-text encoding scheme that represents binary data using a set of 64 ASCII characters. It takes binary data (like images, files, or arbitrary byte sequences) and converts it into a string of printable characters that can safely travel through systems designed for text — like email, URLs, and JSON.

The 64 characters used are A-Z, a-z, 0-9, plus (+), and forward slash (/), with equals (=) used for padding. This character set was chosen because these characters are reliably transmitted across all text-based protocols without being modified or corrupted.

How Base64 Encoding Works

The encoding process takes groups of 3 bytes (24 bits) of binary data and splits them into 4 groups of 6 bits each. Each 6-bit group maps to one of the 64 characters in the Base64 alphabet. Since 6 bits can represent values from 0 to 63, this gives us exactly 64 possible characters — hence the name.

If the input data is not evenly divisible by 3 bytes, padding is added. One remaining byte produces two Base64 characters plus two equals signs (==). Two remaining bytes produce three Base64 characters plus one equals sign (=). This is why you often see Base64 strings ending with one or two equals signs.

The trade-off is size: Base64 encoding increases the data size by approximately 33%. Three bytes of input become four characters of output. This overhead is acceptable for most use cases, but you should be aware of it when encoding large files.

Common Use Cases for Base64

HTTP Basic Authentication

One of the most common uses of Base64 is in HTTP Basic Authentication. When a client authenticates with a username and password, it combines them with a colon separator (username:password) and Base64-encodes the result. This encoded string is sent in the Authorization header. Note that this is encoding, not encryption — anyone who intercepts the header can decode it, which is why Basic Auth should only be used over HTTPS.

Data URIs

Data URIs allow you to embed files directly in HTML, CSS, or JavaScript using Base64 encoding. Instead of referencing an external image file, you can embed the image data directly in your code. This eliminates an HTTP request but increases the file size. Data URIs are commonly used for small icons, background images, and fonts in CSS.

Email Attachments (MIME)

Email was originally designed for plain text only. When you attach a file to an email, the MIME standard uses Base64 encoding to convert the binary file data into text that can be safely transmitted through email servers. The receiving email client then decodes the Base64 back to the original file.

API Payloads

When APIs need to transmit binary data within JSON payloads, Base64 encoding is the standard approach. Since JSON only supports text, binary data like images, certificates, or encrypted blobs must be encoded as Base64 strings. Many cloud APIs use this pattern for file uploads and downloads.

Base64 is Not Encryption

This is the most important thing to understand about Base64: it provides zero security. Base64 is an encoding scheme, not an encryption algorithm. Anyone can decode a Base64 string — there is no key, no password, no secret. Never use Base64 to hide sensitive information. If you need to protect data, use proper encryption algorithms like AES or RSA.

Base64 Variants

Standard Base64 uses + and / characters, which can cause problems in URLs (where + means space and / is a path separator). Base64URL encoding replaces + with - and / with _, making the output safe for URLs and filenames. This variant is used in JWTs, OAuth tokens, and anywhere Base64 data appears in URLs.

Working with Base64 in JavaScript

JavaScript provides built-in functions for Base64 encoding and decoding. The btoa() function encodes a string to Base64, and atob() decodes it. However, these functions only work with ASCII characters. For UTF-8 text (including emojis and international characters), you need to use TextEncoder and TextDecoder or a similar approach to handle the encoding correctly.

Encode and Decode Base64

Convert text to Base64 or decode Base64 strings instantly with proper UTF-8 support.

Open Base64 Tool →