Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# and PHP MD5 functions

Status
Not open for further replies.

Timothylg

Programmer
Dec 16, 2004
1
US
I am trying to implement Digest-MD5 auth (smtp/imap ver, rfc 2831 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
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:
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;
			}
		}

Thanks

Timothy
 
I think the C# code is Okay but the difference could be due to the pack() function.
I would tru to change the first parameter from H to U (unicode).
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top