What Is Base64 and When Should You Actually Use It?
6 min read · Published October 14, 2026 · Updated October 14, 2026
You paste a long string of letters, digits and a trailing equals sign into a config file, an API response or an email source, and someone says it is Base64. It looks like encryption, so it is easy to assume it hides something. It does not. Base64 is a way of writing arbitrary bytes using only safe, printable characters.
That small idea explains why it shows up everywhere from image data in web pages to login headers. This guide walks through exactly how the encoding works with one example you can check by hand, then covers where it helps, what it costs in size, and where people commonly misuse it.
The problem Base64 solves
Many systems were designed to carry text, not raw bytes. Email bodies, JSON documents, XML files, HTTP headers and URLs all have characters with special meaning or characters that get changed in transit. A raw binary file, such as a PNG image, contains every possible byte value, including control characters and sequences that look like line endings. Dropping those bytes into a text field can corrupt them.
Base64 avoids the trouble by mapping any sequence of bytes onto a set of 64 plain characters: the uppercase letters, the lowercase letters, the digits 0 to 9, plus and slash. Every one of those survives being copied, pasted, embedded in JSON or sent through a text-only channel. The receiver reverses the mapping and recovers the exact original bytes.
How it works, with a worked example
The method is simple arithmetic. Take the input three bytes at a time, which is 24 bits. Split those 24 bits into four groups of 6 bits. Each 6-bit group is a number from 0 to 63, and you look that number up in the Base64 alphabet, where 0 to 25 are A to Z, 26 to 51 are a to z, 52 to 61 are 0 to 9, 62 is plus and 63 is slash.
Take the three letters Man. In ASCII they are the byte values 77, 97 and 110. Written in binary and joined together they form 24 bits, which regroup into four 6-bit numbers: 19, 22, 5 and 46. Looking those up gives T, W, F and u, so Man becomes TWFu.
When the input is not a multiple of three bytes, the encoder pads the final group with zero bits and then adds equals signs so the output length is a multiple of four. One leftover byte produces two characters and two equals signs. Two leftover bytes produce three characters and one equals sign. The padding is only there to mark how many bytes were in the last group.
Text: M a n
ASCII: 77 97 110
Binary: 01001101 01100001 01101110
Regroup: 010011 010110 000101 101110
Numbers: 19 22 5 46
Base64: T W F u
Man -> TWFu
Ma -> TWE=
M -> TQ==
Hello, World! -> SGVsbG8sIFdvcmxkIQ==Where you will meet it
Data URIs are a common example. A small image can be placed straight into HTML or CSS as a string that starts with data:image/png;base64, followed by the encoded bytes. That saves a network request for tiny icons, though it makes the document larger and harder to cache separately.
Email attachments travel as Base64 inside the message source, because the original mail system could only carry text. JSON APIs often carry small files or binary tokens as Base64 strings, since JSON has no binary type. HTTP Basic authentication encodes the username and password pair, joined by a colon, as Base64 in a header. JSON Web Tokens use a URL-safe variant for each of their three parts, and certificates and keys in PEM files are Base64 text between header lines.
- Data URIs for small inline images or fonts.
- MIME email attachments and embedded images.
- Binary fields in JSON, XML or configuration files.
- Authorization headers, tokens and PEM-formatted keys.
The size cost
Every three input bytes become four output characters, so Base64 is about one third larger than the original, with a little extra for padding and, in email, for line breaks. A 300 KB image becomes roughly 400 KB of text. Because text then often goes through compression such as gzip, some of that overhead is recovered on the wire, but not for data that was already compressed, like JPG or PNG.
This is why Base64 suits small payloads and is a poor choice for large files. If an API accepts multipart uploads or a direct file upload, use that instead of wrapping a multi-megabyte file in a JSON string. The decoder also has to hold both the encoded and the decoded copy in memory.
Base64 is not encryption
Anyone can decode Base64 in a second; there is no key and no secret. Treat an encoded value exactly as you would treat the plain value. A Basic authentication header contains the password in a reversible form, which is why it should only be sent over HTTPS. Putting a secret in a JSON field as Base64 hides it from a casual glance and from nothing else.
If you need confidentiality, use real encryption. If you need to detect tampering, use a signature or a message authentication code. Base64 is only a transport wrapper.
The URL-safe variant, and how it differs from percent encoding
The standard alphabet includes plus and slash, which have special meanings in URLs, and the padding character equals appears in query strings. The URL-safe variant replaces plus with a hyphen and slash with an underscore, and often leaves out the padding. The bytes 251, 255 and 191, for example, encode to +/+/ in the standard form and -_-_ in the URL-safe form. Check which variant a system expects before decoding.
Percent encoding, also called URL encoding, is a different job. It leaves ordinary characters alone and replaces only the unsafe ones with a percent sign and two hex digits, so a space becomes %20. It keeps text readable and is used on text that is already mostly URL-safe. Base64 rewrites everything, and is used to carry binary data. They are sometimes combined: a Base64 value placed in a query string may still need percent encoding for its plus, slash and equals characters.
Tools mentioned in this guide
Frequently asked questions
Why does my Base64 string end with equals signs?
They are padding. Base64 output is built in groups of four characters, and if the input does not divide evenly into groups of three bytes, one or two equals signs fill the last group. They carry no data. Some systems, including many URL-safe uses, leave the padding off.
Can Base64 compress my data?
No. It makes data about a third bigger, not smaller. The only reason it can look compact is that it turns binary into plain text you can paste. If size matters, compress the data first with a proper compression format, then encode the result if you must send it as text.
Why does decoding Base64 sometimes give garbled characters?
The bytes decoded correctly, but they are not text in the encoding you expected, or they are binary data such as an image. Text should be decoded as UTF-8 if it was encoded from UTF-8. A wrong alphabet, such as URL-safe against standard, also corrupts the result.
Is it safe to paste Base64 into an online decoder?
Remember that Base64 hides nothing, so whatever is inside is readable by whoever decodes it. Avoid pasting live passwords or tokens into tools that send data to a server. A decoder that runs in your browser, such as ELDOSA's, keeps the text on your device.