DeveloperMarch 29, 2026

Base64 Encoder/Decoder Guide: What It Is & When to Use It

By The hakaru Team·Last updated March 2026

Quick Answer

  • *Base64 encoding converts binary data into a string of 64 printable ASCII characters, making it safe to transmit over text-based protocols like email and HTTP.
  • *Every 3 bytes of input becomes 4 characters of output — a 33% size increase. Padding (= or ==) fills the final group if needed.
  • *The 64-character alphabet: A–Z, a–z, 0–9, +, /. Base64URL swaps + for - and / for _ for URL safety.
  • *Top uses: email attachments (MIME), HTTP Basic Auth, data URIs, JWT tokens, and API keys embedded in configs.

What Is Base64 Encoding?

Base64 is a binary-to-text encoding scheme defined in RFC 4648. It converts arbitrary binary data — images, PDFs, cryptographic keys, anything — into a string made up of only 64 printable ASCII characters. That constraint is the whole point: many protocols (email, HTTP headers, URLs, XML) were designed for human-readable text and can't reliably carry raw binary bytes. Base64 solves that.

It's not encryption. It's not compression. The encoded string is trivially decodable by anyone who knows it's Base64. Think of it as a packing format — like putting something awkward-shaped into a standardized box so it survives shipping.

How Base64 Works: 3 Bytes In, 4 Characters Out

The core mechanic is simple. Base64 processes input data in 3-byte chunks. Each byte is 8 bits, so 3 bytes = 24 bits. Those 24 bits are split into four 6-bit groups. Each 6-bit value (0–63) maps to one character in the Base64 alphabet. Result: 3 bytes become 4 characters.

Let's trace through the word Man:

InputASCII ValueBinary (8 bits)
M7701001101
a9701100001
n11001101110

Concatenated: 010011010110000101101110 (24 bits). Split into four 6-bit groups: 010011 (19), 010110 (22), 000101 (5), 101110 (46). Those map to: T, W, F, u. So Man encodes to TWFu.

Padding with =

Input data rarely divides evenly into 3-byte groups. When the last group has only 1 byte (8 bits), it gets padded to 12 bits with zeros and produces 2 Base64 characters followed by ==. When the last group has 2 bytes (16 bits), it gets padded to 18 bits and produces 3 characters followed by =. This keeps the output length a multiple of 4.

The Base64 Character Set

The 64-character alphabet is defined in RFC 4648 Table 1:

Index RangeCharactersCount
0–25A B C D E F G H I J K L M N O P Q R S T U V W X Y Z26
26–51a b c d e f g h i j k l m n o p q r s t u v w x y z26
52–610 1 2 3 4 5 6 7 8 910
62+1
63/1
Padding=

All 64 characters are printable ASCII. No control characters, no null bytes, no characters that require escaping in JSON or HTML. That's what makes Base64 universally safe for text protocols.

Top 5 Use Cases for Base64 Encoding

1. Email Attachments (MIME)

This is where Base64 was born. The MIME standard (RFC 2045), published in 1996, specifies Base64 as one of the two standard content-transfer-encodings for email. Email servers were built on 7-bit ASCII — a PDF or image in raw binary form would get corrupted in transit. Base64 solves this by encoding the binary file as safe ASCII text. Every time you attach a file to an email, your client is Base64-encoding it behind the scenes.

2. HTTP Basic Authentication

HTTP Basic Auth sends credentials in the Authorization header as Basic [credentials], where credentials is the Base64 encoding of username:password. For example, admin:secret becomes YWRtaW46c2VjcmV0. This is specified in RFC 7617. Note: Basic Auth over plain HTTP is insecure — the Base64 is trivially reversible. Always use HTTPS.

3. Data URIs for Images and Assets

Data URIs embed files directly in HTML or CSS, eliminating an HTTP request. The format: data:[mediatype];base64,[encoded-data]. A small PNG icon might look like data:image/png;base64,iVBORw0KGgo... as an img src value. According to HTTP Archive data, roughly 15% of all web pagesuse at least one data URI. They're most valuable for tiny images (under 1-2KB) where the request overhead exceeds the size penalty.

4. JWT Tokens

JSON Web Tokens — the authentication standard used by virtually every modern web API — are three Base64URL-encoded segments separated by dots: [header].[payload].[signature]. The header and payload are Base64URL-encoded JSON objects; the signature is a Base64URL-encoded HMAC or RSA signature. JWT is defined in RFC 7519 and is the dominant token format in OAuth 2.0 and OpenID Connect implementations. You can use our JWT decoder tool to inspect any JWT token.

5. API Keys and Configuration Secrets

Many systems encode binary secrets (cryptographic keys, random bytes) as Base64 strings for storage in environment variables, config files, and secret managers. Kubernetes secrets are stored as Base64-encoded values. SSH private keys in PEM format use Base64. AWS and GCP service account credentials are Base64-encoded JSON blobs. Base64 makes arbitrary binary secrets portable as plain text.

Base64 vs Base64URL

