1

I am in the process of try to integrate some C# ASP.NET webservice into my PHP application however there is a big problem. The way the C# webservice does the encryption is not compatible with the way PHP does the MD5 encryption. I have found solution for converting the C# MD5 to PHP MD5 however I can't change the C# code. Is there a way to change the way that PHP does its MD5 encryption to match C#? The c# encryption works like this:

MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(stringToEncrypt);
String myString = Convert.ToBase64String(MD5.ComputeHash(bs));
4
  • 4
    Try sharing your PHP code also. And technically, MD5 is not encryption, but a one-way hash algorithm. Commented Dec 13, 2010 at 21:43
  • 3
    Neither MD5 nor Base64 are encryptions; MD5 is a cryptographic hash function and Base64 is a base conversion to represent binary data with 64 printable ASCII characters. Commented Dec 13, 2010 at 21:44
  • my code is base64_encode(md5(utf8_encode('123456789'))) Commented Dec 14, 2010 at 11:23
  • apparently I needed to do base64_encode(md5('123456789', true)) Commented Dec 14, 2010 at 13:04

4 Answers 4

3

Since MD5 is a published algorithm, the implementations in PHP and c# are identical. The only difference is the string format of the byte sequence they produce. PHP produces a hex string, so you just need to convert from hex to Base64.

Sign up to request clarification or add additional context in comments.

1 Comment

base64_encode(md5('123456789', true)) will also work as noted by ryanzec - I saw no mention as to why exactly this works so I will briefly explain - the optional boolean value, which defaults to false, declares that you want a hexadecimal representation of the md5 hash as opposed to the raw binary representation
0

This could be an encoding issue. Make sure your PHP version gets the UTF-8 representation of the string, apply MD5, then convert to a base 64 string, and you should be fine.

1 Comment

well I am doing base64_encode(md5(utf8_encode('123456789')))) but not getting the same as C#
0

This is an encoding issue I have had before between PHP and C#.

If PHP is manipulating ISO-8859-1 strings (which is probably your case), you can do:

md5(utf8_encode($stringToEncrypt))

The best way to test is to try a very simple string with a-z content, which should work fine regardless of any encoding issue. It that works and the whole thing doesn't, you have an encoding issue.

You could also do:

byte[] bs = System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(stringToEncrypt);

and

md5($stringToEncrypt)

will work just fine.

Comments

0

I see what is your problem:

String myString = Convert.ToBase64String(MD5.ComputeHash(bs)); 

will make a Base64 string out of a pure MD5 array hash.

PHP will first make an hex representation of the MD5 hash then base64-encode it.

You should get the hex result first as described here:

http://blog.stevex.net/c-code-snippet-creating-an-md5-hash-string/

and then base64 encode the string.

1 Comment

yea, I know that but like I said, I can't change the C#, only the PHP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.