X Xerobit

Base64 Encode Online — Encode Text and Files to Base64 Instantly

Base64 encoding converts binary data into printable ASCII text. It's used in email attachments, data URLs, JWT tokens, and API authentication. Here's how encoding works and...

Mian Ali Khalid · · 6 min read
Use the tool
Base64 Encoder / Decoder
Encode and decode Base64 strings and files. Client-side, safe for sensitive data.
Open Base64 Encoder / Decoder →

Base64 encoding converts arbitrary binary data into a string of 64 printable ASCII characters. The result is safe to include anywhere that only accepts text: JSON values, HTTP headers, email bodies, HTML attributes, URLs (with caveats).

Use the base64 encoder and decoder to encode text or files to Base64 and decode Base64 strings instantly.

What Base64 encoding does

Base64 takes 3 bytes of binary input (24 bits) and produces 4 ASCII characters (6 bits each). The character set uses letters (A–Z, a–z), digits (0–9), plus (+), and slash (/).

Input bytes: 77 61 6E     (decimal for "Man")
Binary:      01001101 01100001 01101110
             ↓ split into 6-bit groups ↓
             010011 010110 000101 101110
             = 19    22    5     46
             = T     W     F     u
Output:      "TWFu"

A = padding character is appended to reach a multiple of 4 output characters when the input isn’t a multiple of 3 bytes.

The overhead is fixed: Base64 output is always 33% larger than the input (4/3 ratio).

How to encode to Base64

Online (no installation)

  1. Open the Base64 Tool
  2. Type or paste text in the input field
  3. The Base64 output appears in real time
  4. Copy the encoded string

For files: drag and drop a file, and the tool encodes the raw bytes to Base64.

JavaScript (browser)

// Encode text to Base64:
const text = 'Hello, World!';
const encoded = btoa(text);
console.log(encoded); // "SGVsbG8sIFdvcmxkIQ=="

// Decode Base64 to text:
const decoded = atob(encoded);
console.log(decoded); // "Hello, World!"

// Handle Unicode (btoa only handles Latin-1):
function encodeUnicode(str) {
  return btoa(unescape(encodeURIComponent(str)));
}
function decodeUnicode(str) {
  return decodeURIComponent(escape(atob(str)));
}

// Using TextEncoder for binary (modern approach):
function toBase64(str) {
  return btoa(new TextEncoder().encode(str).reduce(
    (acc, byte) => acc + String.fromCharCode(byte), ''
  ));
}

JavaScript (Node.js)

// Encode:
const encoded = Buffer.from('Hello, World!').toString('base64');
console.log(encoded); // "SGVsbG8sIFdvcmxkIQ=="

// Decode:
const decoded = Buffer.from(encoded, 'base64').toString('utf8');
console.log(decoded); // "Hello, World!"

// Encode a file:
const fs = require('fs');
const fileData = fs.readFileSync('image.png');
const fileBase64 = fileData.toString('base64');

Python

import base64

# Encode text:
text = 'Hello, World!'
encoded = base64.b64encode(text.encode('utf-8'))
print(encoded)              # b'SGVsbG8sIFdvcmxkIQ=='
print(encoded.decode())     # SGVsbG8sIFdvcmxkIQ==

# Decode:
decoded = base64.b64decode(encoded).decode('utf-8')
print(decoded)              # Hello, World!

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

Command line (Linux/macOS)

# Encode text:
echo -n "Hello, World!" | base64
# SGVsbG8sIFdvcmxkIQ==

# Decode:
echo "SGVsbG8sIFdvcmxkIQ==" | base64 -d
# Hello, World!

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

# Decode a file:
base64 -d image.b64 > image-decoded.png

Note: echo adds a newline by default. Use echo -n to encode the text without a trailing newline — the newline would be included in the encoding otherwise.

Where Base64 encoding is used

Data URLs

Embed images directly in HTML/CSS without a separate file request:

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

Small images (< 5KB) as data URLs can reduce HTTP requests. Large images are slower to parse than file requests — the base64 size increase (33%) plus the parsing time makes it counterproductive for anything over 5KB.

Email attachments (MIME)

Email protocols transfer ASCII text, not binary. Email attachments are Base64-encoded in MIME format:

Content-Type: image/png; name="photo.png"
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0...

This is handled automatically by email clients. You’d only encounter this if parsing raw email files (.eml format).

HTTP Basic Authentication

Basic auth encodes username:password as Base64 in the Authorization header:

Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

dXNlcm5hbWU6cGFzc3dvcmQ= decodes to username:password. This is not encryption — it’s trivially reversible. Basic auth is only secure over HTTPS.

JWT tokens

The header and payload of a JWT are Base64URL-encoded (a URL-safe variant that uses - instead of + and _ instead of /):

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.HMAC_SIGNATURE
↑ header (Base64URL)                   ↑ payload (Base64URL)         ↑ signature

The header and payload aren’t encrypted — they’re just encoded. Anyone can decode them. The signature proves the token wasn’t tampered with.

API credentials and tokens

Many API keys and secrets are Base64-encoded binary tokens. If an API key looks like eyJ... or ends with ==, it’s likely Base64-encoded.

Base64 vs Base64URL

Standard Base64 uses + and / as the 62nd and 63rd characters. These are special characters in URLs. Base64URL substitutes:

  • +-
  • /_
  • = padding is often omitted

Use standard Base64 for data URLs, email, and general encoding. Use Base64URL for JWT, OAuth tokens, and anything embedded in a URL.

// Standard to Base64URL:
function toBase64URL(base64) {
  return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
}

Related posts

Related tool

Base64 Encoder / Decoder

Encode and decode Base64 strings and files. Client-side, safe for sensitive data.

Written by Mian Ali Khalid. Part of the Encoding & Crypto pillar.