473,408 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Bug found in VB.net using New Byte call

I came across an example in the MSDN documentation using RC2 encryption(the
link to the article is at the end of this message). When I tried it I had a
problem with getting back the same length string that I sent into it. After
working on Debugging the code I found two problems. One was in the
statement fromEncrypt = New Byte(encrypted.Length) {} where both
FromEncrypt and encrypted are both byte arrays and encrypted's length is 24.
Once this statement is executed FromEncrypt's length is 25. This introduces
an extra null byte onto fromEncrypt's array. The other problem, which maybe
just a misunderstanding on my part about the ASCIIEncoding object, is on
the statement roundtrip = textConverter.GetString(fromEncrypt). This call
to the ASCIIEncoding object, using a byte array with multiple 0 bytes on the
end of the array, results in a string with null characters attached on the
end. My undertanding is that strings are null terminated. When I pass a
byte array containing multiple 0 bytes on the end I would expect a byte to
string converter to remove nulls off the end of the byte array. As it
happened I had to write code to redim the byte array to remove the nulls
before I got back the same string that I sent. I hope this post helps
others who try to use this example in the future and I also hope Microsoft
reads these things so they know they have a problem in the New Byte(length)
method.
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemsecurit
ycryptographyrc2cryptoserviceproviderclasstopic.ht m
Nov 22 '05 #1
3 4226

"Steve Mauldin" <st**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I came across an example in the MSDN documentation using RC2 encryption(the link to the article is at the end of this message). When I tried it I had a problem with getting back the same length string that I sent into it. After working on Debugging the code I found two problems. One was in the
statement fromEncrypt = New Byte(encrypted.Length) {} where both
FromEncrypt and encrypted are both byte arrays and encrypted's length is 24. Once this statement is executed FromEncrypt's length is 25


IIRC, for VB, when you create an array of 25, it creates a 26 elements
(0..25) and not the 25 a C* programmer would expect (0..24).
Nov 22 '05 #2
Steve Mauldin <st**********@hotmail.com> wrote:
I came across an example in the MSDN documentation using RC2 encryption(the
link to the article is at the end of this message). When I tried it I had a
problem with getting back the same length string that I sent into it. After
working on Debugging the code I found two problems. One was in the
statement fromEncrypt = New Byte(encrypted.Length) {} where both
FromEncrypt and encrypted are both byte arrays and encrypted's length is 24.
Once this statement is executed FromEncrypt's length is 25. This introduces
an extra null byte onto fromEncrypt's array.
I'm not sure I follow you here - could you produce a short but complete
example?
The other problem, which maybe
just a misunderstanding on my part about the ASCIIEncoding object, is on
the statement roundtrip = textConverter.GetString(fromEncrypt). This call
to the ASCIIEncoding object, using a byte array with multiple 0 bytes on the
end of the array, results in a string with null characters attached on the
end. My undertanding is that strings are null terminated. When I pass a
byte array containing multiple 0 bytes on the end I would expect a byte to
string converter to remove nulls off the end of the byte array.
No, Unicode 0 is a perfectly valid character. Strings happen to be
stored in a null-terminated way in the CLR so that interop is easy, but
they aren't *conceptually* null-terminated, and the null character is
available as a character. A string is just a sequence of characters,
and has a length -
As it
happened I had to write code to redim the byte array to remove the nulls
before I got back the same string that I sent.
If you're having to do that, it sounds like something has gone wrong
earlier on. However, calling String.Replace ("\0", "") would do the job
otherwise.
I hope this post helps
others who try to use this example in the future and I also hope Microsoft
reads these things so they know they have a problem in the New Byte(length)
method.
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemsecurit
ycryptographyrc2cryptoserviceproviderclasstopic.ht m


