473,722 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.G etString(bytPar am)
'convert string to byte()
bytParam = textConverter.G etBytes(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!
--
mo*******@nospa m.nospam
Nov 21 '05 #1
8 4197
>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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
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.

Nov 21 '05 #3
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 = SomeMethodToCon vertByteToStrin g(byt)
5) Call the url:
System.Diagnost ics.Process.Sta rt("http://localhost/wsVIPN/Reports/VIPNReportStand ard.aspx?Param= "
& sParam)

On the server side in the web page load event

6) Receive the querystring: Dim sParam As String =
Request.QuerySt ring("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!

--
mo*******@nospa m.nospam
""Peter Huang" [MSFT]" <v-******@online.m icrosoft.com> wrote in message
news:iA******** *****@TK2MSFTNG XA01.phx.gbl...
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.

Nov 21 '05 #4
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.Enc oding.Default.G etString(bytPar am1)

Dim bytParam2() As Byte = System.Text.Enc oding.Default.G etBytes(sParam)

Console.WriteLi ne("bytParam1.L ength = {0}", bytParam1.Lengt h)

Console.WriteLi ne("bytParam2.L ength = {0}", bytParam2.Lengt h)

Console.WriteLi ne()

Console.WriteLi ne("Position bytParam1 bytParam2")

Console.WriteLi ne("-------- --------- ---------")

For _i As Integer = 0 To bytParam1.Lengt h - 1
Console.WriteLi ne("{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.
"moondaddy" <mo*******@nosp am.nospam> wrote in message
news:On******** ******@TK2MSFTN GP12.phx.gbl...
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 = SomeMethodToCon vertByteToStrin g(byt)
5) Call the url:
System.Diagnost ics.Process.Sta rt("http://localhost/wsVIPN/Reports/VIPNReportStand ard.aspx?Param= "
& sParam)

On the server side in the web page load event

6) Receive the querystring: Dim sParam As String =
Request.QuerySt ring("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!

--
mo*******@nospa m.nospam
""Peter Huang" [MSFT]" <v-******@online.m icrosoft.com> wrote in message
news:iA******** *****@TK2MSFTNG XA01.phx.gbl...
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.


Nov 21 '05 #5
Dim sParam As String = System.Text.Enc oding.Default.G etString(bytPar am1)


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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #6
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.
"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:ut******** ******@TK2MSFTN GP15.phx.gbl...
Dim sParam As String = System.Text.Enc oding.Default.G etString(bytPar am1)


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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #7
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!

--
mo*******@nospa m.nospam
"Stephany Young" <noone@localhos t> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
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.
"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:ut******** ******@TK2MSFTN GP15.phx.gbl...
Dim sParam As String =
System.Text.Enc oding.Default.G etString(bytPar am1)


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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 21 '05 #8
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim bts(127) As Byte
For i As Integer = 0 To 127
bts(i) = i
Next
Dim str As String = HttpUtility.Url Encode(bts)
Process.Start(" http://localhost/WebSideServer/Default.aspx?na me=" &
str)
End Sub

[Asp.net side]
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Response.Write( "Start dump Byte
Array========== =============== =============== =====<BR>")
For Each b As Byte In
HttpUtility.Url DecodeToBytes(R equest.QueryStr ing("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.

Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
10307
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with bytes that are not printable characters? I've tested this and it seems to work fine, but I want to make sure there isn't some condition or situation I'm not aware of that could cause problems. I'm doing this because it's easier to do some of my...
5
2846
by: c duden | last post by:
I am attempting to encrypt some text and be able to decrypt it at a later time. I have two methods to do this: public static Byte EncryptText(string textToEncrypt, string encryptionHash) { Byte bytearrayinput = StringAndByteManipulation.ConvertStringToByteArray(textToEncrypt); //DES instance System.Security.Cryptography.TripleDESCryptoServiceProvider des = new
7
505
by: john | last post by:
On my form i have a message box called txtItemDesc that displays the french phrase qualité Père Noël. Now then when i run this code on that text box: Dim chrArr() As Char chrArr = txtItemDesc.Text.ToCharArray Dim pos As Integer While pos < chrArr.Length Dim c As Char MsgBox(Asc(chrArr(pos)) & " " & chrArr(pos)) pos = pos + 1
3
2732
by: Joshua Russell | last post by:
Hi, I've got a program (see source below) that makes a file and fills it with random binary values (from 0 to 255). The source below works, however the program creates files at a rate of about 0.5MB per second. There is a serious performance issue with this program. There is a loop within the main loop that generates a buffer of 500 Bytes that are then all written at once. Can anybody tell me how I can improve the performance of the...
6
33872
by: clintp | last post by:
I have a byte array that contains 8-bit ascii characters. I'm not particular about the codepage used to display them, but I have to preserve the position in the string with something and be able to back to the original byte implementation. For example, this doesn't work: // Bytes for .02{cent} in my standard codepage byte ba = new Byte { 0x2e, 0x30, 0x32, 0x9b };
0
3937
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
2
1505
by: Tiraman :-\) | last post by:
Hi Everyone, i have the following problem in my client-server Application My server take array and serialize it into the memorystream Dim ns As NetworkStream = client.GetStream() Dim writer As New IO.StreamWriter(ns) Dim bf As New BinaryFormatter Dim mem As New IO.MemoryStream
21
1795
by: Mike | last post by:
I am having a problem getting AND to work. I have a MySQL database field defined as varbinary(8) with X00 in it. Then I have define statements with X01 in it. define('CERTACCESS_MEDALS', x01); Then I run the data through a function function SecurityLevel_Check($security_byte, $securitylevel_bit) { //AND = 1 IF BOTH are 1 return $security_byte And $securitylevel_bit; }
0
8861
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8051
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.