8

In Page1.aspx, I have

byte[] byt = System.Text.Encoding.UTF8.GetBytes(TextBox1.Text);
Response.Redirect("Page2.aspx?BytArray=" + byt,false);

The value of TextBox1 is "mnop".

Now in Page2.aspx I have the below code

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {           
        var byteArray = System.Text.Encoding.UTF8.GetBytes(Request.QueryString["BytArray"]);

        var x1 = System.Convert.ToBase64String(byteArray, 0, byteArray.Length);

        var x2 = Encoding.UTF8.GetString(byteArray);
    }
}
  • x1 output is U3lzdGVtLkJ5dGVbXQ==

  • x2 output is System.Byte[]

But how to get "mnop" back ? What I am missing?

Even C#: How can I safely convert a byte array into a string and back? gave the answer as U3lzdGVtLkJ5dGVbXQ==

Thanks.

Community
  • 1
  • 1

3 Answers3

10

You cannot send raw bytes as query string. Try Base64 encoding it instead:

byte[] byt = System.Text.Encoding.UTF8.GetBytes(TextBox1.Text);
string encoded = HttpUtility.UrlEncode(Convert.ToBase64String(byt));
Response.Redirect("Page2.aspx?BytArray=" + encoded, false);

and then retrieve it back:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {           
        byte[] byteArray = Convert.FromBase64String(Request.QueryString["BytArray"]);
        string value = System.Text.Encoding.UTF8.GetString(byteArray);
    }
}

But I really don't see the point of the whole exercise of converting to byte arrays when you can directly send the string value of the text box as is (after url encoding it of course). If this is some form of a way to hide the real value from the user I hope you are well aware that Base64 is not encryption, it's encoding.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

The original request wasn't converting the byt to a string - it was just using it as bytes. So this line:

Response.Redirect("Page2.aspx?BytArray=" + byt,false);

Was actually going to this URL:

Page2.aspx?BytArray=System.Byte[]

You need to change that line to:

Response.Redirect("Page2.aspx?BytArray=" + HttpUtility.UrlEncode(System.Convert.ToBase64String(byt)), false);

And then on the way back replace all this:

var byteArray = System.Text.Encoding.UTF8.GetBytes(Request.QueryString["BytArray"]);

var x1 = System.Convert.ToBase64String(byteArray, 0, byteArray.Length);

var x2 = Encoding.UTF8.GetString(byteArray);

With just this:

var byteArray = Convert.FromBase64String(Request.QueryString["BytArray"]);

var x2 = Encoding.UTF8.GetString(byteArray);
Writwick
  • 2,133
  • 6
  • 23
  • 54
yamen
  • 15,390
  • 3
  • 42
  • 52
0

You can't. The code in the first page doesn't sent the value of the byte array, but the data type. Request.QueryString["BytArray"] returns System.Byte[], so it's impossible to get the content of the original byte arrray back.

You can't send bytes as data in the URL without further encoding them. You can for example use base64:

byte[] byt = System.Text.Encoding.UTF8.GetBytes(TextBox1.Text);
Response.Redirect("Page2.aspx?BytArray=" + Server.UrlEncode(Convert.ToBase64String(byt)), false);

Now the URL contains the actual bytes, so it's possible to get them:

byte[] byteArray = Convert.FromBase64String(Request.QueryString["BytArray"]);
string x = System.Text.Encoding.UTF8.GetString(byteArray);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005