This documentation is archived and is not being maintained.

AesCryptoServiceProvider.CreateEncryptor Method (Byte[], Byte[])

Updated: March 2011

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

Namespace:  System.Security.Cryptography
Assembly:  System.Core (in System.Core.dll)

public override ICryptoTransform CreateEncryptor(
	byte[] key,
	byte[] iv


Type: System.Byte[]

The secret key to use for the symmetric algorithm.

Type: System.Byte[]

The initialization vector to use for the symmetric algorithm.

Return Value

Type: System.Security.Cryptography.ICryptoTransform
A symmetric AES encryptor object.


The key or iv parameter is null.

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

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

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("Key");
    byte[] encrypted;
    // Create an AesCryptoServiceProvider object 
    // with the specified key and IV. 
    using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
        aesAlg.Key = Key;
        aesAlg.IV = IV;

        // Create a decrytor 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.
                encrypted = msEncrypt.ToArray();

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


Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5




March 2011

Added example.

Information enhancement.