Back to Tools

Base64 Encoder

Encode text to Base64 format instantly. You can also decode Base64 by flipping the order.

Text
0 chars
Base64
0 chars
πŸ’‘ Tip: Type in either field to convert automatically

What is Base64?

Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It's called "Base64" because it uses 64 different characters to represent data: 26 uppercase letters (A-Z), 26 lowercase letters (a-z), 10 digits (0-9), plus two additional characters (+ and /), with = used for padding.

Base64 encoding converts binary data into a text format that can be safely transmitted over text-based protocols like HTTP, email, or stored in text files like JSON or XML.

Why Base64 Matters

  • Text-safe transmission: Ensures binary data can be sent through text-only channels
  • Data integrity: Prevents data corruption when transferring binary through text protocols
  • Embedding binary in text: Allows images, files, and binary data in JSON, XML, and HTML
  • Security: Used in authentication tokens, certificates, and encrypted data
  • Cloud infrastructure: Essential for Kubernetes secrets, Docker configs, and API keys

How Base64 Encoding Works

Base64 encoding takes binary data and converts it into a string of ASCII characters. The process works as follows:

The Encoding Process

  1. Take binary data: Each byte (8 bits) of binary data
  2. Group into 6-bit chunks: Base64 uses 6 bits per character (2^6 = 64 possible values)
  3. Map to characters: Each 6-bit value maps to one of 64 characters
  4. Add padding: If the input isn't divisible by 3 bytes, add = padding

Character Set

Base64 uses these 64 characters:

A-Z  (26 characters: A, B, C, ..., Z)
a-z  (26 characters: a, b, c, ..., z)
0-9  (10 characters: 0, 1, 2, ..., 9)
+    (1 character)
/    (1 character)
=    (padding character, not part of the 64)

Example Encoding

Let's encode the word "Hi":

  1. Text: "Hi"
  2. ASCII values: H = 72, i = 105
  3. Binary: H = 01001000, i = 01101001
  4. Combined: 0100100001101001 (16 bits)
  5. Grouped in 6-bit chunks:
    • 010010 = 18 β†’ S
    • 000110 = 6 β†’ G
    • 100100 = 36 β†’ k
    • (need 2 more bits, pad with 00) = 00 β†’ A
  6. Result: "SGk=" (with padding)

Padding Explained

Base64 encoding works with groups of 3 bytes (24 bits), which become 4 Base64 characters. If the input length isn't divisible by 3:

  • 1 byte remaining: Add 2 padding characters (==)
  • 2 bytes remaining: Add 1 padding character (=)

Examples:

  • "Hi" (2 bytes) β†’ "SGk=" (1 padding =)
  • "H" (1 byte) β†’ "SA==" (2 padding ==)
  • "Hello" (5 bytes) β†’ "SGVsbG8=" (1 padding =)

Common Use Cases

1. Kubernetes Secrets

Kubernetes stores secrets as Base64-encoded strings:

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  username: YWRtaW4=        # "admin" in Base64
  password: cGFzc3dvcmQxMjM= # "password123" in Base64

Encoding secrets:

echo -n "admin" | base64
# Output: YWRtaW4=
 
echo -n "password123" | base64
# Output: cGFzc3dvcmQxMjM=

Decoding secrets:

echo "YWRtaW4=" | base64 -d
# Output: admin

2. Docker Config Files

Docker uses Base64 for registry authentication:

{
  "auths": {
    "registry.example.com": {
      "auth": "dXNlcm5hbWU6cGFzc3dvcmQ="
    }
  }
}

The auth field contains username:password encoded in Base64.

3. Data URLs (Embedding Images)

Base64 allows embedding images directly in HTML/CSS:

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

4. API Authentication

Many APIs use Base64 for basic authentication:

const username = "user";
const password = "pass";
const credentials = btoa(`${username}:${password}`);
// credentials = "dXNlcjpwYXNz"
 
fetch('https://api.example.com/data', {
  headers: {
    'Authorization': `Basic ${credentials}`
  }
});

5. Email Attachments (MIME)

Email uses Base64 to encode binary attachments:

Content-Type: image/png
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="image.png"

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==

6. JSON Web Tokens (JWT)

JWTs consist of three Base64-encoded parts separated by dots:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

Each part (header, payload, signature) is Base64-encoded JSON.

7. AWS Secrets Manager / Parameter Store

When storing binary data or special characters:

# Encode a secret before storing
aws secretsmanager create-secret \
  --name my-secret \
  --secret-string $(echo -n "my-secret-value" | base64)

8. Git Configuration

Git sometimes uses Base64 for credential storage:

https://username:base64encodedpassword@github.com/user/repo.git

Base64 vs Other Encoding Methods

Base64 vs URL Encoding

