Export (0) Print
Expand All

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

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

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

'Declaration
Public Overrides Function CreateEncryptor ( _
	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 encryptor object.

ExceptionCondition
ArgumentNullException

The key or iv parameter is Nothing.

ArgumentException

key is invalid.

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 method to encrypt a message. This code example is part of a larger example provided for the AesCryptoServiceProvider class.

    Shared Function EncryptStringToBytes_Aes(ByVal plainText As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte() 
        ' Check arguments. 
        If plainText Is Nothing OrElse plainText.Length <= 0 Then 
            Throw New ArgumentNullException("plainText")
        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 
        Dim encrypted() As Byte 
        ' 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 encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)

            ' Create the streams used for encryption. 
            Dim msEncrypt As New MemoryStream()
            Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                Using swEncrypt As New StreamWriter(csEncrypt)
                    'Write all data to the stream.
                    swEncrypt.Write(plainText)
                End Using
                encrypted = msEncrypt.ToArray()

            End Using 
        End Using 

        ' Return the encrypted bytes from the memory stream. 
        Return encrypted

    End Function 'EncryptStringToBytes_Aes

.NET Framework

Supported in: 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft