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.
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.