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

 

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

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

public override ICryptoTransform CreateDecryptor(
	byte[] key,
	byte[] iv
)

Parameters

key
Type: System.Byte[]

The secret key to use for the symmetric algorithm.

iv
Type: System.Byte[]

The initialization vector to use for the symmetric algorithm.

Return Value

Type: System.Security.Cryptography.ICryptoTransform

A symmetric AES decryptor object.

Exception Condition
ArgumentNullException

key or iv is null.

ArgumentException

key is invalid.

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.

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 decrytor 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;

}

.NET Framework
Available since 3.5
Return to top
Show: