[encoding] Why does a base64 encoded string have an = sign at the end

I know what base64 encoding is and how to calculate base64 encoding in C#, however I have seen several times that when I convert a string into base64, there is an = at the end.

A few questions came up:

  1. Does a base64 string always end with =?
  2. Why does an = get appended at the end?

This question is related to encoding base64

The answer is


The equals sign (=) is used as padding in certain forms of base64 encoding. The Wikipedia article on base64 has all the details.


1: No.

2: As a short answer: The 65th character ("=" sign) is used only as a complement in the final process of encoding a message.

You will not have a '=' sign if your string has a multiple of 3 characters number, because Base64 encoding takes each three bytes (8 bits) and represents them as four printable characters in the ASCII standard.

Details:

(a) If you want to encode

ABCDEFG <=> [ABC] [DEF] [G

Base64 will deal with the first block (producing 4 characters) and the second (as they are complete). But for the third it will add a double == in the output in order to complete the 4 needed characters. Thus, the result will be QUJD REVG Rw== (without spaces).

(b) If you want to encode

ABCDEFGH <=> [ABC] [DEF] [GH

similarly, it will add just a single = in the end of the output to get 4 characters.

The result will be QUJD REVG R0g= (without spaces).


It's padding. From http://en.wikipedia.org/wiki/Base64:

In theory, the padding character is not needed for decoding, since the number of missing bytes can be calculated from the number of Base64 digits. In some implementations, the padding character is mandatory, while for others it is not used. One case in which padding characters are required is concatenating multiple Base64 encoded files.


http://www.hcidata.info/base64.htm

Encoding "Mary had" to Base 64

In this example we are using a simple text string ("Mary had") but the principle holds no matter what the data is (e.g. graphics file). To convert each 24 bits of input data to 32 bits of output, Base 64 encoding splits the 24 bits into 4 chunks of 6 bits. The first problem we notice is that "Mary had" is not a multiple of 3 bytes - it is 8 bytes long. Because of this, the last group of bits is only 4 bits long. To remedy this we add two extra bits of '0' and remember this fact by putting a '=' at the end. If the text string to be converted to Base 64 was 7 bytes long, the last group would have had 2 bits. In this case we would have added four extra bits of '0' and remember this fact by putting '==' at the end.


From Wikipedia:

The final '==' sequence indicates that the last group contained only one byte, and '=' indicates that it contained two bytes.

Thus, this is some sort of padding.


= is a padding character. If the input stream has length that is not a multiple of 3, the padding character will be added. This is required by decoder: if no padding present, the last byte would have an incorrect number of zero bits.

Better and deeper explanation here: https://base64tool.com/detect-whether-provided-string-is-base64-or-not/


Its defined in RFC 2045 as a special padding character if fewer than 24 bits are available at the end of the encoded data.


  1. No.
  2. To pad the Base64-encoded string to a multiple of 4 characters in length, so that it can be decoded correctly.