[英]C# code php equivalent md5 hash
我有这个C#代码:
public static string Encript(string strData)
{
string hashValue = HashData(strData);
string hexHashData = ConvertStringToHex(hashValue);
return hexHashData;
}
public static string HashData(string textToBeEncripted)
{
//Convert the string to a byte array
Byte[] byteDataToHash = System.Text.Encoding.Unicode.GetBytes(textToBeEncripted);
//Compute the MD5 hash algorithm
Byte[] byteHashValue = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(byteDataToHash);
return System.Text.Encoding.Unicode.GetString(byteHashValue);
}
public static string ConvertStringToHex(string asciiString)
{
string hex = "";
foreach (char c in asciiString)
{
int tmp = c;
hex += String.Format("{0:x2}", (uint) System.Convert.ToUInt32(tmp.ToString()));
}
return hex;
}
在这里你可以看到一个在线版本 。你可以看到字符串“test”我得到输出“5c82e9e41c7599f790ef1d774b7e6bf”这就是我在php端试过的
$a = "test";
$a = mb_convert_encoding($a, "UTF-16LE");
$a = md5($a);
echo $a;
但是php代码的值是“c8059e2ec7419f590e79d7f1b774bfe6”。为什么不工作?
编辑:看起来C#代码不正确,需要更换
'test'的正确MD5哈希是PHP中的'098f6bcd4621d373cade4e832627b4f6'。
我用C#中的代码测试了它
public static string CreateMD5(string input)
{
// Use input string to calculate MD5 hash
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
// Convert the byte array to hexadecimal string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
}
它将生成与PHP相同的哈希值,而不使用您正在使用的“转换编码”方法。
我相信转换编码是给你一个不同的答案,没有尝试
$a = mb_convert_encoding($a, "UTF-16LE");
问题是您在C#代码中错误地转换结果。 如果在调用ComputeHash
后在代码中放置断点,并检查byteHashValue
的值,您将看到它是c8059e2e...
或者,只需将此代码添加到ComputeHash
方法:
Console.WriteLine(BitConverter.ToString(byteHashValue));
我建议你重写你的代码:
public static string Encript(string strData)
{
string hashValue = HashData(strData);
return hashValue;
}
public static string HashData(string textToBeEncripted)
{
//Convert the string to a byte array
Byte[] byteDataToHash = System.Text.Encoding.Unicode.GetBytes(textToBeEncripted);
//Compute the MD5 hash algorithm
Byte[] byteHashValue = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(byteDataToHash);
return BitConverter.ToString(byteHashValue).Replace("-", "");
}
哦,还有旁注:这个词是“加密”,而不是“上标”。
您的情况下的问题不是编码,而是您在C#端转换为字符串。 只要您在任何地方使用相同的编码,它应该按预期工作。 但请注意,大多数在线哈希斯使用ASCII
编码,而您使用的是UTF-16的System.Text.Encoding.Unicode
,因此结果将与在线编码器不同。
下面的代码将给出与PHP代码段相同的结果(即c8059e2ec7419f590e79d7f1b774bfe6
)
public static void Main(string[] args)
{
string a = "test";
string en = HashData(a);
Console.WriteLine(en);
}
public static string HashData(string textToBeEncripted)
{
Byte[] byteDataToHash = System.Text.Encoding.Unicode.GetBytes(textToBeEncripted);
Byte[] byteHashValue = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(byteDataToHash);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (var b in byteHashValue)
s.Append(b.ToString("x2"));
return s.ToString();
}
如果您使用System.Text.Encoding.ASCII
,您将获得其他答案中建议的098f6bcd4621d373cade4e832627b4f6
。 但是,您还必须在PHP代码中使用ASCII编码。
这是因为对于UTF16,每个字符由两个字节表示,而不仅仅由一个字节表示。 因此byteDataToHash
将具有两倍的大小( [116, 0, 101, 0, 115, 0, 116, 0]
byteDataToHash
[116, 0, 101, 0, 115, 0, 116, 0]
与[116, 101, 115, 116]
byteDataToHash
[116, 101, 115, 116]
)。 当然,不同的字节向量会导致不同的哈希值。 但如上所述,只要所有包含的组件使用相同的编码,您使用哪一个并不重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.