Base64 tool
Base64 Encode/Decode Tool
What is Base64 Encoding?
Base64 encoding is a method for converting binary data into a stream of printable ASCII characters. It is a form of data translation, not encryption, designed to allow binary data to be transmitted and stored over media that are designed to deal only with text.
1. The "Why": The Problem It Solves
Many systems, such as older email protocols (SMTP), were originally designed to handle plain text (ASCII). These systems can misinterpret binary data (like images, executables, or zip files) because certain byte values represent control characters (e.g., the end-of-file character).
Base64 solves this by representing the binary data using a set of 64 safe, text-only characters. This ensures the data remains intact and unchanged during transport.
Common modern use cases include:
- Data URLs: Embedding small images or fonts directly in HTML or CSS code.
<img src="data:image/png;base64,iVBORw0KGgoAAAAN..." /> - Email Attachments (MIME): The original and still fundamental use case.
- Storing Binary Data in Text Formats: Including binary data within JSON, XML, or configuration files.
2. How It Works: The Core Mechanism
The name "Base64" comes from the fact that it uses a 64-character alphabet:
- Uppercase letters A-Z (26 characters)
- Lowercase letters a-z (26 characters)
- Digits 0-9 (10 characters)
- The symbols
+and/(2 characters)
Step-by-Step Encoding Process (with the example "Man")
Let's encode the text "Man", which is 3 bytes long.
Step 1: Convert to Binary (ASCII)
M-> 77 ->01001101a-> 97 ->01100001n-> 110 ->01101110
Step 2: Concatenate the Binary Stream
010011010110000101101110
Step 3: Split into 6-bit Chunks
010011010110000101101110
Step 4: Convert each 6-bit chunk to its Decimal Value
010011-> 19010110-> 22000101-> 5101110-> 46
Step 5: Map to the Base64 Alphabet
- 19 -> T
- 22 -> W
- 5 -> F
- 46 -> u
The string "Man" is encoded as "TWFu".
Padding with =
The example above worked perfectly because the input was exactly 3 bytes (24 bits), which divides evenly by 6. If the input isn't a multiple of 3 bytes, we use the equal sign = for padding.
-
1 Byte Leftover (e.g., "A"):
- The encoding process results in 2 characters.
- Two
=signs are added as padding, making it"QQ==".
-
2 Bytes Leftover (e.g., "BC"):
- The encoding process results in 3 characters.
- One
=sign is added as padding, making it"QkM=".
3. Key Characteristics
| Aspect | Description |
|---|---|
| Purpose | To represent binary data in a plain text format for safe transport. |
| Core Value | Compatibility and data integrity across text-based systems. |
| Is it secure? | No. It is not encryption. There is no key, and it can be decoded by anyone. It offers only obscurity. |
| Data Size | Increases the data size by approximately 33% (because 3 bytes become 4 characters). |
| Common Uses | Email attachments (MIME), Data URLs, storing complex data in JSON/XML. |