【发布时间】:2020-03-22 23:59:21
【问题描述】:
我对 asp.net/c# 很陌生,我正在尝试在 C# 中重新创建密码验证。我将此哈希存储在数据库中:
U2zdbUmZXCeOLs0OuS9bhg==7hQ60TTq0ZiT/z+eu4bdzpmBcp5uYa70ZDxQPncEG0c=
这个哈希的密码是 1234567。这是因为我可以在 web 应用程序中使用这个密码登录。
所以如果我理解正确的话。散列由 base64 编码的盐 U2zdbUmZXCeOLs0OuS9bhg== 和使用此盐散列的密码组成:7hQ60TTq0ZiT/z+eu4bdzpmBcp5uYa70ZDxQPncEG0c=
但是如果我使用我在互联网上找到的这个例子。我没有得到相同的哈希结果。我已经尝试过使用编码(导致不同的哈希),但没有运气。 hashAlgorithmType 在 web.config 中设置为 HMACSHA256。我做错了什么?
using System;
using System.Security.Cryptography;
using System.Text;
public class Program
{
public static void Main()
{
var base64Salt = "U2zdbUmZXCeOLs0OuS9bhg==";
var base64Hash = "7hQ60TTq0ZiT/z+eu4bdzpmBcp5uYa70ZDxQPncEG0c=";
// Decode the base64 salt to get the salt byte array
var saltBytes = Convert.FromBase64String(base64Salt);
// Provide the user's plain password
var plaintextPassword = "1234567";
// Salt the plaintext password, prepend to user's provided password, and then hash
try
{
var hmac256 = new HMACSHA256(saltBytes);
var hash = Convert.ToBase64String(hmac256.ComputeHash(Encoding.UTF8.GetBytes(plaintextPassword)));
Console.WriteLine(base64Salt+hash);
if (hash == base64Hash)
{
Console.WriteLine("Success! Both hashes match!");
}
else
{
Console.WriteLine("Passwords do not match.");
}
}
catch (Exception e)
{
Console.WriteLine("Error!", e.Message);
}
}
}
【问题讨论】:
标签: c# asp.net security hash hmac