Skip to content
useToolz online tools
Base64 Encoding: What It Is, Why You Need It, and How to Use It
Development

Base64 Encoding: What It Is, Why You Need It, and How to Use It

Александр Михеев

Александр Михеев

19 July 2024 · 4 min read

What Is Base64

Base64 is a method of encoding binary data into a text format using 64 printable ASCII characters. The Base64 alphabet includes uppercase and lowercase Latin letters (A-Z, a-z), digits (0-9), and two additional characters: + and /. The = character is used for padding.

The main purpose of Base64 is to safely transmit binary data through channels designed for text only. This is not encryption — Base64 provides no data protection whatsoever. Anyone can decode a Base64 string back to the original data.

How Base64 Encoding Works

The encoding algorithm is straightforward:

  • The input data is split into groups of 3 bytes (24 bits).
  • Each 24-bit group is divided into 4 blocks of 6 bits.
  • Each 6-bit block is converted into one character from the Base64 alphabet.
  • If the input data is not a multiple of 3 bytes, padding (= characters) is added.

Thus, 3 bytes of input data become 4 Base64 characters. This means the data size increases by approximately 33%. For example, a 1 MB file will be about 1.33 MB after encoding.

Where Base64 Is Used

APIs and Authentication Tokens

HTTP Basic Authentication encodes the login and password in login:password format using Base64 and sends it in the Authorization header. JWT tokens also contain Base64URL-encoded segments. Important to remember: Base64 is encoding, not encryption. Tokens should only be transmitted over HTTPS.

Email and MIME

The SMTP email protocol originally supports only 7-bit ASCII text. To send attachments (images, documents), they are encoded in Base64 and embedded in the email body in MIME format. In fact, Base64 was originally developed for email.

Data URI in HTML and CSS

Base64 allows you to embed small files directly in HTML or CSS, avoiding additional HTTP requests:

<img src="data:image/png;base64,iVBORw0KGgo..." />

This is useful for small icons and decorative elements. However, for large images, Data URI is inefficient: the 33% size increase and the inability to leverage browser caching outweigh the savings from fewer HTTP requests.

Storing Data in Text Formats

When you need to store binary data in JSON, XML, or YAML — formats that only support text — Base64 is the standard solution. For example, Kubernetes Secrets configurations store values in Base64.

Encoding and Decoding in Practice

Most programming languages have built-in functions for working with Base64:

  • JavaScript: btoa() for encoding and atob() for decoding (works with ASCII only). For Unicode, use TextEncoder.
  • Python: the base64 module — base64.b64encode() and base64.b64decode().
  • PHP: the functions base64_encode() and base64_decode().
  • Command line: echo "text" | base64 and echo "dGV4dA==" | base64 -d.

For quick encoding and decoding without writing code, use our online Base64 tool. A separate Base64 decoder is also available for decoding strings.

The Base64URL Variant

Standard Base64 uses the + and / characters, which have special meaning in URLs. For safe data transmission in URLs, the Base64URL variant exists, where + is replaced with - and / with _. Padding (=) is usually omitted. This variant is used in JWT tokens and other web standards.

Limitations and Common Mistakes

  • 33% size increase. Don't encode large files in Base64 unnecessarily. For file transfers, use multipart/form-data.
  • Base64 is not encryption. Never use Base64 to "protect" passwords or secrets. Use specialized hashing algorithms for that.
  • Unicode issues. The btoa() function in JavaScript doesn't support characters beyond Latin-1. For Cyrillic and other Unicode characters, first convert the string to UTF-8 bytes.
  • Line breaks. Some implementations insert line breaks every 76 characters (MIME standard). When transmitting via API, these line breaks can cause parsing errors.

Frequently Asked Questions

Is It Safe to Transmit Data in Base64?

Base64 does not provide security. It is simply encoding, not encryption. Anyone can decode a Base64 string. For data protection, use encryption (AES, RSA) and transmit over HTTPS.

How Does Base64 Differ from Base32 and Base16?

All three formats encode binary data into text but use different alphabets. Base16 (hex) uses 16 characters and doubles the size. Base32 uses 32 characters and increases the size by 60%. Base64 is the most compact option with a 33% increase.

Should You Encode Images in Base64?

For small icons (up to 2-5 KB) — yes, it reduces the number of HTTP requests. For large images — no: the size increase and inability to cache make this approach inefficient. To convert images to Base64, use our image to Base64 converter.

How to Encode Text to Base64 Online?

Open our Base64 tool, paste your text or data, and click the encode button. The result can be copied to your clipboard with a single click. All operations run in your browser — your data never leaves your device.

Понравилась статья?

Оцените — это помогает нам делать контент лучше

Change rating

Your rating:

Thanks for your rating!

Comments

Log in to leave a comment

No comments yet. Be the first!

We use cookies for site operation and analytics. Подробнее

Upscaled image
Download

Log in to continue

or