Problem converting byte() to string and then back to byte()

M

moondaddy

I need to convert a byte array to a string and pass it as a parameter in a
URL and then convert it back to the original byte array. However, its
getting scrambled in the conversion. In short, here's the code:

======================================
Dim textConverter As New ASCIIEncoding
Dim sParam As String = "This is my cool param"
Dim bytParam() As Byte

'load the byte array here...

'convert byte() to string
sParam = textConverter.GetString(bytParam)
'convert string to byte()
bytParam = textConverter.GetBytes(sParam)
=====================================

bytParam is not the same as it was before it was converted to a string.
How can I do this and preserve the byte() to exactly what it was when I
started?

Here's bytParams before converting to a string: (copied from the watch
window)
bytParam {Length=32} Byte()
0 27 Byte
-1 163 Byte
-2 198 Byte
-3 42 Byte
-4 46 Byte
-5 191 Byte
-6 236 Byte
-7 250 Byte
-8 163 Byte
-9 211 Byte
-10 222 Byte
-11 98 Byte
-12 31 Byte
-13 86 Byte
-14 223 Byte
-15 64 Byte
-16 164 Byte
-17 197 Byte
-18 1 Byte
-19 180 Byte
-20 102 Byte
-21 129 Byte
-22 246 Byte
-23 148 Byte
-24 204 Byte
-25 35 Byte
-26 8 Byte
-27 15 Byte
-28 36 Byte
-29 31 Byte
-30 1 Byte
-31 253 Byte


and here it is after restoring it back from a string:
bytParam {Length=32} Byte()
0 27 Byte
-1 35 Byte
-2 70 Byte
-3 42 Byte
-4 46 Byte
-5 63 Byte
-6 108 Byte
-7 122 Byte
-8 35 Byte
-9 83 Byte
-10 94 Byte
-11 98 Byte
-12 31 Byte
-13 86 Byte
-14 95 Byte
-15 64 Byte
-16 36 Byte
-17 69 Byte
-18 1 Byte
-19 52 Byte
-20 102 Byte
-21 1 Byte
-22 118 Byte
-23 20 Byte
-24 76 Byte
-25 35 Byte
-26 8 Byte
-27 15 Byte
-28 36 Byte
-29 31 Byte
-30 1 Byte
-31 125 Byte


Thanks!
 
M

Mattias Sjögren

I need to convert a byte array to a string and pass it as a parameter in a
URL and then convert it back to the original byte array.

Consider formatting each byte as two hex characters or base64 encode
with the Convert class.



Mattias
 
P

Peter Huang [MSFT]

Hi

I agree with Mattias's suggestion. You may have a try.
Also what is the process of your application.
I assume your client is a winform application, after convert the byte array
as an string, it will be passed as a query
string(URL,http://host/test.aspx?id=querystring), or it will be data in a
form POST?( I assume the server side is an ASPX page)

Which method do you use to pass the data, via webbrowser or httpwebrequest?
Also you may try to use the HttpUtility class which use the UrlEncode and
UrlDecode.

If you still have any concern, please post the information above, so that
we can figure out your scenario correctly.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

moondaddy

Thanks. I'm looking into your suggestion but need some further help. To
clarify:

I have vb.net 1.1 winforms app and I want to call reports from sql server
reporting services. The problem is that this is confidential data and SSRS
has a number of security issues which I wont discuss here. but in short, I
will assigning the required credentials to the report on the server side
where things are secure. Now all I need to do is encrypt the parameters
being passed to the server. I've tried a number of different ways of
fetching reports including web services, but have been running into major
issues on each one. The most reliable way of getting the report is by
calling a URL that will return a web page with the SSRS report in it.

So here's what I'm doing:

I use one generic page for all reports and pass a dataset as a parameter
which as information such as which report to return and any number of
parameters needed by the report. the steps are:

1) Build the dataset in the smart client
2) convert the dataset to a string like this: Dim str as string = ds.getxml
3) encrypt the string to a byte array: Dim byt() as byte =
MyEncryptMethod(str)
4) convert byt to a string so I can pass it as a querystring in the URL: Dim
sParam as string = SomeMethodToConvertByteToString(byt)
5) Call the url:
System.Diagnostics.Process.Start("http://localhost/wsVIPN/Reports/VIPNReportStandard.aspx?Param="
& sParam)

