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

EncodingBase64

Encoding Problem Overview


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?

Encoding Solutions


Solution 1 - Encoding

Q Does a base64 string always end with =?

A: No. (the word usb is base64 encoded into dXNi)

Q Why does an = get appended at the end?

A: As a short answer:
The last character (= sign) is added only as a complement (padding) in the final process of encoding a message with a special number of characters.

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

Example:

(a) If you want to encode

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

Base64 deals 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).

[ABC] => QUJD

[DEF] => REVG

[G] => Rw==

(b) If you want to encode ABCDEFGH <=> [ABC] [DEF] [GH]

similarly, it will add one = at the end of the output to get 4 characters.

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

[ABC] => QUJD

[DEF] => REVG

[GH] => R0g=

Solution 2 - Encoding

It serves as padding.

A more complete answer is that a base64 encoded string doesn't always end with a =, it will only end with one or two = if they are required to pad the string out to the proper length.

Solution 3 - Encoding

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.

Solution 4 - Encoding

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

Solution 5 - Encoding

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

Solution 6 - Encoding

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

Solution 7 - Encoding

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.

Solution 8 - Encoding

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.

Solution 9 - Encoding

= 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/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsantosh singhView Question on Stackoverflow
Solution 1 - EncodingBadr BellajView Answer on Stackoverflow
Solution 2 - EncodingAndrew HareView Answer on Stackoverflow
Solution 3 - EncodingLegolasView Answer on Stackoverflow
Solution 4 - EncodingiandotkellyView Answer on Stackoverflow
Solution 5 - EncodingIan KempView Answer on Stackoverflow
Solution 6 - EncodingSam HollowayView Answer on Stackoverflow
Solution 7 - EncodingThomas LeonardView Answer on Stackoverflow
Solution 8 - EncodingDevView Answer on Stackoverflow
Solution 9 - EncodingVladimir IgnatyevView Answer on Stackoverflow