← All guides

Base64 Explained: What It Is, Why It Exists, and When to Use It

Base64 shows up all over the place, even if you never knew its name. You have seen it as a long block of letters, digits, plus signs, and slashes, often ending in one or two equals signs. It appears in email attachments, data URLs, API tokens, and JSON Web Tokens. Base64 is not a code, and it is not a cipher. It is a way to write any bytes using only 64 safe characters, so binary data can pass through systems built for text. This guide walks through how it works, with the actual math done step by step.

Why Base64 exists

Many systems that move data were built for plain text. Email is the clearest example. The early mail rules assumed messages were made of readable characters. Some servers along the way would break, delete, or change bytes that didn't fit that safe set. A photo, a program, or any file is just bytes, and many of those bytes don't fit the safe set. Sending a file raw through a text-only channel was like mailing a glass object in a paper envelope.

Base64 solves this by translation. It takes the bytes of anything and rewrites them using an alphabet of 64 characters that every text system handles safely: the uppercase letters A to Z, the lowercase letters a to z, the digits 0 to 9, and the two symbols + and /. Any data, once encoded, becomes a plain string that can ride inside an email, a URL, a JSON document, or a config file without breaking the channel it travels through. The receiver decodes it and gets the exact original bytes back.

How the encoding works

The encoder works in small groups. It takes the input three bytes at a time. Three bytes are 24 bits. It splits those 24 bits into four groups of six bits. Each six-bit group is a number from 0 to 63, and each number picks one character from the 64-character alphabet. So every three bytes of input become exactly four characters of output. That is the whole mechanism. Everything else about Base64 follows from it.

Watch it happen on the word Man. The three letters are three bytes, so they fill one complete group and come out as four characters: TWFu. Shorter leftovers need help. The letter M alone is one byte, which is not enough bits for four characters, so the encoder fills what it can and marks the gap with padding, the equals signs at the end: M becomes TQ==. Two bytes, like Ma, need one pad: TWE=. The padding is not decoration. It tells the decoder exactly how many bytes the last group really held, so the original comes back byte for byte.

Why the output grows by a third

Four characters out for every three bytes in means the encoded form is always about a third bigger than the original. The math is exact: a file of 102,400 bytes, which is 100 KiB, encodes to 136,536 characters, about 133.3 KiB. That 33.3 percent overhead is the price of safety. It is why Base64 is fine for a small image inside a web page or an attachment in an email, and a poor choice for shipping very large files when a direct binary transfer is available.

The overhead also explains a common sight: an email with a 1 MB attachment can be about 1.37 MB on the wire. Nothing went wrong. The attachment was encoded for travel, and the encoding added its third. When you see a data URL in a stylesheet or a long token in a header, the same rule produced the length you are looking at.

Base64URL and the JWT connection

Two characters in the standard alphabet cause trouble in web addresses: + and / both mean something in a URL, and the padding character = has a meaning too. The URL-safe variant, called Base64URL, swaps + for - and / for _, and usually drops the padding. Same encoding, friendlier alphabet. You meet it most often inside JSON Web Tokens, where all three parts of the token are Base64URL text.

Here is a real example. The string eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 is the header of a typical token. Decoded, it is a small piece of JSON: {"alg":"HS256","typ":"JWT"}. Anyone holding the token can do that decoding with the tools on this site, in either direction, with no key. That is by design. A JWT is signed, not sealed. The signature proves the token was not changed. The Base64 protects nothing, and it is not meant to.

What Base64 is not

Base64 is not encryption. It has no key, no secret, and no work factor. Anyone who spots the format can decode it right away, including every tool on this page. Encoding a password in Base64 protects it about as much as writing it in all caps. If data has to stay secret, it needs real encryption. An encoded encrypted file is fine to send, but the encoding is just the envelope, not the lock.

It is not compression either. It makes data bigger, never smaller, by the third we counted above. And it is not a hash: a hash is a one-way fingerprint of a fixed length, while Base64 is a two-way translation that returns the original whole. Keep the three ideas separate and Base64 becomes what it should be, a simple, reliable way to carry bytes through text-only country.

Try it yourself

Try it with your own text. The free Base64 Encoder and Decoder on CodeToolWorks converts text both ways and handles the URL-safe alphabet. The Image to Base64 Converter turns a picture file into its string form, and the JWT Decoder shows the header and payload inside any token.