【问题标题】:Manually Converting Password and Salt to Compare with ASPNETDB Password手动转换密码和盐以与 ASPNETDB 密码进行比较
【发布时间】:2012-05-16 19:16:20
【问题描述】:

我正在尝试手动将 aspnetdb 密码字段与我的手动哈希密码进行比较以检查有效性(我无法使用默认的成员身份实现,因为我正在使用自定义实现)。无论如何,我从记录中获取 Base64 密码 salt,并使用以下算法获取加盐哈希:

static byte[] GenerateSaltedHash(byte[] plainText, byte[] salt)
{
HashAlgorithm algorithm = new SHA256Managed();

byte[] plainTextWithSaltBytes =
    new byte[plainText.Length + salt.Length];

for (int i = 0; i < plainText.Length; i++)
{
    plainTextWithSaltBytes[i] = plainText[i];
}
for (int i = 0; i < salt.Length; i++)
{
    plainTextWithSaltBytes[plainText.Length + i] = salt[i];
}

byte[] hash = algorithm.ComputeHash(plainTextWithSaltBytes);

return hash;
}

然后我获取这些字节并与 Convert.GetBase64Bytes(MembershipUser.Password) 进行比较。尽管我从 Convert 方法获得的字节和我计算的哈希值永远不会相同,即使我知道密码相同,但在某处存在断开连接。

任何关于我哪里出错的见解?

【问题讨论】:

  • 您可以通过使用Array.CopyTo 连接两个字节数组来节省一些代码和可能的错误: byte[] plainTextWithSaltBytes = new byte[plainText.Length + salt.Length]; plainText.CopyTo(plainTextWithSaltBytes, 0); salt.CopyTo(plainTextWithSaltBytes, plainText.Length);

标签: c# asp.net .net hash


【解决方案1】:

在查看 SqlMembershipProvider 的来源时,他们似乎在密码之前复制了盐

static byte[] GenerateSaltedHash(byte[] plainText, byte[] salt)
{
HashAlgorithm algorithm = new SHA256Managed();

byte[] plainTextWithSaltBytes = new byte[plainText.Length + salt.Length];
salt.CopyTo(plainTextWithSaltBytes, 0);
plainText.CopyTo(plainTextWithSaltBytes, salt.Length); 

byte[] hash = algorithm.ComputeHash(plainTextWithSaltBytes);

return hash;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 2017-11-25
    相关资源
    最近更新 更多