473,387 Members | 1,535 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,387 software developers and data experts.

C# and PHP MD5 functions

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("d388dad90d4bbd760a152321 f2143af7");
//Correct Response
}
public static string MD5(string password)
{
ASCIIEncoding UE = new ASCIIEncoding();
byte[] textBytes = UE.GetBytes(password);
try
{
System.Security.Cryptography.MD5CryptoServiceProvi der
cryptHandler;
cryptHandler = new
System.Security.Cryptography.MD5CryptoServiceProvi der();
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
*-----------------------*
Nov 16 '05 #1
1 1828

"timothylg" <tg*****@xuttuh-dot-org.no-spam.invalid> wrote in message
news:41**********@Usenet.com...
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?

Nov 16 '05 #2

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

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.