In future, giving web links to MSDN is considerably more helpful,
otherwise people have to have the same version of MSDN installed as
you. However, having had a look at the MSDN documentation it certainly
doesn't look like well written code (there's no need to create a new
instance of ASCIIEncoding, for instance, declaring all the byte arrays
early on is horrible), reading from a stream and assuming the whole
length will be reading in one go is unreliable, etc. And indeed, the
supposedly round-tripped data *isn't* the same (try just writing the
length of the two strings out and you'll see the difference).
Basically, it's a nasty bit of code, but I don't think all of the
problems you have with it are necessary the same as the real problems
in the code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #3
Steve,
As Ayende stated. Arrays in VB.NET use the upper bound when defining arrays,
rather then the number of elements.

So:

fromEncrypt = New Byte(encrypted.Length) {}

Will create an array with an extra element.

While:

fromEncrypt = New Byte(encrypted.Length - 1) {}

Will create an array with the same number of elements.

Apparently this was done to simplify migrating from VB6 to VB.NET.

Hope this helps
Jay
"Steve Mauldin" <st**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I came across an example in the MSDN documentation using RC2 encryption(the link to the article is at the end of this message). When I tried it I had a problem with getting back the same length string that I sent into it. After working on Debugging the code I found two problems. One was in the
statement fromEncrypt = New Byte(encrypted.Length) {} where both
FromEncrypt and encrypted are both byte arrays and encrypted's length is 24. Once this statement is executed FromEncrypt's length is 25. This introduces an extra null byte onto fromEncrypt's array. The other problem, which maybe just a misunderstanding on my part about the ASCIIEncoding object, is on
the statement roundtrip = textConverter.GetString(fromEncrypt). This call to the ASCIIEncoding object, using a byte array with multiple 0 bytes on the end of the array, results in a string with null characters attached on the
end. My undertanding is that strings are null terminated. When I pass a
byte array containing multiple 0 bytes on the end I would expect a byte to
string converter to remove nulls off the end of the byte array. As it
happened I had to write code to redim the byte array to remove the nulls
before I got back the same string that I sent. I hope this post helps
others who try to use this example in the future and I also hope Microsoft
reads these things so they know they have a problem in the New Byte(length) method.
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemsecurit ycryptographyrc2cryptoserviceproviderclasstopic.ht m

Nov 22 '05 #4

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

Similar topics

3
by: Steve Mauldin | last post by:
I came across an example in the MSDN documentation using RC2 encryption(the link to the article is at the end of this message). When I tried it I had a problem with getting back the same length...
1
by: Trev | last post by:
I can call C++ methods using byte arrays as values but I wish to call a C++ method using a reference to an empty byte array that is then initiaised to a set size by the c++ method and populated...
0
by: Eugene Safrankow | last post by:
Hello All! I've encountered with the error when I call a method of dependency library (written in managed VC++) from Smart Client placed on a web page. In general, I make a call to the Windows...
1
by: Apogee | last post by:
Does anyone know what this means: "Invalid byte was found at byte index 63. " If yes, please help. Apogee
4
by: prashantkumar1982 | last post by:
I am using the select call to read from many sockets. I don't want to call read on every socket to check if it is closed, as it defeats the purpose of using the select call. Is there any way to...
4
by: Tony Cheng | last post by:
I want to know which account will be used when File.Exists are called in ASP.NET ? Thx
0
by: mathlec | last post by:
I have 2 WebApplication project (dummy and dummy2). The first is trying to load a UserControl compiled in the second. I've set up the application as follow: \_(Virtual directory) .:Modules:.
0
by: cgraham | last post by:
Hi, I have just upgraded our terminal server to Win 2003 Enterprise Ed on a new Dell server from an older model running Win 2003 Standard Ed. The DB2 client we are using is 7.1, Fixpak 14 and all...
2
by: damezumari | last post by:
I use win 2000, php 4.4.2, phpmyadmin 2.8.0.3, iis 5.0, and mysql 5.0. When I try to run http://localhost/phpmyadmin/index.php I get the error messages: --- The mbstring PHP extension was...
3
by: Rupali12345 | last post by:
hi all I m trying to read an .dcm file using byte array.But i dont know actually how to read byte code from file.please give me any example of reading byte code. I search in google also...but in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
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...

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.