This has come up several times, so here is the lowdown. You may be confused why your 2 identical strings generate different key bytes in PHP and .Net. The answer lies in the encoding. In both platforms, a string is a collection of characters.
- PHP characters are ASCII encoded, with a length of 7 bits.
- .Net characters are UTF-16, 1 or more sets of 16-bit words.
If you want your strings to generate the same bytecode, you need to do some encoding. In this example, I want to make a 24 byte key that is the md5 of some plain text.
Example:
PHP:
$key = substr(md5("some random text"),0,24);
.Net
static string MD5SUM(byte[] FileOrText) //Output: String<-> Input: Byte[] //
{
return BitConverter.ToString(new MD5CryptoServiceProvider()
.ComputeHash(FileOrText)).Replace("-","").ToLower();
}
string md5 = MD5SUM(ASCIIEncoding.ASCII.GetBytes("some random text"))
.Substring(0, 24);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
key = enc.GetBytes(md5);