CALCULATORCASTLE

Base64 Encode / Decode

Encode text or data to Base64 format and decode Base64 strings back to plain text.

About

Base64 Encode / Decode

These tools Base64-encode and decode text, images and files. Everything happens inside your browser, so nothing you paste or upload is sent across the internet. Paste text into the first tool, or hand a file to the second and get its Base64 back, ready to copy or download.

What Base64 is

Base64 encoding is a way of converting binary data to and from text. Some computer systems, especially older ones, and a number of transmission and storage formats handle text far more reliably than raw binary. Base64 represents binary information such as images, files or arbitrary data using a set of 64 printable ASCII characters, which makes it safe to move through those systems.

The character set is the uppercase letters A to Z, the lowercase letters a to z, the digits 0 to 9, and two more characters, normally + and /. The = symbol is used as padding at the end of the output so the encoded string comes out the right length. The table below shows the characters and their numeric values under RFC 4648.

0: A8: I16: Q24: Y32: g40: o48: w56: 4
1: B9: J17: R25: Z33: h41: p49: x57: 5
2: C10: K18: S26: a34: i42: q50: y58: 6
3: D11: L19: T27: b35: j43: r51: z59: 7
4: E12: M20: U28: c36: k44: s52: 060: 8
5: F13: N21: V29: d37: l45: t53: 161: 9
6: G14: O22: W30: e38: m46: u54: 262: +
7: H15: P23: X31: f39: n47: v55: 363: /

Why the output is bigger

Modern computers are byte-based, at 8 bits per byte, while Base64 represents data in 6-bit values mapped onto those 64 characters. Three bytes of input carry 24 bits, which divides evenly into four 6-bit blocks, so three bytes become four characters. Base64 output is therefore about 4/3, or roughly 1.33 times, the size of the original: a 33 percent increase before anything else is added.

In real terms, one kibibyte of data becomes 1,368 characters, and a one megabyte image becomes about 1.33 MB of text. If the encoder also wraps lines at 76 characters, which the MIME standard for email requires, the line breaks push the overhead to about 37 percent.

Worked examples

The text 'Dog' encodes as 'RG9n'. Its three ASCII characters carry 3 ร— 8 = 24 bits, which split cleanly into four 6-bit blocks.

Input letterDog
8-bit decimal value68111103
Bits010001000110111101100111
6-bit decimal value17   6   61   39
Encoded Base64 letterR   G   9   n

Most of the time the input does not divide by three, leaving 2 or 4 bits over at the end. One or two '=' characters are then added to stand in for the missing group. 'Do' encodes as 'RG8=':

Input letterDopadding
8-bit decimal value68111
Bits010001000110111100
6-bit decimal value17   6   60
Encoded Base64 letterR   G   8   =

And 'Dogs' encodes as 'RG9ncw==':

Input letterDogspadding
8-bit decimal value68111103115
Bits010001000110111101100111011100110000
6-bit decimal value17   6   61   39   28   48
Encoded Base64 letterR   G   9   n   c   w   ==

The rule is short: input length divisible by three needs no padding, two leftover bytes need one '=', and one leftover byte needs two. That is also why a valid Base64 string can never be one character longer than a multiple of four, and why the decoder here rejects a length that leaves a single leftover character.

Where Base64 is used

E-mail attachments. SMTP was originally designed to carry 7-bit ASCII only. Encoding binary data as Base64 is what lets an older SMTP server move a photograph or a PDF intact, and it is still what every MIME attachment does today.

Embedding binary data in text files. Text formats dominate data transfer, and Base64 lets binary ride inside them. Binary can be Base64-encoded and dropped into JSON or XML, and images or PDFs can be embedded directly in HTML or CSS as a data URI, which is a string of the form data:image/png;base64,โ€ฆ used in place of a file path. Encode an image with the second tool above and it hands you exactly that string.

