CryptDeriveKey fails for AES algorithm name

后端 未结 2 653
北恋
北恋 2021-01-07 11:29

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         


        
2条回答
  •  盖世英雄少女心
    2021-01-07 12:35

    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;
    }
    

提交回复
热议问题