问题
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