HTTP headers and tokens. HTTP Basic authentication sends the username and password as Base64. JSON Web Tokens are three Base64 sections joined by dots. Both rely on the encoding to keep bytes safe through headers that would otherwise mangle them.

Base64 is not encryption

This is the misunderstanding worth spelling out. Base64 provides no secrecy whatsoever. It is a reversible mapping with no key, and anybody can decode it in one step, including with the tool on this page. A password sent as Base64 in an HTTP Basic header is transmitted in the clear as far as security is concerned, which is why Basic auth is only acceptable over HTTPS.

Encoding solves a transport problem, not a confidentiality one. If the data needs to stay private, encrypt it first and then Base64 the ciphertext if the channel demands text.

The URL-safe variant, and other alphabets

The standard alphabet ends in + and /, both of which have meaning inside a URL, and = has meaning in a query string. RFC 4648 therefore defines a URL-safe variant that swaps + for - and / for _, usually with the padding dropped entirely. JSON Web Tokens use it, as do most URL parameters carrying encoded data. The decoder on this page accepts both alphabets and ignores whitespace, since that is what people actually paste out of emails, JSON files and terminal output.

Base64 is not the only such scheme. Base32 uses a smaller alphabet that survives case-insensitive systems, at a cost of 1.60 times the original size. Ascii85 packs four bytes into five characters for 1.25 times, and appears inside PDF and PostScript. Base58 drops the characters people confuse when reading aloud, which is why Bitcoin addresses use it.

Character sets, and where decoding goes wrong

Base64 encodes bytes, not characters, so the text has to become bytes first and the character set decides how. In UTF-8 an accented letter such as รฉ is two bytes and an emoji is four, so the encoded output is longer than the visible character count suggests. Choosing ASCII or ISO-8859-1 restricts you to one byte per character, and this tool says so rather than silently substituting a question mark when a character has no byte in that set.

The same choice matters on the way back. Decoding produces bytes, and turning those bytes into readable text needs the same character set they were encoded with. Text that comes back as mojibake usually means the byte stream is fine and the character set is wrong.

Reading the results on this page

The panel reports how many characters or bytes came out, what went in, and the size change between them, along with the number of padding characters and how many four-character blocks the result contains. The output box below it holds the full result, with copy and download buttons. For files, encoding shows the Base64 text and, for an image, a preview and the data URI you can paste straight into HTML. Decoding a file downloads the reconstructed binary directly.

Common questions

Frequently asked questions

A way of representing binary data as text, using 64 printable ASCII characters: A to Z, a to z, 0 to 9, plus + and /. It exists so binary can travel through systems that only handle text reliably, such as email or a JSON field.

Because it packs 8-bit bytes into 6-bit values. Three bytes become four characters, so the output is 4/3 the size, about 33 percent larger. A kibibyte becomes 1,368 characters, and line wrapping for email pushes the overhead to roughly 37 percent.

Padding. Base64 works in groups of three bytes, and when the input does not divide by three the final group is short. Two leftover bytes get one =, and one leftover byte gets two, so the output length stays a multiple of four.

No, and treating it as such is a real mistake. There is no key and it is reversible by anyone in one step. It hides nothing. Encrypt data if it needs to be private, then Base64 the result only if the channel requires text.

The URL-safe variant replaces + with - and / with _, and usually drops the = padding, because those three characters have their own meaning inside a URL. JSON Web Tokens use it. This decoder accepts either form.

Yes. Use the file tool above and it returns the Base64 text plus a ready-made data URI in the form data:image/png;base64,โ€ฆ which can be pasted into an img tag or a CSS background to embed the image with no separate request.

Almost always a character set mismatch rather than a bad Base64 string. The bytes decode fine, but they are being read as the wrong encoding. Try UTF-8 first, since it is what most modern text uses.

No. The encoding and decoding both run in your browser using local JavaScript, and no text or file is sent to a server. That matters for the tokens, keys and private documents people routinely paste into tools like this one.