C# and PHP MD5 functions

T

timothylg

I am trying to implement Digest-MD5 auth (smtp/imap ver, rfc 2831
http://www.faqs.org/rfcs/rfc2831.html). I have a working example of
this in php (which I gleamed form the Pear project), I'm trying to
port this to c#, this is where I am beginning to run into problems.
In PHP I recieve the expected results, but in C# I do not. I believe
that this might be an encoding problem (The text being hashed should
be in ascii, and I believed that I accounted for that but I think it
might be helpful to have a few extra pair of eyes looking at the
code, maybe telling me what I have done wronge.

The working PHP code

<?php
$authcid = "chris";
$realm = "elwood.innosoft.com";
$pass = "secret";
$nonce = "OA6MG9tEQGm2hh";
$cnonce = "OA6MHXh6VqTrRk";
$digest_uri = "imap/elwood.innosoft.com";
$A1 = sprintf('%s:%s:%s', pack('H32',
md5(sprintf('%s:%s:%s', $authcid, $realm,
$pass))), $nonce, $cnonce);
$A2 = 'AUTHENTICATE:' . $digest_uri;

print(md5(sprintf('%s:%s:00000001:%s:auth:%s',
md5($A1), $nonce, $cnonce, md5($A2))));

printf("<BR>d388dad90d4bbd760a152321f2143af7");
##Correct Response

?>


And now the c# code segment

[code:1:888bd25bbc]private void DigestMD5Test_Click(object sender,
System.EventArgs e)
{
string authcid = "chris";
string realm = "elwood.innosoft.com";
string pass = "secret";
string nonce = "OA6MG9tEQGm2hh";
string cnonce = "OA6MHXh6VqTrRk";
string digest_uri = "imap/elwood.innosoft.com";
string A1 =
String.Format("{0}:{1}:{2}",
MD5(string.Format("{0}:{1}:{2}",
authcid, realm, pass)), nonce, cnonce);
string A2 = "AUTHENTICATE:" + digest_uri;

System.Console.WriteLine(MD5(String.Format("{0}:{1}:00000001:{2}:auth:{3}",
MD5(A1), nonce, cnonce, MD5(A2))));

System.Console.WriteLine("d388dad90d4bbd760a152321f2143af7");
//Correct Response
}
public static string MD5(string password)
{
ASCIIEncoding UE = new ASCIIEncoding();
byte[] textBytes = UE.GetBytes(password);
try
{
System.Security.Cryptography.MD5CryptoServiceProvider
cryptHandler;
cryptHandler = new
System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash = cryptHandler.ComputeHash
(textBytes);
StringBuilder sb = new StringBuilder(hash.Length *
2);
foreach(byte b in hash)
{

sb.Append(String.Format("{0:x2}",
b));
}
return sb.ToString().ToLower();
}
catch
{
throw;
}
}[/code:1:888bd25bbc]

Thanks

Timothy

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
D

Daniel O'Connell [C# MVP]

timothylg said:
I am trying to implement Digest-MD5 auth (smtp/imap ver, rfc 2831
http://www.faqs.org/rfcs/rfc2831.html). I have a working example of
this in php (which I gleamed form the Pear project), I'm trying to
port this to c#, this is where I am beginning to run into problems.
In PHP I recieve the expected results, but in C# I do not. I believe
that this might be an encoding problem (The text being hashed should
be in ascii, and I believed that I accounted for that but I think it
might be helpful to have a few extra pair of eyes looking at the
code, maybe telling me what I have done wronge.

First thing that comes to mind...what does pack() do in php? I don't think
thats being performend in the C# version.

Second, next time please try to post simpler code. Your use of namespaces is
really annoying(you use standard namespaces for some code and other code has
no namespaces attached when those classes are not in a standard C# file).
You also have things far too deeply nested for someone unfamiliar with PHP
to follow. You should read
http://www.yoda.arachsys.com/csharp/complete.html.

Have you tried just hashing a simple string instead of trying all this and
make sure its not an encoding issue?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top