AesManaged.CreateEncryptor Method

Definition

Creates a symmetric encryptor object.

Overloads

CreateEncryptor()

Creates a symmetric 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:
AesManaged.cs
Source:
AesManaged.cs
Source:
AesManaged.cs

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

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

Returns

A symmetric encryptor object.

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:
AesManaged.cs
Source:
AesManaged.cs
Source:
AesManaged.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#
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 encryptor object.

Exceptions

key or iv is null.

key is invalid.

Examples

The following example shows how to use the CreateEncryptor method to encrypt a message. This code example is part of a larger example provided for the AesManaged 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 AesManaged object
    // with the specified key and IV.
    using (AesManaged aesAlg = new AesManaged())
    {
        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;
}

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