473,406 Members | 2,467 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,406 software developers and data experts.

System.Security.Cryptography.MD5.Create( ??? )

Does anyone know why the documentation for
System.Security.Cryptography.MD5.Create() seems to omit completely any
description of allowed arguments.

I'm trying to convert some C++ code to C# and seem to be getting different
MD5 results. If other algorithms are available, maybe that is related to the
problem I'm having. But the docs for this method don't tell me anything
about what algorithms are supported?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Nov 16 '06 #1
4 6954
Jonathan Wood <jw***@softcircuits.comwrote:
Does anyone know why the documentation for
System.Security.Cryptography.MD5.Create() seems to omit completely any
description of allowed arguments.

I'm trying to convert some C++ code to C# and seem to be getting different
MD5 results. If other algorithms are available, maybe that is related to the
problem I'm having. But the docs for this method don't tell me anything
about what algorithms are supported?
MD5 *is* the algorithm - the version which accepts names of different
implementations of the algorithm should by and large be ignored.

Could you post a short but complete program which demonstrates the
problem you're having? I wouldn't be surprised if it were to do with
converting the data you've got into a binary form to be hashed in the
first place.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 16 '06 #2
Actually, it's part of some rather complex code used to create shareware
authorization licenses so I can't really post a complete program here. But a
key part calculates a 32-bit checksum of a string and looks something like
this (I'm still trying to figure out what's available in the CRT). This is
designed to duplicate some existing C++ code where each character is
converted to a WORD (so I get the same result in both multibyte and Unicode
versions). The result is then computed by adding all four DWORDs of the
result, which is a bit awkward in C#.

UInt32 CalcCheckSum(string s)
{
MD5 md5 = MD5.Create();
// md5.Initialize();
byte[] data = StringToByteArray(s);
data = md5.ComputeHash(data);

UInt32 result = 0;
for (int i = 0; i < data.Length; i += sizeof(UInt32))
result += ByteArrayToDword(data, i);
return result;
}

byte[] StringToByteArray(string s)
{
byte[] b = new byte[s.Length << 1];
for (int i = 0; i < s.Length; i++)
{
b[i << 1] = (byte)s[i];
b[(i << 1) + 1] = (byte)(s[i] >8);
}
return b;
}

UInt32 ByteArrayToDword(byte[] b, int startIndex)
{
return (UInt32)(b[startIndex] |
b[startIndex + 1] << 8 |
b[startIndex + 2] << 16 |
b[startIndex + 3] << 24);
}
--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Jonathan Wood <jw***@softcircuits.comwrote:
>Does anyone know why the documentation for
System.Security.Cryptography.MD5.Create() seems to omit completely any
description of allowed arguments.

I'm trying to convert some C++ code to C# and seem to be getting
different
MD5 results. If other algorithms are available, maybe that is related to
the
problem I'm having. But the docs for this method don't tell me anything
about what algorithms are supported?

MD5 *is* the algorithm - the version which accepts names of different
implementations of the algorithm should by and large be ignored.

Could you post a short but complete program which demonstrates the
problem you're having? I wouldn't be surprised if it were to do with
converting the data you've got into a binary form to be hashed in the
first place.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 16 '06 #3
Jonathan Wood <jw***@softcircuits.comwrote:
Actually, it's part of some rather complex code used to create shareware
authorization licenses so I can't really post a complete program here. But a
key part calculates a 32-bit checksum of a string and looks something like
this (I'm still trying to figure out what's available in the CRT). This is
designed to duplicate some existing C++ code where each character is
converted to a WORD (so I get the same result in both multibyte and Unicode
versions). The result is then computed by adding all four DWORDs of the
result, which is a bit awkward in C#.

UInt32 CalcCheckSum(string s)
{
MD5 md5 = MD5.Create();
// md5.Initialize();
byte[] data = StringToByteArray(s);
data = md5.ComputeHash(data);

UInt32 result = 0;
for (int i = 0; i < data.Length; i += sizeof(UInt32))
result += ByteArrayToDword(data, i);
return result;
}
So, the first thing to check is whether the data that you're hashing is
exactly the same. My guess is that that's the problem.

