AesCryptoServiceProvider.CreateEncryptor Method

Definition

Creates a symmetric AES encryptor object.

Overloads

CreateEncryptor()

Creates a symmetric AES encryptor object using the current key and initialization vector (IV).

CreateEncryptor(Byte[], Byte[])

Creates a symmetric encryptor object using the specified key and initialization vector (IV).

CreateEncryptor()

Source:
AesCryptoServiceProvider.cs
Source:
AesCryptoServiceProvider.cs
Source:
AesCryptoServiceProvider.cs

Creates a symmetric AES encryptor object using the current key and initialization vector (IV).

C#
public override System.Security.Cryptography.ICryptoTransform CreateEncryptor();
C#
[System.Security.SecurityCritical]
public override System.Security.Cryptography.ICryptoTransform CreateEncryptor();

Returns

A symmetric AES encryptor object.

Attributes

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

CreateEncryptor(Byte[], Byte[])

Source:
AesCryptoServiceProvider.cs
Source:
AesCryptoServiceProvider.cs
Source:
AesCryptoServiceProvider.cs

Creates a symmetric encryptor object using the specified key and initialization vector (IV).

C#
public override System.Security.Cryptography.ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV);
C#
public override System.Security.Cryptography.ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV);
C#
[System.Security.SecurityCritical]
public override System.Security.Cryptography.ICryptoTransform CreateEncryptor(byte[] key, byte[] iv);
C#
public override System.Security.Cryptography.ICryptoTransform CreateEncryptor(byte[] key, byte[] iv);

Parameters

rgbKeykey
Byte[]

The secret key to use for the symmetric algorithm.

rgbIViv
Byte[]

The initialization vector to use for the symmetric algorithm.

Returns

A symmetric AES encryptor object.

Attributes

Exceptions

The key or iv parameter is null.

key is invalid.

Examples

The following example shows how to use the AesCryptoServiceProvider.CreateEncryptor method to encrypt a message. This code example is part of a larger example provided for the AesCryptoServiceProvider class.

C#
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
    // Check arguments.
    if (plainText == null || plainText.Length <= 0)
        throw new ArgumentNullException("plainText");
    if (Key == null || Key.Length <= 0)
        throw new ArgumentNullException("Key");
    if (IV == null || IV.Length <= 0)
        throw new ArgumentNullException("IV");
    byte[] encrypted;

    // Create an AesCryptoServiceProvider object
    // with the specified key and IV.
    using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
    {
        aesAlg.Key = Key;
        aesAlg.IV = IV;

        // Create an encryptor to perform the stream transform.
        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

        // Create the streams used for encryption.
        using (MemoryStream msEncrypt = new MemoryStream())
        {
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {
                    //Write all data to the stream.
                    swEncrypt.Write(plainText);
                }
            }

            encrypted = msEncrypt.ToArray();
        }
    }

    // Return the encrypted bytes from the memory stream.
    return encrypted;
}

Remarks

See the code examples in the LegalKeySizes and LegalBlockSizes properties to determine the size of the key and iv parameters.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1