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

santosh singh picture santosh singh · Aug 2, 2011 · Viewed 177.9k times · Source

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?

Answer

Badr Bellaj picture Badr Bellaj · Apr 12, 2016

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 (8bits) 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(producing 4 characters) with the first block 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 space)

(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 space)