How to Encode and Decode Base64 (Text and Files)

3 min read
Beginner Base64 Encoding Developer Free Tool

You are working with an API that requires Base64-encoded data. Or you received a string that looks like SGVsbG8gV29ybGQ= and need to decode it. Or you need to embed an image directly in HTML or CSS. Base64 encoding is the answer.

This guide explains what Base64 is, why it exists, and how to encode and decode it.

Encode/Decode Base64 Now

Use our free Base64 Encoder & Decoder:

  1. Paste text or upload a file
  2. Click Encode to convert to Base64
  3. Click Decode to convert back to original
  4. Copy the result

Works entirely in your browser — no data sent to any server.

What is Base64?

Base64 is a way to represent binary data using only text characters. It converts any data into a string made of letters (A-Z, a-z), numbers (0-9), plus (+), slash (/), and equals (=) for padding.

Example:

Text:   Hello World
Base64: SGVsbG8gV29ybGQ=

Why Does Base64 Exist?

Many systems were designed to handle text, not binary data:

  • Email (SMTP) was designed for ASCII text — cannot send raw binary attachments
  • JSON can only contain text — cannot embed binary data directly
  • HTML/CSS can embed images as text using Base64 data URIs
  • URLs have restricted characters — Base64 safely encodes data for URLs
  • XML cannot contain certain byte values

Base64 solves this by converting binary data into safe text characters that work everywhere.

The Trade-Off

Base64 makes data about 33% larger. A 3 KB image becomes a ~4 KB Base64 string. This is why you should not Base64-encode large files unnecessarily.

Common Uses

Embedding Images in HTML/CSS

<img src="data:image/png;base64,iVBORw0KGgo..." />
.icon {
  background-image: url(data:image/svg+xml;base64,PHN2Zy...);
}

This eliminates an extra HTTP request — the image loads with the HTML. Good for small icons (under 5 KB), bad for large images.

API Authentication

Many APIs use Base64 for Basic Authentication:

Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

That Base64 string decodes to username:password.

Email Attachments

When you attach a file to an email, your email client Base64-encodes it before sending. The recipient's client decodes it automatically.

Data URIs

Embed any file directly in a URL:

data:text/plain;base64,SGVsbG8gV29ybGQ=

JSON Payloads

When an API needs to send binary data in JSON:

{
  "filename": "report.pdf",
  "content": "JVBERi0xLjQK..."
}

JWT Tokens

JSON Web Tokens use Base64URL encoding (a URL-safe variant) for the header and payload. Decode them with our JWT Decoder.

Encode/Decode in Code

JavaScript

// Encode
btoa("Hello World")  // "SGVsbG8gV29ybGQ="

// Decode
atob("SGVsbG8gV29ybGQ=")  // "Hello World"

// For Unicode text
function b64Encode(str) {
  return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
    (_, p1) => String.fromCharCode('0x' + p1)));
}

Python

import base64

# Encode
base64.b64encode(b"Hello World").decode()  # "SGVsbG8gV29ybGQ="

# Decode
base64.b64decode("SGVsbG8gV29ybGQ=").decode()  # "Hello World"

# Encode a file
with open("image.png", "rb") as f:
    encoded = base64.b64encode(f.read()).decode()

Command Line

# Encode
echo -n "Hello World" | base64
# SGVsbG8gV29ybGQ=

# Decode
echo "SGVsbG8gV29ybGQ=" | base64 --decode
# Hello World

# Encode a file
base64 image.png > image.b64

# Decode a file
base64 --decode image.b64 > image.png

Base64 vs Base64URL

Character Base64 Base64URL
62nd + -
63rd / _
Padding = Optional (often omitted)

Base64URL is used in JWTs, URLs, and filenames where +, /, and = would cause problems.

Frequently Asked Questions

Is Base64 encryption? No. Base64 is encoding, not encryption. Anyone can decode it — there is no key or password. Do not use Base64 to "hide" sensitive data.

Why is my Base64 string so long? Base64 increases size by ~33%. A 1 MB file becomes ~1.33 MB in Base64. This is normal.

Can I Base64 encode any file? Yes — images, PDFs, videos, executables, anything. But large files create very long strings.

What does the = at the end mean? Padding. Base64 works in groups of 3 bytes → 4 characters. If the input is not a multiple of 3 bytes, = or == is added to fill the gap.

Related Tools