Reputation: 3351
Most of the examples that show how to encrypt using the AES code in System.Security.Cryptography look like this:
using (Aes aes = Aes.Create())
{
aes.Key = Key;
aes.IV = IV;
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
Is it necessary to specify the key and IV twice - once to the Aes object and once to the CreateEncryptor() function? Or in this case, since I've already specified the key and IV, can I call the form of CreateEncryptor() that takes no arguments?
Upvotes: 3
Views: 590
Reputation: 199
It depends on how you use Aes and encryptor instance. To create encryptor, You can just write the following code without using the 2 assignment statements:
using System.Security.Cryptography;
ICryptoTransform encryptor = aes.CreateEncryptor(Key, IV);
Upvotes: 2
Reputation: 51330
You don't have to. You can use the CreateEncryptor()
overload (without parameters), if you used the properties to specify the values:
public virtual ICryptoTransform CreateEncryptor() {
return CreateEncryptor(Key, IV);
}
Here's what the doc says:
Creates a symmetric encryptor object with the current Key property and initialization vector (IV).
Or you can omit setting the properties and use the overload with arguments if you don't use the aes
object afterwards to create other encryptors/decryptors.
Upvotes: 7