What Is Base64 Encoding and When Should You Use It?

How Base64 works, why it makes data about a third larger, where it is used in practice, and why it is not a form of encryption.

By Utkarsh Garg · 2026-10-27 · 5 min read

What Base64 does

Base64 turns any sequence of bytes into plain text made of 64 safe characters: the letters A to Z and a to z, the digits 0 to 9, plus and slash. It exists because many systems that were built for text, such as email, URLs, JSON and HTML attributes, cannot carry raw binary data without corrupting it.

The method takes three bytes at a time, which is 24 bits, and splits them into four groups of six bits. Each group becomes one character. When the input length is not a multiple of three, the output is padded with one or two equals signs at the end.

The cost: about 33 percent more data

Because every three bytes become four characters, Base64 output is roughly one third larger than the input. A 30 KB image becomes about 40 KB of text. That overhead is the reason inlining large files as Base64 is usually a poor trade for web pages, even though it saves a request.

Where you will meet it

Base64 shows up in more places than most people expect.

  • Email attachments, which use it inside the MIME format.
  • Data URIs, such as small images embedded directly in CSS or HTML.
  • The two parts of a JSON Web Token, which use a URL safe variant.
  • HTTP Basic authentication, where the username and password are joined with a colon and encoded.
  • Storing or sending small binary values, such as keys or hashes, in JSON or configuration files.

It is not encryption

Base64 offers no secrecy at all. Anyone can decode it instantly without a key, so encoding a password or token with Base64 does not protect it. The Basic authentication header is a good example: it is only acceptable over HTTPS because the encoding itself hides nothing.

If data must be private, encrypt it first and then Base64 encode the encrypted bytes if you need a text form.

URL safe Base64 and text encoding

The standard alphabet uses plus and slash, which have special meaning in URLs. The URL safe variant replaces them with minus and underscore and usually drops the trailing equals signs. If a decoded value looks like nonsense, check which variant was used.

Text must be turned into bytes before it is encoded, and the character set matters. Non English text and emoji need UTF-8. A common bug is encoding with a function that only understands single byte characters, which fails or produces output that decodes to the wrong characters. A decoder that treats the result as UTF-8 text will show you straight away whether the bytes were valid.

Worked example: encoding three letters by hand

The word Man is a good test case because it is exactly three bytes, so no padding is needed. In ASCII the letters are M = 77, a = 97 and n = 110. In binary that is 01001101, 01100001 and 01101110.

Join the bits into one 24 bit string: 010011010110000101101110. Now split it into four groups of six bits: 010011, 010110, 000101 and 101110. These equal 19, 22, 5 and 46.

Look each number up in the Base64 alphabet, where A is 0, Z is 25, a is 26, z is 51, 0 is 52, 9 is 61, plus is 62 and slash is 63. The values 19, 22, 5 and 46 give T, W, F and u. So Man encodes to TWFu.

Decoding runs the same steps backward: each character becomes six bits, the bits are regrouped into bytes, and the bytes are read as text.

How padding works

When the input is not a multiple of three bytes, the last group is short. The encoder fills the missing bits with zeros and adds equals signs so the output length is still a multiple of four.

Padding with real inputs
InputBytesBase64 outputPadding
Man3TWFuNone
Ma2TWE=One equals sign
M1TQ==Two equals signs
Hello, World!13SGVsbG8sIFdvcmxkIQ==Two equals signs

The padding carries no data. It only tells a decoder how many bytes were in the last group, which is why URL safe Base64 can often drop it and let the decoder work the length out.

Size growth in numbers

The output length is four characters for every three bytes, rounded up. The formula is ceil(n / 3) x 4, where n is the byte count.

Input size in bytesBase64 length in characters
3040
100136
1,0001,336
3,0004,000

Line breaks add more. MIME email wraps Base64 into short lines, so attachments grow slightly beyond the figures above.

Common mistakes and how to spot them

  • Decoded text shows odd characters such as é instead of é. The text was encoded as UTF-8 but decoded as a single byte set, or the other way around. For example, héllo encodes to aMOpbGxv in UTF-8, because é takes two bytes.
  • Decoding fails on a value containing minus or underscore. It is URL safe Base64. Swap minus for plus and underscore for slash, or use a decoder that accepts both. The bytes of ???>>> encode to Pz8/Pj4+ in standard form and Pz8_Pj4- in URL safe form.
  • Decoding fails because of whitespace. Line breaks or spaces added by an email client or a copy and paste are not part of the data. Strip them first.
  • The length is wrong. A valid padded string has a length divisible by four. If it does not, characters were lost in transit or the padding was removed.
  • The decoded result is binary garbage. The data may not be text at all, for example an image or a compressed file. Save it as a file instead of reading it as text.

Frequently asked questions

  • Is Base64 the same as encryption? No. It uses no key and anyone can reverse it, so it protects nothing. YWRtaW46czNjcmV0 decodes straight back to admin:s3cret.
  • Why does Base64 output sometimes end in equals signs? They are padding that fills the last group of four characters. One or two of them appear when the input is not a multiple of three bytes.
  • Why is Base64 about 33 percent larger? Three bytes become four characters, so the data grows by one third before any line breaks are counted.
  • Can I put Base64 in a URL? Use the URL safe variant, or percent encode the plus, slash and equals characters of the standard one. Otherwise they can be misread by the server.
  • Does Base64 compress data? No. It makes data larger. If size matters, compress first, then encode.