1

I have to encode some HTML source code into base64 format before form submission, and then decode it back to original code in the code behind. Here is the testing code by MsgBox:

MsgBox(HttpContext.Current.Request.Form("encodedSourceCode"))
MsgBox(Convert.ToString(HttpContext.Current.Request.Form("encodedSourceCode").GetType()))
Dim b = Convert.FromBase64String(HttpContext.Current.Request.Form("encodedSourceCode"))
Dim html = System.Text.Encoding.UTF8.GetString(b)
MsgBox(html)

And I have added an alert() for encodedSourceCode in client script.

The results turn out to be:

First MsgBox: Empty

Second MsgBox: "System.String"

Last MsgBox: Original HTML source code

And the JS alert dialog shows the base64 string, which consists of a bunch of digits and alphabets.

In short, everything is fine, except the first MsgBox, which is supposed to be base64 encoded string but turns out to be empty. Why? Is it normal?

Actually it does not matter much because even the final result (after decoding) seems to have no problem, but I'm just curious why the interim result is not shown as what it's supposed to be.

Ursidae
  • 87
  • 9

2 Answers2

2

It seems that the string is simply too long without 'wrappable' characters, I suppose. MsgBox cuts out the 'last word' and shows nothing.
This may confirm it:

dim test = HttpContext.Current.Request.Form("encodedSourceCode")
MsgBox(test) ' empty
test = test.Substring(0, 20)
MsgBox(test) ' shows the first 20 characters

Testing in LinqPad, I get the limit around 43.000 characters:

MsgBox("".PadLeft(43000, "a"))
MsgBox("".PadLeft(44000, "a"))
MsgBox("".PadLeft(43000, "a") & " " & "".PadLeft(1000, "a"))

1st: shows text.
2nd: shows empty box, length = 44.000
3rd: shows text, although the total length is 44.001, but wrappable at the space.

KekuSemau
  • 6,830
  • 4
  • 24
  • 34
  • The original source code in the last `MsgBox` is very long too, but many lines with indentation can be shown inside the box. – Ursidae May 28 '19 at 09:04
  • See my edit. As I said, the base64 string has no _non-word_ characters, this seems to be the main point. – KekuSemau May 28 '19 at 09:24
0

It definitely has nothing to do with base64 strings as they are simple strings. Here the proof:

    Dim myString = "Hello world, this is just an ɇxâmpŀƏ ʬith some non-ansi characters..."
    Dim myEncoding As Encoding = Encoding.UTF8
    MsgBox(myString)
    Dim myBase64 = Convert.ToBase64String(myEncoding.GetBytes(myString))
    MsgBox(myBase64)
    Dim myStringAgain = myEncoding.GetString(Convert.FromBase64String(myBase64))
    MsgBox(myStringAgain)
    MsgBox(If(StringComparer.Ordinal.Equals(myString, myStringAgain), "same", "different"))

The line

MsgBox(Convert.ToString(HttpContext.Current.Request.Form("encodedSourceCode").GetType()))

results in "System.String" because you convert the name of the type to a string (see xxx.GetType()).

Christoph
  • 3,322
  • 2
  • 19
  • 28
  • This is not the focus. I tested and mentioned it just to show that `HttpContext.Current.Request.Form("encodedSourceCode")` is a simple string that should be able to be displayed in `MsgBox`. The main thing is why `HttpContext.Current.Request.Form("encodedSourceCode")` is empty in `MsgBox` when it can return the expected result after decoding. – Ursidae May 28 '19 at 08:33
  • Some hidden difference in the key "encodedSourceCode"? It definitely should display the string. – Christoph May 28 '19 at 08:43
  • The five lines I quoted above are all consecutive in my code. Nothing in between. And all results directly get data from HTTP Request, so they should be the same. – Ursidae May 28 '19 at 09:14