Standard Base64 uses + and / as its last two characters. Both have reserved meanings in URLs (+ means space in query strings; / is a path separator) and in HTTP headers. This causes problems when Base64 strings appear in URLs or headers.

Base64URL (RFC 4648 Section 5) solves this with two substitutions:

VariantCharacter 62Character 63PaddingPrimary use
Base64+/= requiredEmail, data URIs, file encoding
Base64URL-_Often omittedJWT tokens, OAuth, URL params

If you're decoding a JWT and getting errors, check whether your decoder is using Base64URL mode. Standard Base64 decoders will choke on the - and _ characters.

Base64 vs Other Encoding Schemes

Base64 is not the only way to encode binary data as text. Here's how it compares to the alternatives:

EncodingAlphabet sizeSize overheadReadabilityCommon use
Binary2 chars (0, 1)8× rawVery lowDebug/educational
Hex (Base16)16 chars2× rawModerateHash digests, color codes
Base3232 chars1.6× rawHigh (no confusion chars)TOTP secrets, file names
Base6464 chars1.33× rawModerateEmail, JWT, HTTP auth
Base8585 chars1.25× rawLowPDF, git pack files

Hex encoding (Base16) is common for hash outputs — SHA-256 produces a 32-byte digest that appears as 64 hex characters. You'll see it in checksums, color codes (#FF5733), and UUID strings. Our hash generator outputs hex by default.

Base32 uses only uppercase letters and digits 2-7, avoiding visually ambiguous characters (no 0/O confusion, no 1/I confusion). That's why TOTP authenticator secrets (like the QR code you scan when setting up two-factor authentication) are Base32-encoded.

Common Base64 Decoding Errors

Invalid character in Base64 string

You're likely trying to decode a Base64URL string with a standard decoder (or vice versa). Replace - with + and _ with /, then decode. Alternatively, use our tool's URL-safe mode.

Incorrect padding

Some implementations strip the trailing = padding. If your Base64 string length isn't a multiple of 4, add = characters until it is. One missing: add =. Two missing: add ==.

Line breaks in the string

MIME Base64 (email) inserts a line break every 76 characters per RFC 2045. If you copy a Base64 string from an email source, strip all whitespace before decoding.

Working with Base64 in Code

Every major language has built-in Base64 support:

  • JavaScript/Node.js: btoa(str) / atob(str) in browsers; Buffer.from(str, 'base64') in Node
  • Python: import base64; base64.b64encode(b'data') and base64.b64decode('...')
  • Go: encoding/base64 standard library; use base64.URLEncoding for Base64URL
  • Java: java.util.Base64.getEncoder().encodeToString(bytes)
  • Bash: echo -n 'text' | base64 and echo 'encoded' | base64 --decode

For quick ad-hoc encoding and decoding without switching contexts, our Base64 Encoder/Decoder handles both standard Base64 and Base64URL with a single click. Also useful: our URL encoder/decoder for percent-encoding, our JSON formatter for pretty-printing API responses, and our regex tester for validating Base64 patterns.

Encode or decode Base64 instantly

Try our free Base64 Encoder/Decoder →

Need to inspect a JWT? Try our JWT Decoder

Frequently Asked Questions

What is Base64 encoding?

Base64 is a binary-to-text encoding scheme that converts binary data into a string of 64 printable ASCII characters. It groups 3 bytes of binary data into 4 six-bit values, each mapped to one character from the Base64 alphabet (A–Z, a–z, 0–9, +, /). This makes binary data safe to transmit over text-based protocols.

Does Base64 encoding compress or encrypt data?

No. Base64 is neither compression nor encryption. It actually increases data size by about 33% — every 3 bytes becomes 4 characters. It is purely an encoding scheme that makes binary data representable as ASCII text. For security, you need separate encryption; for size reduction, use a compression algorithm like gzip first.

What is the difference between Base64 and Base64URL?

Base64URL is a URL-safe variant of Base64 that replaces + with - and / with _, and typically omits padding (=). Standard Base64 uses + and / which have special meanings in URLs and HTTP headers. Base64URL is used in JWT tokens, OAuth tokens, and any context where the encoded string appears in a URL or HTTP header.

Why do some Base64 strings end with = or ==?

Base64 encodes in 3-byte groups. When the input length is not divisible by 3, padding characters (=) fill the remaining slots. One = means the last group had 2 bytes; two ==means it had 1 byte. Padding ensures the output length is always a multiple of 4 characters. Some implementations omit padding — both forms are valid.

Can I use Base64 to encode images for use in HTML or CSS?

Yes. Data URIs embed Base64-encoded images directly in HTML or CSS, eliminating an HTTP request. The format is: data:[mediatype];base64,[data]. For small icons or critical above-the-fold images this can improve performance. For images over 10–20KB, external files are typically faster because they can be cached and served in parallel.

Is Base64 safe to use in email?

Yes — that is exactly what it was designed for. The MIME standard (RFC 2045, published 1996) specifies Base64 as one of the two standard content-transfer-encodings for email attachments. Email servers historically could only handle 7-bit ASCII text, so Base64 allowed binary files like PDFs and images to survive transit across incompatible mail servers.