The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
RijndaelManaged Class
.NET Framework 3.0
Accesses the managed version of the Rijndael algorithm. This class cannot be inherited.
Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)
System.Security.Cryptography Namespace
Assembly: mscorlib (in mscorlib.dll)
'Declaration <ComVisibleAttribute(True)> _ Public NotInheritable Class RijndaelManaged Inherits Rijndael 'Usage Dim instance As RijndaelManaged
/** @attribute ComVisibleAttribute(true) */ public final class RijndaelManaged extends Rijndael
ComVisibleAttribute(true) public final class RijndaelManaged extends Rijndael
Not applicable.
This algorithm supports key lengths of 128, 192, or 256 bits.
| Topic | Location |
|---|---|
| How to: Decrypt XML Elements with Symmetric Keys | .NET Framework: Security |
| How to: Encrypt XML Elements with Symmetric Keys | .NET Framework: Security |
| How to: Decrypt XML Elements with Symmetric Keys | .NET Framework: Security |
| How to: Encrypt XML Elements with Symmetric Keys | .NET Framework: Security |
The following example demonstrates how to encrypt and decrypt sample data using the RijndaelManaged class.
Imports System Imports System.IO Imports System.Security.Cryptography Module RijndaelMemoryExample Sub Main() Try Dim original As String = "Here is some data to encrypt!" ' Create a new instance of the RijndaelManaged ' class. This generates a new key and initialization ' vector (IV). Dim myRijndael As New RijndaelManaged() ' Encrypt the string to an array of bytes. Dim encrypted As Byte() = encryptStringToBytes_AES(original, myRijndael.Key, myRijndael.IV) ' Decrypt the bytes to a string. Dim roundtrip As String = decryptStringFromBytes_AES(encrypted, myRijndael.Key, myRijndael.IV) 'Display the original data and the decrypted data. Console.WriteLine("Original: {0}", original) Console.WriteLine("Round Trip: {0}", roundtrip) Catch e As Exception Console.WriteLine("Error: {0}", e.Message) End Try End Sub 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("Key") End If ' Declare the streams used ' to encrypt to an in memory ' array of bytes. Dim msEncrypt As MemoryStream = Nothing Dim csEncrypt As CryptoStream = Nothing Dim swEncrypt As StreamWriter = Nothing ' Declare the RijndaelManaged object ' used to encrypt the data. Dim aesAlg As RijndaelManaged = Nothing ' Declare the bytes used to hold the ' encrypted data. Dim encrypted As Byte() = Nothing Try ' Create a RijndaelManaged object ' with the specified key and IV. aesAlg = New RijndaelManaged() 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. msEncrypt = New MemoryStream() csEncrypt = New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write) swEncrypt = New StreamWriter(csEncrypt) 'Write all data to the stream. swEncrypt.Write(plainText) Finally ' Clean things up. ' Close the streams. If Not (swEncrypt Is Nothing) Then swEncrypt.Close() End If If Not (csEncrypt Is Nothing) Then csEncrypt.Close() End If If Not (msEncrypt Is Nothing) Then msEncrypt.Close() End If ' Clear the RijndaelManaged object. If Not (aesAlg Is Nothing) Then aesAlg.Clear() End If End Try ' Return the encrypted bytes from the memory stream. Return msEncrypt.ToArray() End Function 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("Key") End If ' TDeclare the streams used ' to decrypt to an in memory ' array of bytes. Dim msDecrypt As MemoryStream = Nothing Dim csDecrypt As CryptoStream = Nothing Dim srDecrypt As StreamReader = Nothing ' Declare the RijndaelManaged object ' used to decrypt the data. Dim aesAlg As RijndaelManaged = Nothing ' Declare the string used to hold ' the decrypted text. Dim plaintext As String = Nothing Try ' Create a RijndaelManaged object ' with the specified key and IV. aesAlg = New RijndaelManaged() 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. msDecrypt = New MemoryStream(cipherText) csDecrypt = New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read) srDecrypt = New StreamReader(csDecrypt) ' Read the decrypted bytes from the decrypting stream ' and place them in a string. plaintext = srDecrypt.ReadToEnd() Finally ' Clean things up. ' Close the streams. If Not (srDecrypt Is Nothing) Then srDecrypt.Close() End If If Not (csDecrypt Is Nothing) Then csDecrypt.Close() End If If Not (msDecrypt Is Nothing) Then msDecrypt.Close() End If ' Clear the RijndaelManaged object. If Not (aesAlg Is Nothing) Then aesAlg.Clear() End If End Try Return plaintext End Function End Module
System.Object
System.Security.Cryptography.SymmetricAlgorithm
System.Security.Cryptography.Rijndael
System.Security.Cryptography.RijndaelManaged
System.Security.Cryptography.SymmetricAlgorithm
System.Security.Cryptography.Rijndael
System.Security.Cryptography.RijndaelManaged
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Reference
RijndaelManaged MembersSystem.Security.Cryptography Namespace
Other Resources
Cryptographic ServicesCommunity Additions
Show: