我尝试使用MD5在PHP中散列字符串,在C#中使用相同的字符串,但如果字符串包含特殊字符,则结果会有所不同。有人可以解释我如何匹配吗?
这是我在C#中的代码:
MD5 md5 = new MD5CryptoServiceProvider();
//compute hash from the bytes of text
md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));
//get hash result after compute it
byte[] result = md5.Hash;
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
//change it into 2 hexadecimal digits
//for each byte
strBuilder.Append(result[i].ToString("x2"));
}
return strBuilder.ToString();
我的字符串是"Samuel2989&*"
字符串。
输出:
PHP:957915b9c9a8fb65e13fe1dc9a8b86d4
C#:f5ee451f7eb4587da00d4aa31ae1c378
答案 0 :(得分:0)
你可以试试这种方法并告诉我它是否适合你?
public static string MD5Hash(string input)
{
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
bs = x.ComputeHash(bs);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in bs)
{
s.Append(b.ToString("x2").ToLower());
}
string password = s.ToString();
return password;
}