I suggest you print out the content of data before calling ComputeHash
- just print each byte out as an integer in both your C++ code and the
C# code.

Now to make your code easier:

StringToByteArray can be replaced with Encoding.Unicode.GetBytes(data)
ByteArrayToDword can be replaced with BitConverter.ToUInt32(data, i)

If you need to do bit conversion with a different endianness, I have an
EndianBitConverter in my misc util library:
http://www.pobox.com/~skeet/csharp/miscutil

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '06 #4
Dangit! Unbelievably, C++ code that I've been using for several years had a
bug where it only looked at the first 4 bytes of the 16 bytes of MD5 hash
data. After many hours, I managed to isolate this and I get the same result
when I do the same in my C# code.

Thanks!

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Jonathan Wood <jw***@softcircuits.comwrote:
>Actually, it's part of some rather complex code used to create shareware
authorization licenses so I can't really post a complete program here.
But a
key part calculates a 32-bit checksum of a string and looks something
like
this (I'm still trying to figure out what's available in the CRT). This
is
designed to duplicate some existing C++ code where each character is
converted to a WORD (so I get the same result in both multibyte and
Unicode
versions). The result is then computed by adding all four DWORDs of the
result, which is a bit awkward in C#.

UInt32 CalcCheckSum(string s)
{
MD5 md5 = MD5.Create();
// md5.Initialize();
byte[] data = StringToByteArray(s);
data = md5.ComputeHash(data);

UInt32 result = 0;
for (int i = 0; i < data.Length; i += sizeof(UInt32))
result += ByteArrayToDword(data, i);
return result;
}

So, the first thing to check is whether the data that you're hashing is
exactly the same. My guess is that that's the problem.

I suggest you print out the content of data before calling ComputeHash
- just print each byte out as an integer in both your C++ code and the
C# code.

Now to make your code easier:

StringToByteArray can be replaced with Encoding.Unicode.GetBytes(data)
ByteArrayToDword can be replaced with BitConverter.ToUInt32(data, i)

If you need to do bit conversion with a different endianness, I have an
EndianBitConverter in my misc util library:
http://www.pobox.com/~skeet/csharp/miscutil

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '06 #5

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

Similar topics

0
by: Andrzej | last post by:
Hi, I have to figure out why we have a problem with special characters in encrypted usernames and passwords. Case: Username: r&bgeorge Password: tigger
0
by: Ebert | last post by:
hi I am just trying out some example in the help, however, it is stranged that I can't imports system.security.cryptography.Xml namespace, I only have cryptography.X509 and the rest but not .XML,...
3
by: segue | last post by:
I have VS 2005, .NET 2.0, no girlfriend and this article: http://msdn2.microsoft.com/en-us/library/ms278836.aspx. Yet, I can't successfully import this namespace. ...
1
by: muthu | last post by:
Hi, I have two web applications running on my machine.The application is developed using asp.net 1.1 and vb.net.When i try to run both the applications in the same browsers, i get the following...
1
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
Hi, everybody here. I am implementing data encryption/decryption, and try to use System.Security.Cryptography.TripleDESCryptoServiceProvider. But I can not find it in MS Visual Studio when I...
1
by: Eric Simmons | last post by:
Hello, I am trying to run a .NET 2.0 application that I developed and I am getting the following error: Key not valid for use in specified state I am attempting to retrieve the...
6
by: andrew | last post by:
Hi, I have a web service application written in C# .NET 1.1 using MD5CryptoServiceProvider.ComputeHash(Byte) The problem is that after a while(web service processes requests) the call throws...
0
by: Amelyan | last post by:
Why does this happen? How to fix it? Once in a while I get error in ~/ScriptResource.axd?d=... System.Reflection.TargetInvocationException: Exception has been thrown by the target of an...
0
by: a.bavdhankar | last post by:
Hi, I am using System.Security.Cryptography.RijndaelManaged in web application that is executed under annonymous user. I am getting access denied error message. It seems annonymous user does...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...
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...

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.