我使用Rfc2898DeriveBytes来哈希密码。
但是我不确定要传递给需要int的GetBytes方法的内容。
我应该传递什么价值以及为什么?
Rfc2898DeriveBytes hasher = new Rfc2898DeriveBytes(password, System.Text.Encoding.Default.GetBytes(salt), PasswordHasher.Iterations);
return Convert.ToBase64String(hasher.GetBytes(100));
答案 0 :(得分:7)
如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
基本上,您将获得所选长度的一致字节列表(基于密码,盐和迭代)。您可以随时从相同的输入重新生成完全相同的字节列表。