Skip to main content
Developer7 min readPhoto of Muhammad Irfan LiaqatBy Muhammad Irfan Liaqat

Base64 Encoding: What It Is and Why

How Base64 turns bytes into text, why it adds 33%, how base64url differs, and why the same word can encode two ways. Every example tested.

“Hello, world!” encoded to Base64 as SGVsbG8sIHdvcmxkIQ== and decoded back again.

Base64 turns any data, such as an image, a PDF or a line of text, into plain letters, digits and two symbols. You use it when a system can only carry text but you need to send bytes. It is not encryption, and it makes data about a third larger.

The examples below were checked in the Base64 Encoder/Decoder and in Node.js, which uses the same JavaScript engine as Chrome.

What Base64 Does

Computers store everything as bytes. Some channels, such as email bodies, URLs, JSON and HTTP headers, were designed for text, and certain byte values would corrupt them. Base64 rewrites the bytes using 64 safe characters:

  • A to Z (26)
  • a to z (26)
  • 0 to 9 (10)
  • + and / (2)

A 65th character, =, pads the end. This alphabet is defined in section 4 of RFC 4648, the standard most software follows.

How the Encoding Works

Base64 reads the input three bytes at a time. Three bytes are 24 bits, which it splits into four groups of 6 bits. Each 6-bit group is a number from 0 to 63, and that number picks a character from the alphabet.

The word "Man" is three bytes, so it becomes exactly four characters:

Man  →  TWFu

When the input doesn't divide evenly by three, the last group is padded with =:

InputBytesBase64
Man3TWFu
Ma2TWE=
M1TQ==
Hello, world!13SGVsbG8sIHdvcmxkIQ==

That is why Base64 output is always a multiple of four characters, and why you see one or two = signs at the end of many strings.

Why Base64 Makes Data Bigger

Every 3 bytes become 4 characters, so the output is 4/3 of the input: about 33% larger. A 3,000-byte file becomes 4,000 characters. Some email systems also add a line break every 76 characters, which adds a little more.

That cost is fine for a small icon. For a 2 MB photo, it means sending about 2.7 MB of text.

Where Base64 Is Used

  • Data URLs: a small image can sit inside HTML or CSS as data:image/png;base64, followed by the encoded bytes, which saves a separate request.
  • Email attachments: MIME encodes attachments as Base64 so they survive mail servers built for text.
  • HTTP Basic authentication: the browser sends the username and password joined by a colon, encoded as Base64. "user:pass" becomes dXNlcjpwYXNz, which anyone can decode, so Basic auth is only safe over HTTPS.
  • JSON APIs: JSON has no binary type, so APIs that accept file uploads in JSON often expect the file as a Base64 string.
  • JWT tokens: each part of a JWT is encoded with the URL-safe variant described below. The JWT Decoder decodes them.

Base64 vs Base64url

The + and / characters have special meanings in URLs and file names. Section 5 of RFC 4648 defines a URL-safe variant, base64url, that swaps them:

Standard Base64base64url
+-
/_
= paddingUsually left off

The same two bytes encode as +/8= in standard Base64 and -_8 in base64url. A standard decoder rejects the - and _ characters. To decode a base64url string with a standard tool, replace - with + and _ with /, then decode.

Text Has to Become Bytes First

Base64 encodes bytes, not letters. Before encoding text, a program has to choose how to turn the letters into bytes, and the choice changes the result.

The word "café" shows the difference:

MethodResult
UTF-8 (what the Alventra tool uses)Y2Fmw6k=
JavaScript's btoa() on its ownY2Fm6Q==

The browser's built-in btoa() treats each character as a single Latin-1 byte, and it throws an error on characters outside that range, such as emoji or Arabic script. The Base64 Encoder/Decoder converts text to UTF-8 first. That way "café" decodes correctly in other languages and tools, and an emoji such as 🙂 encodes to 8J+Zgg== instead of failing.

If you compare outputs from two tools and they differ, check whether both use UTF-8.

What the Decoder Forgives

Browsers follow a forgiving decoding rule. In testing, the tool's decoder:

  • Accepts missing padding: SGVsbG8 decodes to "Hello" without the final =.
  • Ignores spaces and line breaks: a string copied from an email with line breaks still decodes.
  • Rejects other characters: a - or _ from base64url gives an "Invalid input" message.

Base64 Is Not Encryption

Base64 has no key. Anyone can reverse it in a second, so never use it to hide passwords, tokens or personal data. If you need to check that data hasn't changed, use a hash: the Hash Generator creates SHA-256 fingerprints. If you need to keep data secret, use real encryption.

Base64 is not compression either. It always makes data larger.

When to Use It, and When Not To

Use Base64 for:

  • Icons and small images under a few kilobytes in CSS or HTML
  • Binary data inside JSON, XML or other text formats
  • Systems that only accept text, such as some form fields and environment variables

Avoid it for:

  • Large images or files, which should be sent as normal file uploads
  • Anything secret
  • Data sent repeatedly, where the 33% overhead adds up

Takeaways

  • Base64 turns every 3 bytes into 4 text characters, so data grows by about 33%.
  • The = signs are padding, added when the input length isn't a multiple of three.
  • base64url swaps + and / for - and _, so it is safe in URLs and JWTs.
  • Text must be converted to bytes first, and UTF-8 is the safe choice.
  • Base64 hides nothing: it is encoding, not encryption.

Paste some text into the Base64 Encoder/Decoder to see it encoded as you type, or turn an image into a data URL with Image to Base64.