On the server side in the web page load event

6) Receive the querystring: Dim sParam As String =
Request.QueryString("Param")
7) Convert it back to a byte() so I can decrypt it
8) Decrypt it: Dim str as string = MyDecryptMethod(byt)
9) Convert the string back to the dataset: ds.ReadXml(str )
10) Now that my params are back in the dataset I can process the report.

I could really use some help and specific code examples on these
conversions. I tried your idea to use UrlEncode, however, I was doing it in
the smart client and could not find the right namespace to get it to
compile.

I hope you can follow this.

Thanks again!
 
S

Stephany Young

Try this:

Dim bytParam1() As Byte = New Byte() {27, 163, 198, 42, 46, 191, 236, 250,
163, 211, 222, 98, 31, 86, 223, 64, 164, 197, 1, 180, 102, 129, 246, 148,
204, 35, 8, 15, 36, 31, 1, 253}

Dim sParam As String = System.Text.Encoding.Default.GetString(bytParam1)

Dim bytParam2() As Byte = System.Text.Encoding.Default.GetBytes(sParam)

Console.WriteLine("bytParam1.Length = {0}", bytParam1.Length)

Console.WriteLine("bytParam2.Length = {0}", bytParam2.Length)

Console.WriteLine()

Console.WriteLine("Position bytParam1 bytParam2")

Console.WriteLine("-------- --------- ---------")

For _i As Integer = 0 To bytParam1.Length - 1
Console.WriteLine("{0,2} {1,3} {2,3}", _i, bytParam1(_i),
bytParam2(_i))
Next

What you were doing was using ASCII encoding instead of ANSI encoding. Not,
in your results, that each original value that was greater than 127 appeared
to have had 128 subtracted from it. The ASCII character set runs from 0 to
127 whereas the ANSI character set runs from 0 to 256.

If you need to use a DBCS then you need to use the appropriate one of the
other encoding formats.
 
M

Mattias Sjögren

Dim sParam As String = System.Text.Encoding.Default.GetString(bytParam1)

That's inappropriate for data that is being sent from a client to a
server, because the default ANSI codepage can be different on
different machines, so there's no guarantee that this will roundtrip
properly. You will also get problems with non printable characters.



Mattias
 
S

Stephany Young

I think it's best to leave that point up to the original poster.

The point I was making was that it was his use of ASCII encoding that was
causing his problem.
 
M

moondaddy

Thanks to all. Both of you are brining things to light that I was unaware
of and will also prevent the solution from working correctly (which is what
it's all about). So I need a different way to skin-the-cat. I haven't
gotten into serializing things yet, but this might be the time. Since all
my parameters are wrapped into a dataset. Do you think it would work to
have a simple class that just contained an encrypted copy of this dataset?
then serialize the class into a string and pass it in the URL. And then
reverse engineer everything on the server? I'll start working on this, but
would like some feedback if possible.

Thanks again!

--
(e-mail address removed)
Stephany Young said:
I think it's best to leave that point up to the original poster.

The point I was making was that it was his use of ASCII encoding that was
causing his problem.
 
P

Peter Huang [MSFT]

Hi


We need to add reference to System.Web.dll to use the HttpUtility class.
Here is my test about how to pass byte array from winform to asp.net
[Winform side]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim bts(127) As Byte
For i As Integer = 0 To 127
bts(i) = i
Next
Dim str As String = HttpUtility.UrlEncode(bts)
Process.Start("http://localhost/WebSideServer/Default.aspx?name=" &
str)
End Sub

[Asp.net side]
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.Write("Start dump Byte
Array=============================================<BR>")
For Each b As Byte In
HttpUtility.UrlDecodeToBytes(Request.QueryString("name"))
Response.Write(b.ToString() & "<BR>")
Next
Response.Write("End dump Byte
Array=============================================")
End Sub

Also use QueryString approach has its limitatio you may pay attention.
What is the limit on QueryString / GET / URL parameters?
http://www.aspfaq.com/show.asp?id=2222

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top