RFC2898DeriveBytes 如何生成 AES 密钥?
我看到了一些代码,就像
string password = "11111111";
byte[] salt = Encoding.ASCII.GetBytes("22222222");
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt);
RijndaelAlg.Key = key.GetBytes(RijndaelAlg.KeySize / 8);
我可以看到密钥是由 Rfc2898DeriveBytes 使用密码和盐生成的。然后 AES 通过 GetBytes 检索密钥。
但问题是,RFC2898DeriveBytes 是做什么的,key.GetBytes(cb) 是做什么的?有人能详细说明一下吗?我无法从文档中获取它。
I saw some code like
string password = "11111111";
byte[] salt = Encoding.ASCII.GetBytes("22222222");
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt);
RijndaelAlg.Key = key.GetBytes(RijndaelAlg.KeySize / 8);
I can see the key is generated by Rfc2898DeriveBytes with passphrase and salt. Then AES retrieves the key by GetBytes.
But the question is, what does RFC2898DeriveBytes do and what key.GetBytes(cb) do? Could anyone elaborate this? I couldn't get it from the documentation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
RFC2898 是指 2000 年 9 月发布的基于密码的加密规范。实际上,
Rfc2898DeriveBytes
使用密码和盐来生成密钥。它使用的方法称为 PBKDF2(基于密码的密钥导出函数 #2),并在 RFC2898 的第 5.2 节中定义。从第 5.2 节:有关更多详细信息,请参阅 RFC2898。
至于 Rfc2898DeriveBytes.GetBytes 的作用,它在每次调用时返回一个不同的密钥;它实际上只是使用相同的密码和盐以及迭代计数重复应用 PBKDF2。
RFC 文档中对此进行了概述,其中 PBKDF2 定义为
其中
P
是密码,S
是盐,c
是迭代计数,< code>dkLen 是所需密钥的长度。RFC 总的来说非常有趣,并且在历史上非常重要。 RFC 1149 非常重要,RFC 2324。
RFC2898 refers to a password-based cryptography specification published in September 2000. Effectively,
Rfc2898DeriveBytes
takes a password and salt to generate keys. The method it uses is known as PBKDF2 (Password Based Key Derivation Function #2) and is defined in section 5.2 of RFC2898. From section 5.2:For further details, see RFC2898.
As for what
Rfc2898DeriveBytes.GetBytes
does, it returns a different key on each invocation; it effectively just applies PBKDF2 repeatedly with the same password and salt but also an iteration count.This is outlined in RFC doc where PBKDF2 is defined as
where
P
is the password,S
is the salt,c
is the iteration count anddkLen
is the length of the desired key.The RFCs are in general very interesting and historically quite important. RFC 1149 is quite important, as is RFC 2324.
从 Reflector 中的实现来看:
From looking at the implementation in Reflector: