Rijndael Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Caution
The Rijndael and RijndaelManaged types are obsolete. Use Aes instead.
Represents the base class from which all implementations of the Rijndael symmetric encryption algorithm must inherit.
public ref class Rijndael abstract : System::Security::Cryptography::SymmetricAlgorithm
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public abstract class Rijndael : System.Security.Cryptography.SymmetricAlgorithm
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Obsolete("The Rijndael and RijndaelManaged types are obsolete. Use Aes instead.", DiagnosticId="SYSLIB0022", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public abstract class Rijndael : System.Security.Cryptography.SymmetricAlgorithm
[System.Obsolete("The Rijndael and RijndaelManaged types are obsolete. Use Aes instead.", DiagnosticId="SYSLIB0022", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public abstract class Rijndael : System.Security.Cryptography.SymmetricAlgorithm
public abstract class Rijndael : System.Security.Cryptography.SymmetricAlgorithm
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Rijndael : System.Security.Cryptography.SymmetricAlgorithm
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
type Rijndael = class
inherit SymmetricAlgorithm
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[<System.Obsolete("The Rijndael and RijndaelManaged types are obsolete. Use Aes instead.", DiagnosticId="SYSLIB0022", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
type Rijndael = class
inherit SymmetricAlgorithm
[<System.Obsolete("The Rijndael and RijndaelManaged types are obsolete. Use Aes instead.", DiagnosticId="SYSLIB0022", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
type Rijndael = class
inherit SymmetricAlgorithm
type Rijndael = class
inherit SymmetricAlgorithm
[<System.Runtime.InteropServices.ComVisible(true)>]
type Rijndael = class
inherit SymmetricAlgorithm
Public MustInherit Class Rijndael
Inherits SymmetricAlgorithm
- Inheritance
- Derived
- Attributes
Examples
The following code example uses the Rijndael class to encrypt and then decrypt data.
using System;
using System.IO;
using System.Security.Cryptography;
namespace RijndaelManaged_Example
{
class RijndaelExample
{
public static void Main()
{
try
{
string original = "Here is some data to encrypt!";
// Create a new instance of the Rijndael
// class. This generates a new key and initialization
// vector (IV).
using (Rijndael myRijndael = Rijndael.Create())
{
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes(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 (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
}
static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an Rijndael object
// with the specified key and IV.
using (Rijndael rijAlg = Rijndael.Create())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
static string DecryptStringFromBytes(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 Rijndael object
// with the specified key and IV.
using (Rijndael rijAlg = Rijndael.Create())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.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;
}
}
}
Imports System.IO
Imports System.Security.Cryptography
Class RijndaelExample
Public Shared Sub Main()
Try
Dim original As String = "Here is some data to encrypt!"
' Create a new instance of the Rijndael
' class. This generates a new key and initialization
' vector (IV).
Using myRijndael = Rijndael.Create()
' Encrypt the string to an array of bytes.
Dim encrypted As Byte() = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV)
' Decrypt the bytes to a string.
Dim roundtrip As String = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV)
'Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original)
Console.WriteLine("Round Trip: {0}", roundtrip)
End Using
Catch e As Exception
Console.WriteLine("Error: {0}", e.Message)
End Try
End Sub
Shared Function EncryptStringToBytes(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 Rijndael object
' with the specified key and IV.
Using rijAlg = Rijndael.Create()
rijAlg.Key = Key
rijAlg.IV = IV
' Create an encryptor to perform the stream transform.
Dim encryptor As ICryptoTransform = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV)
' Create the streams used for encryption.
Using 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
End Using
' Return the encrypted bytes from the memory stream.
Return encrypted
End Function 'EncryptStringToBytes
Shared Function DecryptStringFromBytes(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 Rijndael object
' with the specified key and IV.
Using rijAlg = Rijndael.Create()
rijAlg.Key = Key
rijAlg.IV = IV
' Create a decryptor to perform the stream transform.
Dim decryptor As ICryptoTransform = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.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
End Class
Remarks
This algorithm supports key lengths of 128, 192, or 256 bits; defaulting to 256 bits. This algorithm supports block sizes of 128, 192, or 256 bits; defaulting to 128 bits (Aes-compatible).
Important
The Rijndael class is the predecessor of the Aes algorithm. You should use the Aes algorithm instead of Rijndael. For more information, see the entry The Differences Between Rijndael and AES in the .NET Security blog.
Constructors
Rijndael() |
Initializes a new instance of Rijndael. |
Fields
BlockSizeValue |
Represents the block size, in bits, of the cryptographic operation. (Inherited from SymmetricAlgorithm) |
FeedbackSizeValue |
Represents the feedback size, in bits, of the cryptographic operation. (Inherited from SymmetricAlgorithm) |
IVValue |
Represents the initialization vector (IV) for the symmetric algorithm. (Inherited from SymmetricAlgorithm) |
KeySizeValue |
Represents the size, in bits, of the secret key used by the symmetric algorithm. (Inherited from SymmetricAlgorithm) |
KeyValue |
Represents the secret key for the symmetric algorithm. (Inherited from SymmetricAlgorithm) |
LegalBlockSizesValue |
Specifies the block sizes, in bits, that are supported by the symmetric algorithm. (Inherited from SymmetricAlgorithm) |
LegalKeySizesValue |
Specifies the key sizes, in bits, that are supported by the symmetric algorithm. (Inherited from SymmetricAlgorithm) |
ModeValue |
Represents the cipher mode used in the symmetric algorithm. (Inherited from SymmetricAlgorithm) |
PaddingValue |
Represents the padding mode used in the symmetric algorithm. (Inherited from SymmetricAlgorithm) |
Properties
BlockSize |
Gets or sets the block size, in bits, of the cryptographic operation. (Inherited from SymmetricAlgorithm) |
FeedbackSize |
Gets or sets the feedback size, in bits, of the cryptographic operation for the Cipher Feedback (CFB) and Output Feedback (OFB) cipher modes. (Inherited from SymmetricAlgorithm) |
IV |
Gets or sets the initialization vector (IV) for the symmetric algorithm. (Inherited from SymmetricAlgorithm) |
Key |
Gets or sets the secret key for the symmetric algorithm. (Inherited from SymmetricAlgorithm) |
KeySize |
Gets or sets the size, in bits, of the secret key used by the symmetric algorithm. (Inherited from SymmetricAlgorithm) |
LegalBlockSizes |
Gets the block sizes, in bits, that are supported by the symmetric algorithm. (Inherited from SymmetricAlgorithm) |
LegalKeySizes |
Gets the key sizes, in bits, that are supported by the symmetric algorithm. (Inherited from SymmetricAlgorithm) |
Mode |
Gets or sets the mode for operation of the symmetric algorithm. (Inherited from SymmetricAlgorithm) |
Padding |
Gets or sets the padding mode used in the symmetric algorithm. (Inherited from SymmetricAlgorithm) |
Methods
Clear() |
Releases all resources used by the SymmetricAlgorithm class. (Inherited from SymmetricAlgorithm) |
Create() |
Creates a cryptographic object to perform the Rijndael algorithm. |
Create(String) |
Obsolete.
Creates a cryptographic object to perform the specified implementation of the Rijndael algorithm. |
CreateDecryptor() |
Creates a symmetric decryptor object with the current Key property and initialization vector (IV). (Inherited from SymmetricAlgorithm) |
CreateDecryptor(Byte[], Byte[]) |
When overridden in a derived class, creates a symmetric decryptor object with the specified Key property and initialization vector (IV). (Inherited from SymmetricAlgorithm) |
CreateEncryptor() |
Creates a symmetric encryptor object with the current Key property and initialization vector (IV). (Inherited from SymmetricAlgorithm) |
CreateEncryptor(Byte[], Byte[]) |
When overridden in a derived class, creates a symmetric encryptor object with the specified Key property and initialization vector (IV). (Inherited from SymmetricAlgorithm) |
DecryptCbc(Byte[], Byte[], PaddingMode) |
Decrypts data using CBC mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
DecryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, PaddingMode) |
Decrypts data using CBC mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
DecryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode) |
Decrypts data into the specified buffer, using CBC mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
DecryptCfb(Byte[], Byte[], PaddingMode, Int32) |
Decrypts data using CFB mode with the specified padding mode and feedback size. (Inherited from SymmetricAlgorithm) |
DecryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, PaddingMode, Int32) |
Decrypts data using CFB mode with the specified padding mode and feedback size. (Inherited from SymmetricAlgorithm) |
DecryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32) |
Decrypts data into the specified buffer, using CFB mode with the specified padding mode and feedback size. (Inherited from SymmetricAlgorithm) |
DecryptEcb(Byte[], PaddingMode) |
Decrypts data using ECB mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
DecryptEcb(ReadOnlySpan<Byte>, PaddingMode) |
Decrypts data using ECB mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
DecryptEcb(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode) |
Decrypts data into the specified buffer, using ECB mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
Dispose() |
Releases all resources used by the current instance of the SymmetricAlgorithm class. (Inherited from SymmetricAlgorithm) |
Dispose(Boolean) |
Releases the unmanaged resources used by the SymmetricAlgorithm and optionally releases the managed resources. (Inherited from SymmetricAlgorithm) |
EncryptCbc(Byte[], Byte[], PaddingMode) |
Encrypts data using CBC mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
EncryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, PaddingMode) |
Encrypts data using CBC mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
EncryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode) |
Encrypts data into the specified buffer, using CBC mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
EncryptCfb(Byte[], Byte[], PaddingMode, Int32) |
Encrypts data using CFB mode with the specified padding mode and feedback size. (Inherited from SymmetricAlgorithm) |
EncryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, PaddingMode, Int32) |
Encrypts data using CFB mode with the specified padding mode and feedback size. (Inherited from SymmetricAlgorithm) |
EncryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32) |
Encrypts data into the specified buffer, using CFB mode with the specified padding mode and feedback size. (Inherited from SymmetricAlgorithm) |
EncryptEcb(Byte[], PaddingMode) |
Encrypts data using ECB mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
EncryptEcb(ReadOnlySpan<Byte>, PaddingMode) |
Encrypts data using ECB mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
EncryptEcb(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode) |
Encrypts data into the specified buffer, using ECB mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
GenerateIV() |
When overridden in a derived class, generates a random initialization vector (IV) to use for the algorithm. (Inherited from SymmetricAlgorithm) |
GenerateKey() |
When overridden in a derived class, generates a random key (Key) to use for the algorithm. (Inherited from SymmetricAlgorithm) |
GetCiphertextLengthCbc(Int32, PaddingMode) |
Gets the length of a ciphertext with a given padding mode and plaintext length in CBC mode. (Inherited from SymmetricAlgorithm) |
GetCiphertextLengthCfb(Int32, PaddingMode, Int32) |
Gets the length of a ciphertext with a given padding mode and plaintext length in CFB mode. (Inherited from SymmetricAlgorithm) |
GetCiphertextLengthEcb(Int32, PaddingMode) |
Gets the length of a ciphertext with a given padding mode and plaintext length in ECB mode. (Inherited from SymmetricAlgorithm) |
GetHashCode() |
Serves as the default hash function. (Inherited from Object) |
GetType() |
Gets the Type of the current instance. (Inherited from Object) |
MemberwiseClone() |
Creates a shallow copy of the current Object. (Inherited from Object) |
ToString() |
Returns a string that represents the current object. (Inherited from Object) |
TryDecryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Int32, PaddingMode) |
Attempts to decrypt data into the specified buffer, using CBC mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
TryDecryptCbcCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32) |
When overridden in a derived class, attempts to decrypt data into the specified buffer, using CBC mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
TryDecryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Int32, PaddingMode, Int32) |
Attempts to decrypt data into the specified buffer, using CFB mode with the specified padding mode and feedback size. (Inherited from SymmetricAlgorithm) |
TryDecryptCfbCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32, Int32) |
When overridden in a derived class, attempts to decrypt data into the specified buffer, using CFB mode with the specified padding mode and feedback size. (Inherited from SymmetricAlgorithm) |
TryDecryptEcb(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32) |
Attempts to decrypt data into the specified buffer, using ECB mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
TryDecryptEcbCore(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32) |
When overridden in a derived class, attempts to decrypt data into the specified buffer, using ECB mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
TryEncryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Int32, PaddingMode) |
Attempts to encrypt data into the specified buffer, using CBC mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
TryEncryptCbcCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32) |
When overridden in a derived class, attempts to encrypt data into the specified buffer, using CBC mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
TryEncryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Int32, PaddingMode, Int32) |
Attempts to encrypt data into the specified buffer, using CFB mode with the specified padding mode and feedback size. (Inherited from SymmetricAlgorithm) |
TryEncryptCfbCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32, Int32) |
When overridden in a derived class, attempts to encrypt data into the specified buffer, using CFB mode with the specified padding mode and feedback size. (Inherited from SymmetricAlgorithm) |
TryEncryptEcb(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32) |
Attempts to encrypt data into the specified buffer, using ECB mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
TryEncryptEcbCore(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32) |
When overridden in a derived class, attempts to encrypt data into the specified buffer, using ECB mode with the specified padding mode. (Inherited from SymmetricAlgorithm) |
ValidKeySize(Int32) |
Determines whether the specified key size is valid for the current algorithm. (Inherited from SymmetricAlgorithm) |
Explicit Interface Implementations
IDisposable.Dispose() |
This API supports the product infrastructure and is not intended to be used directly from your code. Releases the unmanaged resources used by the SymmetricAlgorithm and optionally releases the managed resources. (Inherited from SymmetricAlgorithm) |