生成AES 256位密钥值

bhs*_*bhs 7 c# hash aes encryption-symmetric

有没有人知道从任何长度的密码短语生成256位密钥值的方法?加密不能被加密,因为需要再次生成加密值并在数据库中进行比较.因此,每次加密时,值必须生成相同的加密字符串.

目前我正在使用一个32字符键来处理可能不正确的假设这是256位?

那么,我想要将"快速棕色狐狸"转换为合适的AES 256位密钥?

Use*_*678 17

您可以Rfc2898DeriveBytes Class使用任意大小的密码构造 ,然后在这种情况下派生所需大小的密钥,256位(32字节):

private static byte[] CreateKey(string password, int keyBytes = 32)
{
    const int Iterations = 300;
    var keyGenerator = new Rfc2898DeriveBytes(password, Salt, Iterations);
    return keyGenerator.GetBytes(keyBytes);
}
Run Code Online (Sandbox Code Playgroud)

为了产生确定性输出(即相同的输入将产生相同的输出),您将需要对盐进行硬编码.盐必须至少为8个字节:

private static readonly byte[] Salt = 
    new byte[] { 10, 20, 30 , 40, 50, 60, 70, 80};
Run Code Online (Sandbox Code Playgroud)