-1

While tracing a bug I have found the following discrepancy between HttpServerUtility.UrlTokenEncode and Convert.ToBase64String in conversion to Base64 and back:

string test = "IN ('en-US')";
Console.WriteLine(HttpServerUtility.UrlTokenEncode(Encoding.UTF8.GetBytes(test)));
Console.WriteLine(Convert.ToBase64String(Encoding.UTF8.GetBytes(test)));

The result are slightly different: the first method has an additional trailing zero:

SU4gKCdlbi1VUycp0
SU4gKCdlbi1VUycp

JavaScript btoa() also gives me the value without trailing zero.

I understand that this zero is just for padding here, but is the second conversion still base64-compliant? Or should we refrain from using HttpServerUtility.UrlTokenEncode in favor of Convert.ToBase64String everywhere?

Alexander Galkin
  • 12,086
  • 12
  • 63
  • 115

1 Answers1

3

I think this SO post has more information about this issue, it seems like the number at the end indicates the number of = signs that were removed: Encode URL from C#, decrypt URL in PHP (extra characters being added somehow)

From the looks of it the UrlTokenEncode is especially to encode the tokens within the URL. A token should not contain characters which have a meaning within the URL, like =, / and +.

Here you can see the code that is used for the UrlTokenEncode: http://referencesource.microsoft.com/System.Web/R/0e3cb83cf51ca334.html

Community
  • 1
  • 1
Herman
  • 2,738
  • 19
  • 32