Base64 encoder and decoder

Type on the left to encode to Base64, or paste Base64 on the right to decode it. Drop an image or a file to encode it, and get a decoded image or file back as a download. Nothing is uploaded.

Drop an image or any file here to encode it, or .

13 bytes in, 20 characters out.

Encoding and decoding happen in your browser. Nothing is uploaded.

How to use it

  • To encode text, type or paste it in the left box. The Base64 appears on the right as you type
  • To decode, paste Base64 in the right box. If it's text, it appears on the left. If it's an image or another file, you get a preview and a download link
  • To encode a file or an image, drop it on the dashed box. Tick "Files as a data URI" to get a data:image/png;base64,... string you can paste straight into HTML or CSS

The decoder accepts standard and URL-safe Base64, with or without the = padding, and ignores line breaks. You can paste a whole data URI and it will strip the prefix.

What Base64 is

Base64 is a way of writing binary data using only 64 ordinary characters: A-Z, a-z, 0-9, + and /. It exists because a lot of systems were built to carry text, and raw bytes get damaged when they pass through them.

It works by taking three bytes at a time, which is 24 bits, and splitting them into four groups of 6 bits. Each group becomes one character. That's why:

  • The output is a third bigger than the input. Every 3 bytes become 4 characters
  • There's padding. When the input isn't a multiple of three bytes, one or two = signs fill the last group
Text Bytes Base64
f 1 Zg==
fo 2 Zm8=
foo 3 Zm9v
foob 4 Zm9vYg==
foobar 6 Zm9vYmFy

Those examples are the test vectors from RFC 4648, the standard that defines Base64.

Base64 isn't encryption

Anyone can decode Base64. It hides nothing. It's a change of alphabet, like writing a number in hexadecimal.

This matters because Base64 looks scrambled, and credentials are regularly left "protected" by nothing else. An HTTP Basic authentication header is the username and password in Base64. The payload of a JSON Web Token is Base64 too, and readable by whoever holds the token. If something needs to be secret, encrypt it. If it needs to be tamper-proof, sign it.

URL-safe Base64

Standard Base64 uses + and /, which both mean something else inside a URL, and =, which does too. The URL-safe variant, also in RFC 4648, swaps + for - and / for _, and usually drops the padding.

Standard URL-safe
Characters 62 and 63 + and / - and _
Padding = Usually left off
Used in Email, PEM files, data URIs JSON Web Tokens, URLs, file names

Tick "URL-safe" to encode that way. Decoding works for both without any setting.

Encoding an image as Base64

A data URI puts a file's contents directly in a web page:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUg..." alt="">
.icon { background-image: url("data:image/svg+xml;base64,PHN2Zy..."); }

It saves a network request, which helps for a small icon. For anything large it's a bad trade: the Base64 is a third bigger than the file, it can't be cached separately from the page, and the browser has to decode it before showing it. A few kilobytes is a sensible upper limit.

Going the other way, paste a data URI or the bare Base64 of an image into the right box. The decoder recognises PNG, JPEG, GIF and WebP from their first bytes and shows a preview.

Base64 from the command line and in code

macOS and Linux

echo -n "Hello, world!" | base64          # encode
echo "SGVsbG8sIHdvcmxkIQ==" | base64 -d   # decode
base64 -i photo.png -o photo.txt          # a file, on macOS

The -n matters. Without it, echo adds a line break and you encode that too.

JavaScript

btoa("Hello, world!")            // "SGVsbG8sIHdvcmxkIQ=="
atob("SGVsbG8sIHdvcmxkIQ==")     // "Hello, world!"

btoa only handles characters up to code 255. For text with accents, emoji or non-Latin scripts, encode to UTF-8 bytes first:

const bytes = new TextEncoder().encode("héllo ✓")
const base64 = btoa(String.fromCharCode(...bytes))

Python

import base64
base64.b64encode("Hello, world!".encode())      # b'SGVsbG8sIHdvcmxkIQ=='
base64.b64decode("SGVsbG8sIHdvcmxkIQ==").decode()   # 'Hello, world!'
base64.urlsafe_b64encode(b"\xfb\xff")           # b'-_8='

Where you'll run into Base64

  • PEM files. A certificate or a key between -----BEGIN and -----END lines is Base64 of binary data. The CSR decoder reads those
  • Checksums in update feeds. Auto-update manifests often carry a file's SHA-512 as Base64. The checksum calculator shows a file's hash in both hex and Base64
  • API keys and secrets. Random bytes written as Base64 or its URL-safe form. The API key generator makes them
  • Signed license files. A license that an app verifies offline is signed data, and the signature is stored as Base64
  • Email attachments and JSON APIs, whenever binary data has to travel inside text

The other common way to write bytes as text is hexadecimal, which the hex to text converter handles.

Common questions about Base64

What is Base64 used for?

Carrying binary data, such as images, files, keys and signatures, through systems that only handle text: email, JSON, HTML, URLs and configuration files.

Is Base64 encryption?

No. It's an encoding that anyone can reverse without a key. It provides no secrecy at all.

Why does Base64 end with an equals sign?

The = is padding. Base64 encodes three bytes into four characters, and when the data's length isn't a multiple of three, one or two = signs fill out the final group.

How much bigger does Base64 make a file?

About 33%. Every three bytes become four characters, so a 3 MB file is about 4 MB of Base64.

How do I decode Base64?

Paste it into the right-hand box above. On the command line, base64 -d does it on macOS and Linux.

How do I convert an image to Base64?

Drop the image on the box above. Tick "Files as a data URI" if you want the data:image/...;base64, prefix for use in HTML or CSS.

What is the difference between Base64 and Base64URL?

Base64URL replaces + and / with - and _ and usually omits the = padding, so the result can sit in a URL or a file name without escaping.

Why does my decoded text look like nonsense?

Either the data isn't text, in which case use the download link to save it as a file, or the Base64 was cut short when it was copied. Check that you have all of it.

Is my data uploaded when I use this?

No. The encoding and decoding are done by JavaScript in this page, on your computer.