繁体   English   中英

PHP 中的 SHA256 加盐散列与 C# 中的不同

[英]SHA256 Hashing with Salt in PHP not same as in C#

所以我得到了一个 ASP.NET web 应用程序,它使用加盐的散列法将密码存储在 mysql 数据库中。 我现在试图允许用户通过 php 网站使用与我的 web 应用程序相同的凭据登录。 我使用下面的代码来比较用户输入和 php 中的 hash

$pw = $salt . $extpassword;
if (!mb_check_encoding($pw, 'UTF-8')) {
    $pw = mb_convert_encoding($pw, 'UTF-8');
}

return ($fromdb == base64_encode(hash('sha256',$pw, true)));

至于我在c#中用来生成hash的代码:

System.Security.Cryptography.SHA256Managed sha256 = new System.Security.Cryptography.SHA256Managed();
byte[] hash = sha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(pw + salt)); 
return Convert.ToBase64String(hash);

我不确定为什么这不起作用,因为我是 php 的新手。有人可以帮忙吗?

在 php 你有salt + password ,在 .net 你有pw + salt

这将给出不同的结果。 通过使用修复:

$pw = $extpassword . $salt;

或者

byte[] hash = sha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(salt + pw)); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM