这是人们在我的网站上注册时我所拥有的加密:
$salt = generateSalt();
$hashedPassword = crypt($userPass, $salt);
这是我的generateSalt
函数:
function generateSalt() {
$salt = uniqid(mt_rand(), true);
$salt = '$1$' . $salt;
return $salt;
}
当我用这个加密密码时,我得到例如:
$1$92999442$AK4yZPjnj6BKc9yj4CXKu1
但是当我用这个函数在 C# 上加密相同的密码时:
hashedPassword = GenerateMD5(uName, salt);
生成MD5函数:
public String GenerateMD5(String input, String salt)
{
Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
System.Security.Cryptography.MD5Cng md5hashstring = new System.Security.Cryptography.MD5Cng();
byte[] hash = md5hashstring.ComputeHash(bytes);
string hex = BitConverter.ToString(hash).Replace("-", string.Empty);
return hex;
}
我得到了完全不同的输出。使用相同的密码和相同的盐,我得到这个输出:
9DE11D48C3F7DF1BF89FC76D755A2596
我应该在 PHP 和 C# 中使用什么函数来获得相同的输出?
最佳答案
因为您使用的是两种完全不同的算法。在 PHP 中,您使用的是使用 DES 的 crypt()
,而在 C# 中,您使用的是 MD5。他们永远不会产生相同的输出。如果你想要相同的输出,你应该在 PHP 中使用 md5()
而不是 crypt()
此外,不要使用 MD5,它已被弃用。您现在至少应该使用 SHA-2
关于c# - PHP 和 C# MD5 crypt 没有给出相同的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30081199/