I\'m trying to implement AES encryption in my application. I have the following code to create a hashed version of the user password.
PasswordDeriveBytes pas
Why do you want to derive a key from a password salt rather than the password itself? Usually you use the "raw" password and a salt; indeed in my book (grin) chapter 6 has the following sample.
private void GetKeyAndIVFromPasswordAndSalt(
string password,
byte[] salt,
SymmetricAlgorithm symmetricAlgorithm,
ref byte[] key,
ref byte[] iv)
{
Rfc2898DeriveBytes rfc2898DeriveBytes =
new Rfc2898DeriveBytes(password, salt);
key = rfc2898DeriveBytes.GetBytes(symmetricAlgorithm.KeySize / 8);
iv = rfc2898DeriveBytes.GetBytes(symmetricAlgorithm.BlockSize / 8);
}
Of course salt should be a cryptographically secure random byte array;
private static byte[] GenerateKeyGenerateRandomBytes(int length)
{
byte[] key = new byte[length];
RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
provider.GetBytes(key);
return key;
}