4

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".
2
  • 1
    Comprehensive answer. Commented Sep 17, 2013 at 12:56
  • @SonerGönül, this looks like an interesting question, Not sure why it should print True Commented Sep 17, 2013 at 13:01

1 Answer 1

12

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.