Export (0) Print
Expand All

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

Creates a symmetric 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 decryptor object.

ExceptionCondition
ArgumentNullException

key or iv is null.

ArgumentException

key is invalid.

The following example shows how to use the AesManaged.CreateDecryptor(Byte[], Byte[]) method to decrypt an encrypted message. This code example is part of a larger example provided for the AesManaged 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("Key");

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

    // Create an AesManaged object
    // with the specified key and IV.
    using (AesManaged aesAlg = new AesManaged())
    {
        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

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

ADD
Show:
© 2014 Microsoft