Base64 Encoder

Encode text or data to Base64 format. Perfect for encoding binary data for web transmission.

0 characters

What is Base64 Encoding?

Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It's commonly used to encode binary data for transmission over media that only support text, such as email or HTTP. Developed as part of the MIME (Multipurpose Internet Mail Extensions) standard, Base64 has become a fundamental encoding mechanism in modern web development, APIs, and data exchange protocols.

Common Uses of Base64 Encoding

  • Data URIs in HTML/CSS: Embedding images and other files directly in HTML or CSS using data URIs (e.g., data:image/png;base64,iVBORw0KG...). This eliminates separate HTTP requests for small images, improving page load times. Commonly used for icons, small logos, and inline SVGs.
  • JSON API Transmission: Sending binary data through JSON APIs that only support text. Since JSON is a text format, you cannot directly include binary data (images, PDFs, encrypted data). Base64 encoding converts binary to text, allowing transmission in JSON payloads. RESTful APIs frequently use this for file uploads and downloads.
  • Email Attachments (MIME): MIME (Multipurpose Internet Mail Extensions) uses Base64 to encode email attachments. When you send a PDF, image, or any file via email, it's typically Base64 encoded in the MIME message body. The receiving email client decodes it back to the original file.
  • HTTP Basic Authentication: HTTP Basic Authentication uses Base64 to encode credentials (username:password) in the Authorization header. For example, "Authorization: Basic dXNlcjpwYXNzd29yZA==" where the encoded string is "user:password" in Base64. Note: This is NOT secure without HTTPS, as Base64 is easily decoded.
How Does Base64 Encoding Work?

Base64 encoding converts 8-bit binary data into 6-bit characters using a set of 64 ASCII characters (A-Z, a-z, 0-9, +, /). Every 3 bytes of input data are converted to 4 Base64 characters, which increases the data size by approximately 33%. Here's the technical process:

  • Step 1 - Group into 3-byte blocks: The input data is divided into groups of 3 bytes (24 bits).
  • Step 2 - Split into 6-bit segments: Each 24-bit block is split into four 6-bit segments.
  • Step 3 - Map to Base64 characters: Each 6-bit value (0-63) is mapped to one of 64 ASCII characters: A-Z (values 0-25), a-z (26-51), 0-9 (52-61), + (62), / (63).
  • Step 4 - Padding: If the input length isn't divisible by 3, padding characters (=) are added to make the output length a multiple of 4.

Example: The string "cat" (ASCII: 99, 97, 116) encodes to "Y2F0". The bytes [99, 97, 116] become binary 01100011 01100001 01110100, which splits into 6-bit groups: 011000 110110 000101 110100, mapping to Y(24) 2(54) F(5) 0(52).

History and Background

Base64 encoding originated with RFC 2045 (MIME) in 1996 as a way to encode email attachments. Before Base64, sending files via email was problematic because email protocols were designed for 7-bit ASCII text. Base64 solved this by converting 8-bit binary data into 7-bit safe ASCII characters. Today, Base64 is defined by RFC 4648 and is used far beyond email in web development, APIs, authentication, data URIs, and more.

Is Base64 Encoding Secure?

Base64 is encoding, not encryption. It provides no security and can be easily decoded by anyone. Never use Base64 alone to protect sensitive data. Learn about encoding security

Base64 Encoding in Programming Languages

Most programming languages have built-in functions for Base64 encoding. Here are examples in popular languages:

// Basic encoding
$encoded = base64_encode($data);

// Encoding UTF-8 string
$text = "Hello, 世界!";
$encoded = base64_encode($text);

// Encoding file contents
$fileData = file_get_contents('image.png');
$encoded = base64_encode($fileData);
// Browser (btoa for ASCII only)
const encoded = btoa('Hello'); // Basic ASCII

// Browser with UTF-8 support
const encoded = btoa(unescape(encodeURIComponent('Hello, 世界!')));

// Modern browsers with TextEncoder
const encoder = new TextEncoder();
const data = encoder.encode('Hello, 世界!');
const encoded = btoa(String.fromCharCode(...data));
import base64

# Encode string (automatic UTF-8)
text = "Hello, 世界!"
encoded = base64.b64encode(text.encode('utf-8')).decode('ascii')

# Encode bytes directly
data = b'\x00\x01\x02\x03'
encoded = base64.b64encode(data).decode('ascii')

# Encode file
with open('image.png', 'rb') as f:
    encoded = base64.b64encode(f.read()).decode('ascii')
import "encoding/base64"

// Basic encoding
data := []byte("Hello, 世界!")
encoded := base64.StdEncoding.EncodeToString(data)

// URL-safe encoding
encoded := base64.URLEncoding.EncodeToString(data)

// Encoding to writer (for large data)
encoder := base64.NewEncoder(base64.StdEncoding, outputWriter)
encoder.Write(data)
encoder.Close()
import java.util.Base64;
import java.nio.charset.StandardCharsets;

// Basic encoding
String text = "Hello, 世界!";
String encoded = Base64.getEncoder()
    .encodeToString(text.getBytes(StandardCharsets.UTF_8));

// URL-safe encoding
String encoded = Base64.getUrlEncoder()
    .encodeToString(text.getBytes(StandardCharsets.UTF_8));

// MIME encoding (line breaks every 76 chars)
String encoded = Base64.getMimeEncoder()
    .encodeToString(text.getBytes(StandardCharsets.UTF_8));
require 'base64'

# Basic encoding
text = "Hello, 世界!"
encoded = Base64.encode64(text)

# Strict encoding (no newlines)
encoded = Base64.strict_encode64(text)

# URL-safe encoding
encoded = Base64.urlsafe_encode64(text)
using System;
using System.Text;

// Basic encoding
string text = "Hello, 世界!";
byte[] bytes = Encoding.UTF8.GetBytes(text);
string encoded = Convert.ToBase64String(bytes);

// With line breaks (for MIME)
string encoded = Convert.ToBase64String(bytes,
    Base64FormattingOptions.InsertLineBreaks);

Related Tools

Need to decode Base64? Use our Base64 Decoder to convert Base64 strings back to original text or binary data.

Working with URLs? Try our URL Encoder to make text URL-safe.

Displaying content on web pages? Use our HTML Entity Encoder to prevent XSS attacks.