PHP MD5 not the same as .NET MD5
I’m in the process of writing the API for our fancy new web management platform (I think you’ll like it, watch this space).
One of the security features I’ve implemented is an integrity check for the data being posted across the wire. I won’t go in to the details of it but all I need to say is I’m using MD5 to compute a hash of the posted data.
My trouble came tonight when trying to get an MD5 generated in .NET to match the one being passed to it by PHP. In PHP the md5 function is very basic, but it’s a bit more involved in .NET.
Finally though, I found the solution. Here’s the code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace SimpleWeb.API
{
public class MD5
{
/// <summary>
/// Returns an MD5 has of a string.
/// </summary>
/// <param name=”hashMe”></param>
/// <returns></returns>
public string GetHash(string hashMe)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
UTF7Encoding encoder = new UTF7Encoding();
Byte[] encStringBytes;
encStringBytes = encoder.GetBytes(hashMe);
encStringBytes = md5.ComputeHash(encStringBytes);
string strHex = string.Empty;
foreach (byte b in encStringBytes)
{
strHex += String.Format(“{0:x2}”, b);
}
return strHex;
}
}
}
It would seem the key thing to get them to match is to use the UTF7Encoding. Most of the MD5 examples around the web for .NET though seem to indicate you should use UTF8.
I’d be interested to know if this is the right way to go about cracking this nut, but for now, it’s the only way I can find.
-
victorantos
-
Morten K. Poulsen
-
Lee Kelleher
-
Dennis Bottaro
-
RyanTheGreat
-
Tom
-
Amrox
