1

Hello I trying to convert this part code from php to c# with identical result:

PHP:

md5("apple", true) //result :8pѕ'OlIігg(•

C#:

    byte[] asciiBytes = ASCIIEncoding.ASCII.GetBytes("apple");
    byte[] hashedBytes =  MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
    return System.Text.Encoding.ASCII.GetString(hashedBytes); //result: 8p?'OlI??g(?

Similar but not exactly

upd: with BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); I have got: 1f3870be274f6c49b3e31a0c6728957f

And I have not any problems when I use md5("apple", false)

coder
  • 8,346
  • 16
  • 39
  • 53
Ilya Sulimanov
  • 7,636
  • 6
  • 47
  • 68
  • 2
    This helps? http://stackoverflow.com/questions/5821677/md5-hashing-does-not-match-in-c-sharp-and-php – Jose Rocha Nov 24 '15 at 14:42
  • 2
    You're printing raw binary garbage, and some of those characters are being mis-interpreted by your output environment. e.g. some of the byte sequences LOOK like multi-byte characters. Your md5 is perfectly fine, it's just an output/display problem. – Marc B Nov 24 '15 at 14:45
  • Side node: `MD5CryptoServiceProvider` is `IDisposable` and thus should be *disposed* – Dmitry Bychenko Nov 24 '15 at 14:50

1 Answers1

0

Try this:

 var md5 = System.Security.Cryptography.MD5.Create();
 byte[] inputBytes = Encoding.Default.GetBytes(input);
 byte[] hash = md5.ComputeHash(inputBytes);

 var s = Encoding.Default.GetString(hash);

or choose another Encoding format.

Maxim Goncharuk
  • 1,285
  • 12
  • 20