【发布时间】:2018-03-03 06:15:36
【问题描述】:
在 C# 中,我试图获取密码的哈希 md5 值,如下所示:
string sb = textBox2.Text;
byte[] asciiBytes = ASCIIEncoding.ASCII.GetBytes(sb);
byte[] hashedBytes = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
string hashedString = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(sb);
sb = System.Convert.ToBase64String(plainTextBytes);
在 php 中,我使用 md5 命令获取该值,
echo md5("megusia94");
两种情况下的输入是相同的, 然而 PHP 的输出是: d1e44ad921daadaf8defadcd21c8644a 而在 C# 中,输出为:bWVndXMpYTk0
我做错了什么?我搜索了这个论坛并尝试了: MD5 hashing does not match in C# and PHP c# md5 and php md5 not match
【问题讨论】:
-
你不应该使用MD5 password hashes。请使用 PHP 的 built-in functions(
password_hash()和password_verify())来处理密码安全问题。如果您使用的 PHP 版本低于 5.5,您可以使用password_hash()compatibility pack。 没有必要 escape passwords 或在散列之前对其使用任何其他清理机制。 -
Fred,我也尝试了解决方案...
-
我在上面发布的可能重复的答案与另一个问题中接受的答案中使用的完全相同