FeatureBase64URL Encoding
PurposeBinary data encodingURL-safe text encoding
Size increase~33% largerVariable
Character setA-Z, a-z, 0-9, +, /Percent-encoded
Use caseBinary data, imagesURL parameters
ExampleSGVsbG8=Hello%20World

Base64 vs Hex Encoding

FeatureBase64Hex Encoding
Size increase~33% larger100% larger (2x)
Character set64 characters16 characters (0-9, A-F)
ReadabilityLess readableMore readable
Use caseCompact binary encodingDebugging, hashes

Example:

  • Original: "Hello"
  • Base64: SGVsbG8= (8 characters)
  • Hex: 48656c6c6f (10 characters)

Base64 Variants

Standard Base64

The standard Base64 encoding uses + and / as the 63rd and 64th characters, with = for padding.

Base64URL

A URL-safe variant that uses - and _ instead of + and /, and omits padding:

Standard: SGVsbG8= (with padding)
Base64URL: SGVsbG8 (no padding)

Used in:

  • JWT tokens
  • URL parameters
  • Filenames

MIME Base64

Same as standard Base64 but with line breaks every 76 characters for email compatibility.

Encoding/Decoding in Different Languages

JavaScript/TypeScript

Browser (built-in):

// Encode
const encoded = btoa("Hello World");
// "SGVsbG8gV29ybGQ="
 
// Decode
const decoded = atob("SGVsbG8gV29ybGQ=");
// "Hello World"

Node.js:

// Encode
const encoded = Buffer.from("Hello World").toString('base64');
// "SGVsbG8gV29ybGQ="
 
// Decode
const decoded = Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString();
// "Hello World"

Python

import base64
 
# Encode
encoded = base64.b64encode(b"Hello World").decode('utf-8')
# "SGVsbG8gV29ybGQ="
 
# Decode
decoded = base64.b64decode("SGVsbG8gV29ybGQ=").decode('utf-8')
# "Hello World"

Bash/Command Line

# Encode
echo -n "Hello World" | base64
# SGVsbG8gV29ybGQ=
 
# Decode
echo "SGVsbG8gV29ybGQ=" | base64 -d
# Hello World
 
# Or on macOS/BSD
echo "SGVsbG8gV29ybGQ=" | base64 -D

Go

import (
    "encoding/base64"
    "fmt"
)
 
// Encode
encoded := base64.StdEncoding.EncodeToString([]byte("Hello World"))
// "SGVsbG8gV29ybGQ="
 
// Decode
decoded, _ := base64.StdEncoding.DecodeString("SGVsbG8gV29ybGQ=")
// "Hello World"

Common Mistakes to Avoid

1. Double Encoding

Encoding already-encoded data creates incorrect results:

// Wrong
const once = btoa("Hello");
const twice = btoa(once); // Double encoding!
 
// Correct
const encoded = btoa("Hello");

2. Forgetting Padding

While some decoders handle missing padding, it's not standard:

// Incomplete (missing padding)
"SGVsbG8"  // Should be "SGVsbG8="
 
// Complete
"SGVsbG8="

3. Encoding Unicode Incorrectly

Direct encoding of Unicode strings can cause issues:

// Wrong (may fail with non-ASCII)
btoa("Hello δΈ–η•Œ");
 
// Correct (handle Unicode properly)
btoa(unescape(encodeURIComponent("Hello δΈ–η•Œ")));

4. Not Handling Line Breaks

Base64 strings in emails/MIME may have line breaks:

// Remove line breaks before decoding
const cleaned = base64String.replace(/\s/g, '');
const decoded = atob(cleaned);

5. Confusing Encoding with Encryption

Base64 is encoding, not encryption:

  • Encoding: Reversible transformation (no security)
  • Encryption: Requires a key to reverse (secure)

Base64-encoded data is easily decoded by anyone. Don't use it for security!

6. Using Base64 for Large Files

Base64 increases file size by ~33%. For large files, consider:

  • Direct binary transfer
  • Compression before encoding
  • Chunked encoding

Security Considerations

Base64 is NOT Encryption

Important: Base64 encoding provides no security. It's easily reversible by anyone.

// This is NOT secure
const "secret" = btoa("my-password");
// Anyone can decode: atob("bXktcGFzc3dvcmQ=") β†’ "my-password"

When Base64 is Used in Security

Base64 is used in security contexts, but not for hiding data:

  1. Transport encoding: To safely transmit binary data through text protocols
  2. Token formatting: JWT tokens use Base64 for structure, not security
  3. Certificate encoding: X.509 certificates are Base64-encoded for transmission

Best Practices

  1. Never rely on Base64 for security: Always use proper encryption (AES, RSA, etc.)
  2. Validate input: Check Base64 format before decoding to prevent errors
  3. Handle errors gracefully: Invalid Base64 strings should return clear error messages
  4. Use HTTPS: When transmitting Base64-encoded secrets, always use encrypted connections
  5. Sanitize output: When displaying decoded data, be careful with special characters

Performance Considerations

Size Overhead

Base64 encoding increases data size by approximately 33%:

  • Original: 3 bytes
  • Encoded: 4 characters (4 bytes in ASCII)
  • Overhead: 1 byte per 3 bytes = 33.33%

When Size Matters

For large files or high-volume data:

  • Consider compression before encoding
  • Use binary protocols when possible
  • Stream encoding/decoding for large files

Encoding Speed

Base64 encoding/decoding is fast but not free:

  • Small strings (< 1KB): Negligible overhead
  • Medium files (1KB - 1MB): Still very fast
  • Large files (> 1MB): Consider streaming or alternatives

Real-World Examples

Example 1: Kubernetes Secret Management

# Create a secret from literal values
kubectl create secret generic my-secret \
  --from-literal=username=admin \
  --from-literal=password=secret123
 
# View the secret (Base64 encoded)
kubectl get secret my-secret -o yaml
 
# Output:
# data:
#   username: YWRtaW4=
#   password: c2VjcmV0MTIz
 
# Decode to verify
echo "YWRtaW4=" | base64 -d
# admin

Example 2: Docker Registry Authentication

# Create Docker config with Base64 auth
echo -n "username:password" | base64
# dXNlcm5hbWU6cGFzc3dvcmQ=
 
# Use in ~/.docker/config.json
{
  "auths": {
    "registry.example.com": {
      "auth": "dXNlcm5hbWU6cGFzc3dvcmQ="
    }
  }
}

Example 3: API Basic Authentication

// Encode credentials
const username = "api_user";
const password = "api_key_12345";
const auth = btoa(`${username}:${password}`);
 
// Use in API request
fetch('https://api.example.com/data', {
  headers: {
    'Authorization': `Basic ${auth}`
  }
});

Example 4: Embedding Small Images

<!-- Embed a small logo directly in HTML -->
<img src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cmVjdCB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIgZmlsbD0iIzAwN2NmZiIvPjwvc3ZnPg==" alt="Logo" />

Frequently Asked Questions

Is Base64 encoding secure?

No. Base64 is encoding, not encryption. It's easily reversible and provides no security. Anyone can decode Base64-encoded data.

Why does Base64 increase file size?

Base64 converts 3 bytes of binary data into 4 ASCII characters. Since each ASCII character is 1 byte, 3 bytes become 4 bytes, resulting in a 33% size increase.

Can I use Base64 for passwords?

No. Base64 encoding is not encryption. Passwords should be hashed (using bcrypt, Argon2, etc.) or encrypted (using AES, etc.), never just Base64-encoded.

What's the difference between Base64 and Base64URL?

Base64URL is a URL-safe variant:

  • Uses - and _ instead of + and /
  • Omits padding (=)
  • Used in URLs, JWT tokens, and filenames

How do I handle Unicode characters?

Unicode characters need special handling:

JavaScript:

// Encode Unicode
btoa(unescape(encodeURIComponent("Hello δΈ–η•Œ")));
 
// Decode Unicode
decodeURIComponent(escape(atob("SGVsbG8g5LiW55WM")));

Python:

import base64
 
# Encode Unicode
base64.b64encode("Hello δΈ–η•Œ".encode('utf-8')).decode('utf-8')
 
# Decode Unicode
base64.b64decode(encoded).decode('utf-8')

Can Base64 strings have line breaks?

Yes. MIME Base64 includes line breaks every 76 characters for email compatibility. Always remove whitespace before decoding:

const cleaned = base64String.replace(/\s/g, '');
const decoded = atob(cleaned);

What characters are valid in Base64?

Valid Base64 characters:

  • Uppercase letters: A-Z
  • Lowercase letters: a-z
  • Digits: 0-9
  • Plus sign: +
  • Forward slash: /
  • Padding: = (only at the end, 0-2 characters)

How do I validate a Base64 string?

Check the format:

function isValidBase64(str) {
  const base64Regex = /^[A-Za-z0-9+/]*={0,2}$/;
  return base64Regex.test(str) && str.length % 4 === 0;
}

Why do I see "=" at the end of Base64 strings?

The = characters are padding. Base64 works with groups of 3 bytes. If the input length isn't divisible by 3:

  • 1 byte remaining β†’ 2 padding characters (==)
  • 2 bytes remaining β†’ 1 padding character (=)

Can I decode Base64 without padding?

Some decoders handle missing padding, but it's not standard. Always include proper padding for compatibility.

Additional Resources