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 Overrides Function CreateDecryptor (
	key As Byte(),
	iv As Byte()
) As ICryptoTransform

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.

    Shared Function DecryptStringFromBytes_Aes(ByVal cipherText() As Byte,ByVal Key() As Byte, ByVal IV() As Byte) As String
        ' Check arguments.
        If cipherText Is Nothing OrElse cipherText.Length <= 0 Then
            Throw New ArgumentNullException("cipherText")
        End If
        If Key Is Nothing OrElse Key.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If
        If IV Is Nothing OrElse IV.Length <= 0 Then
            Throw New ArgumentNullException("IV")
        End If
        ' Declare the string used to hold
        ' the decrypted text.
        Dim plaintext As String = Nothing

        ' Create an AesCryptoServiceProvider object
        ' with the specified key and IV.
        Using aesAlg As New AesCryptoServiceProvider()

            aesAlg.Key = Key
            aesAlg.IV = IV

            ' Create a decrytor to perform the stream transform.
            Dim decryptor As ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)

            ' Create the streams used for decryption.
            Using msDecrypt As New MemoryStream(cipherText)

                Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

                    Using srDecrypt As New StreamReader(csDecrypt)

                        ' Read the decrypted bytes from the decrypting stream
                        ' and place them in a string.
                        plaintext = srDecrypt.ReadToEnd()
                    End Using
                End Using
            End Using
        End Using
        Return plaintext

    End Function 'DecryptStringFromBytes_Aes 
End Class 'AesExample

.NET Framework
Available since 3.5
Return to top
Show: