Base64 Encode/Decode
Quickly encode files to base64 or decode base64 strings. Image preview supported.
Encode
Decode
How to Use Base64 Encoder/Decoder?
To Encode:
- Select a file using the File input under the Encode section.
- Click Encode to convert your file to Base64.
- The Base64 string will appear in the text area.
- Click Copy to copy the Base64 string to your clipboard.
To Decode:
- Paste your Base64 string into the text area under the Decode section.
- Optionally enter a filename for the decoded file.
- Click Decode & Download to convert and download the file.
How to Use This Base64 Tool
To Encode Files or Media:
- Select or drop any file (PNG, JPG, SVG, PDF, Font, audio, etc.) in the File input.
- Click Encode to instantly process the binary data into an ASCII Base64 string.
- View the live image preview (for images) or click Copy to copy the raw string or Data URI to your clipboard.
To Decode Base64 Strings:
- Paste your raw Base64 string or Data URI (e.g.
data:image/png;base64,iVBORw...) into the text area. - Optionally specify an output filename and extension (e.g.
logo.png). - Click Decode & Download to generate and save the reconstructed binary file to your disk.
The Engineering Guide to Base64 Encoding
Base64 is a binary-to-text encoding scheme defined in RFC 4648. It represents binary data using an alphabet of 64 printable US-ASCII characters: uppercase letters (A-Z), lowercase letters (a-z), numerals (0-9), and two special symbols (+ and /, with = reserved for padding).
How the Algorithm Works Under the Hood
Every byte of digital data consists of 8 bits. However, traditional text protocols (such as legacy SMTP email or early web protocols) were designed for 7-bit ASCII transmission and would corrupt non-printable control characters or raw high-order bytes. Base64 bridges this gap by regrouping bits:
- The algorithm reads chunks of 3 binary bytes (24 bits) at a time.
- It slices those 24 bits into 4 groups of 6 bits each (since 26 = 64).
- Each 6-bit value (0 to 63) maps directly to one character in the standard 64-character lookup index table.
- Padding (
=): If the input data is not an exact multiple of 3 bytes, padding is appended. A 1-byte remainder produces 2 characters plus two padding symbols (==); a 2-byte remainder produces 3 characters plus one padding symbol (=). - 33% Payload Overhead: Because 3 bytes (24 bits) of raw binary are transformed into 4 ASCII characters (32 bits), Base64 encoding always increases raw payload size by 33.3% (or ~37% when considering line wraps or header formatting).
Standard Base64 vs. URL-Safe Base64
In standard Base64, the characters + and / are used for index values 62 and 63. However, both characters have reserved meanings in URL query strings, path segments, and HTTP headers:
- Standard Base64: Uses
+and/, with=padding. Used in MIME email, basic HTTP authentication, and Data URIs. - URL-Safe Base64 (Base64url): Replaces
+with-(hyphen) and/with_(underscore), and frequently drops the trailing=padding. This variant is mandatory in JSON Web Tokens (JWTs), OAuth tokens, and URL slugs.
When Should You Use Data URIs vs. CDN File Hosting?
| Approach | Ideal Scenarios | Pros | Trade-offs |
|---|---|---|---|
| Inline Base64 Data URI | Tiny UI icons (< 2KB), critical above-the-fold SVGs, standalone HTML reports, offline email newsletters. | Zero HTTP handshake latency; self-contained single-file portability. | 33% larger download size; cannot be cached independently by the browser cache. |
| External CDN Asset | Photographs, hero banners, multi-page web assets, fonts, media libraries (> 5KB). | Independent browser & edge caching; parallel HTTP/2 multiplexing; smaller raw file sizes. | Requires initial DNS lookup, SSL handshake, and separate HTTP request. |
Base64 Code Examples in Popular Languages
If you are automating workflows or implementing encoding in your applications, here are copy-pasteable snippets:
JavaScript / Node.js
// In Node.js: Encode binary file or string to Base64
const fs = require('fs');
const fileBuffer = fs.readFileSync('logo.png');
const base64String = fileBuffer.toString('base64');
console.log(`data:image/png;base64,${base64String}`);
// In Browser JavaScript: Decode Base64 string back to binary
const binaryData = atob(base64String);
// In Browser JavaScript: Convert File object to Base64 Data URI
function fileToDataUri(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(file);
});
}
Python 3
import base64
# Encode a file to Base64
with open("graphic.png", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
print(f"data:image/png;base64,{encoded_string[:30]}...")
# Decode a Base64 string back to a binary file
decoded_bytes = base64.b64decode(encoded_string)
with open("reconstructed.png", "wb") as output_file:
output_file.write(decoded_bytes)
Bash & Command Line (Linux / macOS)
# Encode a file to Base64 (macOS)
base64 -i input.png -o output.txt
# Encode a file to Base64 (Linux / GNU coreutils, single line without wrapping)
base64 -w 0 input.png > output.txt
# Decode Base64 text back to a binary file
base64 -d output.txt > restored.png
Frequently Asked Questions
What is Base64 encoding and why is it needed?
Base64 is a binary-to-text algorithm that converts raw byte streams (such as image files, cryptographic signatures, or binary blobs) into 64 safe ASCII characters. It prevents data corruption across systems and communication protocols that were designed exclusively for plain text, such as JSON APIs, HTML attributes, XML, and SMTP emails.
Does Base64 provide encryption or security?
No. Base64 is strictly an encoding method, not encryption. Anyone who receives a Base64 string can instantly decode it back to the original content without a password or key. Never use Base64 alone to protect sensitive data like passwords or tokens; always apply cryptographic encryption (such as AES-256) first if confidentiality is required.
Why does Base64 increase file size by 33%?
Binary files pack 8 bits of data per byte. Base64 encoding takes 6 bits of data and stores it in an 8-bit ASCII character. Because only 6 out of 8 bits carry data payload, it takes 4 bytes to represent what was originally 3 bytes of binary data. This mathematical ratio (4/3) results in an inherent 33.3% size expansion.
Can I embed images directly into HTML or CSS using Base64?
Yes. By prepending the MIME type header (for example, data:image/png;base64,), you can insert the string directly into an <img src="..."> tag or CSS background-image: url(...). This is optimal for micro-icons and critical above-the-fold graphics because it eliminates an additional network round-trip.
What is the difference between Base64 and Base64URL?
Standard Base64 contains the characters + and /, which have syntactic meaning in URLs (e.g., query separators and path dividers). Base64URL replaces + with - (dash) and / with _ (underscore), and omits trailing = padding characters so the string can be safely embedded in URL parameters, slugs, and JWT authentication tokens without percent-encoding.
Are my files uploaded or stored on Minimo Digital servers?
For client-side file conversions, your data is processed directly inside your web browser using modern JavaScript FileReader and Web API algorithms. Your files never leave your computer or device, ensuring total privacy and security.