繁体   English   中英

密码哈希通过Rfc2898DeriveBytes - 传递给getBytes的内容

[英]Password Hash via Rfc2898DeriveBytes - what to pass to getBytes

我使用Rfc2898DeriveBytes来哈希密码。

但是我不确定要传递给需要int的GetBytes方法的内容。

我应该传递什么价值?为什么?

   Rfc2898DeriveBytes hasher = new Rfc2898DeriveBytes(password, System.Text.Encoding.Default.GetBytes(salt), PasswordHasher.Iterations);
      return Convert.ToBase64String(hasher.GetBytes(100));

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rfc2898derivebytes.getbytes.aspx中所述,GetBytes的参数是您希望GetBytes方法为您生成的字节数。 如果你想要5个字节,则传递5.如果你想要500,则传递500.你要求的字节数通常取决于你所生成的密钥(或其他加密输入)的预期用途需要多少。

要更好地理解输出,请尝试运行以下命令行应用程序:

internal static class Program
{
    private static void Main()
    {
        var deriver = new Rfc2898DeriveBytes("apples and oranges", 100, 20);
        Program.WriteArray(deriver, 5);
        Program.WriteArray(deriver, 10);
        Program.WriteArray(deriver, 20);

        Console.ReadLine();
    }

    private static void WriteArray(Rfc2898DeriveBytes source, int count)
    {
        source.Reset();
        Console.WriteLine(string.Join(" ", source.GetBytes(count).Select(b => b.ToString())));
    }
}

输出应如下所示:

208 194 113 91 125
208 194 113 91 125 157 138 234 20 151
208 194 113 91 125 157 138 234 20 151 159 151 23 94 11 210 38 101 186 143

从本质上讲,您将获得所选长度的一致字节列表(基于密码,盐和迭代)。 您可以随时从相同的输入重新生成完全相同的字节列表。

暂无
暂无

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

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