![]() |
![]() |
|
August 23, 2006WTF? Can some .NET guru help me out?Recently I had to figure out some VB.NET code and port it to C#. I am not much of a VB guy, but I figured I could understand some basic stuff. I am having some difficulties, and was hoping someone could explain the brain dead issue I am having. Here is the VB.NET code:
Here is the C# code I am trying:
You would THINK that key_aes128 would be the same. They are CLOSE.... but not actually the same. Anyone know what the heck I am doing wrong? It SHOULD result in the exact same string. Posted by SilverStr at August 23, 2006 03:46 PM | TrackBackComments
The VB.NET Chr() function is real strange one (that's legacy for you). Here's a version on your C# program's Chr that should return the same result: static Char Chr(int i) Note there's a couple of assumptions I've made here. First of all, I'm assuming that your current system-default codepage is a single-byte code-page, and second, you're never going to pass anything > 0xFF into your Chr function (because then it does something different). If you don't mind your ported code depending on the Microsoft.VisualBasic assmebly, and the most important thing for you is compatibility with the VB version, then I'd suggest you set that as a dependency of your C# app, and use the VB Chr function directly: static Char Chr(int i) Take a step back and think about what you're doing. You're creating an AES key. That should be binary - a byte array - and should go nowhere near a Unicode string. Use: byte[] key_aes128 = This could be different from the VB code depending on how the VB code gets a byte array to use with the AES algorithm. You _are_ using System.Security.Cryptography.Rijndael, aren't you? (I'm guessing the name of the class wasn't changed when Rijndael was selected as the AES algorithm). If you really must use a string I would recommend the use of proper string literal syntax, which VB doesn't support, hence the irritating concatenation: string key_aes128 = Well, I'd also agree with Mike that changing the behaviour would be better than trying to copy the VB code, but that's not always possible... Posted by: Dean Harding at August 23, 2006 05:40 PMThe VB Chr function does more than just return a Char. It determines if you are using single or double byte character set and examines your current culture before returning. So you get a culture specific string. Many of the VB conversion functions work this way. I suspect this might be the issue. If you look at the disassembled code you'll see what is going on. As others have said, the byte array might work better than converting from VB. Posted by: Walt Ritscher - Thinking about code at August 23, 2006 05:56 PMGreat comments by everyone. Thanks so much. It appears that the Chr() VB function indeed does more that I originally got from the MSDN docs. Dean's solution works well. The only difference I did was add a sentry to ensure that nothing larger that 0xFF gets passed in. From this, I now have a fully working test program mimicking the functionality of the VB.NET app. Now I can refactor it and clean it up. I really appreciate everyone's insights. Thanks again. Posted by: Dana Epp at August 23, 2006 06:09 PM |
![]() ![]()
My 5 Favorite Books
Writing Secure Code
Secure Programming Cookbook Security Engineering Secure Coding Principles & Practice Inside the Security Mind ![]()
My 5 Favorite Papers
Smashing the Stack
Penetration Studies Covert Channel Analysis of Trusted Systems DoD Trusted Computer System Evaluation Criteria NSA Security Recommendation Guides ![]()
Archives
September 2006
August 2006 July 2006 June 2006 May 2006 April 2006 March 2006 February 2006 January 2006 December 2005 November 2005 October 2005 September 2005 August 2005 July 2005 June 2005 May 2005 April 2005 March 2005 February 2005 January 2005 December 2004 November 2004 October 2004 September 2004 August 2004 July 2004 June 2004 May 2004 April 2004 March 2004 February 2004 January 2004 December 2003 November 2003 October 2003 September 2003 August 2003 July 2003 June 2003 May 2003 April 2003 March 2003 February 2003 January 2003 December 2002 November 2002 October 2002 September 2002 August 2002 July 2002 ![]() |
|