Generate key using Pass Phrase or AesCryptoServiceProvider?

不问归期 提交于 2020-01-04 09:28:08

问题


We using AES encryption to encrypt the data. Generating a key once in a year using some app(i.e console) so which way i have to choose to generate the key?

1. Do we need to generate the key using the Pass Phrase method?

2. Or we have to choose the default generate key provided by AESCryptoServiceProvider?

The below method uses Pass Phrase for generating the encryption.

Method 1:

private static readonly byte[] Salt = new byte[] { 10, 20, 30, 40, 50, 60, 70, 80 };

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

The below method uses AesCryptoServiceProvider for generating Key and we didn't use Pass Phrase

Method 2:

var Key = default(string);

using (var provider = new AesCryptoServiceProvider())
{
    provider.GenerateKey();
    Key = System.Text.Encoding.ASCII.GetString(provider.Key);
}

Edit:

can we use this Random Number generation method?

public static byte[] RandomNumberGenerator()
{
  var rngBytes = new byte[32];
  using (RandomNumberGenerator objNumberGenerator = new RNGCryptoServiceProvider())
  {
    objNumberGenerator.GetBytes(rngBytes);
    return rngBytes;
  }

}

来源:https://stackoverflow.com/questions/23288540/generate-key-using-pass-phrase-or-aescryptoserviceprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!