c#md5与PHPs md5哈希相同
我有点疯了!我正在尝试根据现有数据库对我的应用程序进行身份验证(因此我无法更改PHP端),我需要将我的密码字段转换为与PHP的md5(moo)命令相同的字段 但是,我尝试创建哈希的每个公式都具有相同的md5,与数据库中的内容非常不同 有没有产生相同结果的公式 我试过:c#md5与PHPs md5哈希相同,c#,md5,C#,Md5,我有点疯了!我正在尝试根据现有数据库对我的应用程序进行身份验证(因此我无法更改PHP端),我需要将我的密码字段转换为与PHP的md5(moo)命令相同的字段 但是,我尝试创建哈希的每个公式都具有相同的md5,与数据库中的内容非常不同 有没有产生相同结果的公式 我试过: public static string MbelcherEncodePassword(string originalPassword) { Byte[] originalBytes;
public static string MbelcherEncodePassword(string originalPassword)
{
Byte[] originalBytes;
Byte[] encodedBytes;
MD5 md5;
// Conver the original password to bytes; then create the hash
md5 = new MD5CryptoServiceProvider();
originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
encodedBytes = md5.ComputeHash(originalBytes);
// Bytes to string
return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(encodedBytes), "-", "").ToLower();
}
以及:
无济于事。任何帮助都将不胜感激
谢谢以下内容将为您提供与PHP md5相同的十六进制字符串:
public string GetMd5Hex(MD5 crypt, string input)
{
return crypt.ComputeHash(UTF8Encoding.UTF8.GetBytes(input))
.Select<byte, string>(a => a.ToString("x2"))
.Aggregate<string>((a, b) => string.Format("{0}{1}", a, b));
}
公共字符串GetMd5Hex(MD5密码,字符串输入)
{
返回crypt.ComputeHash(UTF8Encoding.UTF8.GetBytes(输入))
.选择(a=>a.ToString(“x2”))
.Aggregate((a,b)=>string.Format(“{0}{1}”,a,b));
}
为什么要进行密码编辑?PHP很可能使用UTF-8进行输入,因此也可以使用UTF8Encoding
。虽然这对“测试”密码来说并不重要…1。检查现有应用程序是否使用salt。它可能会这样做(尽管,考虑到应用程序使用MD5进行密码哈希,我不能打赌)-请参阅以获取有关密码存储的更多信息;2.密码的字符集是否仅限于(ASCII的子集)?“数据库中有什么”也不相关。PHP代码可能每次都在数据库中写入随机字节,您是否能够通过查看结果来匹配其行为?你需要看看代码,等等。。。您正在使用md5进行密码哈希!恐怖!md5在这方面非常糟糕,一点也不安全:(另外:发布您正在使用的php,以及一个示例输入/输出对。
public static string MD5Hash(string text)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.Default.GetBytes(text))), "-", "");
}
public string GetMd5Hex(MD5 crypt, string input)
{
return crypt.ComputeHash(UTF8Encoding.UTF8.GetBytes(input))
.Select<byte, string>(a => a.ToString("x2"))
.Aggregate<string>((a, b) => string.Format("{0}{1}", a, b));
}