What Is Base64 Encoding and How Does It Work? A Practical Guide

Understand how Base64 turns bytes into text, why output grows by a third, how Base64URL differs, and how to encode or decode text safely with UTF-8.

Updated September 29, 2026 6 min read

Use the free Base64 Encode / DecodeEncode text to Base64 or decode Base64 to text

Base64 turns up everywhere once you start looking: in email attachments, in HTTP Basic authentication headers, in the middle section of a JSON Web Token, in data: URLs inside CSS, and in API fields that need to carry a file. It usually appears as a long run of letters and digits ending in one or two = signs, and it is often mistaken for encryption.

This guide explains what Base64 actually does, why the output is always longer than the input, where the URL-safe variant fits in, and how to encode and decode text correctly, including the character-set problem that trips up many developers.

Why Base64 exists

Many systems were designed to carry text, not arbitrary bytes. Early email, some HTTP headers, XML and JSON documents, and URLs all assume a restricted set of characters. Raw binary data such as an image, a compressed file or an encryption key can contain byte values that these channels would corrupt, strip or misinterpret.

Base64 solves this by representing any sequence of bytes using only 64 safe characters: A-Z, a-z, 0-9, + and /, plus = for padding. The encoded text can pass through almost any text channel unchanged, and the receiver decodes it back to the exact original bytes.

How the encoding works

The algorithm (defined in RFC 4648) works on groups of three bytes:

  1. Take three bytes, which is 24 bits.
  2. Split those 24 bits into four groups of 6 bits.
  3. Each 6-bit group is a number from 0 to 63. Look it up in the 64-character alphabet.
  4. Repeat until the input runs out. If the last group has only one or two bytes, pad the output with = so its length is a multiple of four.

For example, the text Hello, world (12 bytes) encodes to SGVsbG8sIHdvcmxk (16 characters). Because every 3 bytes become 4 characters, Base64 output is about 33% larger than the input, plus a little padding. That overhead is the main cost of using it.

Input bytes in final group Output characters Padding
3 4 none
2 3 one =
1 2 two ==

Base64 is not encryption

Anyone can decode Base64 instantly; there is no key and no secret. It hides nothing. Treat a Base64 string exactly as you would treat the plain text inside it. If you see a password or API key in Base64 in a config file or a request header, it is effectively stored in plain text. For integrity checks use a hash, and for secrecy use real encryption.

Standard Base64 vs Base64URL

The standard alphabet uses + and /, which have special meanings in URLs and file names. The Base64URL variant replaces them with - and _, and usually drops the = padding. JSON Web Tokens use Base64URL for all three of their parts.

Buffer.from("?id=1&x=ü").toString("base64");    // "P2lkPTEmeD3DvA=="
Buffer.from("?id=1&x=ü").toString("base64url"); // "P2lkPTEmeD3DvA"

If a decoder rejects a string containing - or _, it is probably Base64URL. Swap the characters back and add padding, or use a decoder that accepts both.

Encode or decode text with our tool

The Base64 Encode / Decode tool runs in your browser, so the text you paste is never uploaded.

  1. Open Base64 Encode / Decode and choose Encode or Decode at the top.
  2. Type or paste into the Input box. To encode, enter plain text; to decode, paste the Base64 string.
  3. Click the Encode or Decode button under the input. The result appears in the Output panel.
  4. Click Copy Result to copy it. The swap button between the two modes moves the output back into the input and flips the direction, which is handy for checking a round trip.
  5. To get the Base64 of a whole file, use File to Base64 and pick the file. The output is the raw Base64 without a data: prefix.

Two details make the tool forgiving. Text is encoded as UTF-8 first, so accented letters, non-Latin scripts and symbols such as € work correctly. When decoding, it strips whitespace and line breaks, accepts Base64URL characters and restores missing padding. If the decoded bytes are not valid UTF-8 text (for example, the Base64 of an image), you will see Invalid input, because the output panel shows text only. For images, use Base64 to Image instead, and to go the other way use Image to Base64.

The UTF-8 trap in JavaScript

The browser's built-in btoa() function only accepts characters whose code is below 256. Any character outside that range makes it throw an InvalidCharacterError:

btoa("café €"); // throws InvalidCharacterError

The fix is to convert the string to UTF-8 bytes first:

function toBase64(text) {
  const bytes = new TextEncoder().encode(text);
  let binary = "";
  bytes.forEach((b) => (binary += String.fromCharCode(b)));
  return btoa(binary);
}
toBase64("café €"); // "Y2Fmw6kg4oKs"

In Node.js, Buffer.from(text, "utf8").toString("base64") does the same in one line, and Buffer.from(b64, "base64").toString("utf8") decodes.

Command-line options

On macOS and Linux the base64 utility is built in:

printf 'Hello, world' | base64          # SGVsbG8sIHdvcmxk
printf 'SGVsbG8sIHdvcmxk' | base64 --decode
base64 report.pdf > report.b64          # encode a file

Use printf or echo -n rather than plain echo, which adds a newline that becomes part of the encoded data. Also note that GNU base64 wraps output at 76 characters by default; add -w 0 for a single line.

In Windows PowerShell:

[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("Hello, world"))
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("SGVsbG8sIHdvcmxk"))

For files, the command line is better than a browser tool: it streams the data and handles large files without holding everything in a text box.

Troubleshooting

The decoded text is garbled

The original text was probably encoded with a different character set, such as Windows-1252 instead of UTF-8. Decode to bytes and try the other encoding, or ask the sender which one they used.

"Invalid character" or "incorrect padding"

Check for spaces, line breaks, a data:image/png;base64, prefix, or quote marks copied along with the string. Remove anything that is not part of the Base64 alphabet.

The result has an extra newline

The input had one. Shell echo, text editors that add a final newline, and copy-paste from terminals are the usual sources.

If you are deciding whether to embed images as Base64 in a page or stylesheet, read when to inline images as Base64 data URIs for the size and caching trade-offs.

Frequently asked questions

Why does Base64 end with = or ==?

The equals signs are padding. They appear when the input length is not a multiple of three bytes, so that the output length is always a multiple of four characters.

Is Base64 safe for storing passwords?

No. Base64 is reversible by anyone without a key. Passwords should be stored with a slow password hash such as bcrypt, never encoded.

How much bigger does Base64 make a file?

About one third larger: every 3 bytes of input become 4 characters of output. Line wrapping, if used, adds a little more.

What is the difference between Base64 and URL encoding?

Base64 converts arbitrary bytes into a 64-character alphabet. URL encoding (percent-encoding) keeps readable characters as they are and only escapes reserved ones as %XX. Use the URL Encoder for query strings.

Can I decode Base64 without uploading it?

Yes. Our tool, the base64 command and PowerShell all decode locally on your own device.

Try it now: Base64 Encode / Decode

Loading tool…