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);
}
}
-
x1output is U3lzdGVtLkJ5dGVbXQ== -
x2output 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.
You cannot send raw bytes as query string. Try Base64 encoding it instead:
and then retrieve it back:
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.