Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does conversion from two different base64 strings return equal arrays of bytes?

Tags:

c#

.net

base64

I am wondering why conversion from base64 strings returns equal byte arrays for different strings?

const string s1 = "dg==";
const string s2 = "dq==";

byte[] a1 = Convert.FromBase64String(s1);
byte[] a2 = Convert.FromBase64String(s2);

Console.WriteLine(a1.SequenceEqual(a2)); // prints "True".
like image 951
Qué Padre Avatar asked Dec 15 '22 06:12

Qué Padre


1 Answers

Because of the encoding rules. When the last four-character group contains two padding characters (as is the case here) it decodes to a single byte. This means that decoding will take into account all 6 bits encoded into the first character, plus 2 of the bits encoded into the second.

In the base64 alphabet g corresponds to decimal 32 and q to decimal 42. When converted to 6-bit binary both of these values have their 2 most significant bits set to 10:

Base64   Decimal   Binary
   g       32      100000
   h       33      100001
          ....
   q       42      101010
                   ^^

Since only these two bits go into the decoded output, it follows that the output will also be identical (by the same token, any decimal in the range [32, 47] => any base64 digit in the range [d,v] will produce the same result when substituted in the second position). It all works like this:

Printable         d        g         =     =
Bits              011101   10xxxx    -     -
                  ^^^^^^   ^^
                    |       \-------------------------------\
Interpretation      \-----------------------------------\   |
                                                        |   |
                                                        v   v
Result (binary)                                       01110110
Result (ASCII)                                     the letter "v"
like image 156
Jon Avatar answered Dec 21 '22 22:12

Jon