AesCryptoServiceProvider.CreateDecryptor Method

Definition

Creates a symmetric AES decryptor object.

Overloads

CreateDecryptor()

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

CreateDecryptor(Byte[], Byte[])

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

CreateDecryptor()

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

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

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

Returns

A symmetric AES decryptor object.

Attributes

Exceptions

The current key is invalid or missing.

Applies to

.NET 9 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
.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

CreateDecryptor(Byte[], Byte[])

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

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

C#
public override System.Security.Cryptography.ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[]? rgbIV);
C#
public override System.Security.Cryptography.ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);
C#
[System.Security.SecurityCritical]
public override System.Security.Cryptography.ICryptoTransform CreateDecryptor (byte[] key, byte[] iv);
C#
public override System.Security.Cryptography.ICryptoTransform CreateDecryptor (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 decryptor object.

Attributes

Exceptions

key or iv is null.

key is invalid.

Examples

The following example shows how to use the AesCryptoServiceProvider.CreateDecryptor method to decrypt an encrypted message. This code example is part of a larger example provided for the AesCryptoServiceProvider class.

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

    // Declare the string used to hold
    // the decrypted text.
    string plaintext = null;

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

        // Create a decryptor to perform the stream transform.
        ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

        // Create the streams used for decryption.
        using (MemoryStream msDecrypt = new MemoryStream(cipherText))
        {
            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            {
                using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                {

                    // Read the decrypted bytes from the decrypting stream
                    // and place them in a string.
                    plaintext = srDecrypt.ReadToEnd();
                }
            }
        }
    }

    return plaintext;
}

Applies to

.NET 9 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
.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