RijndaelManaged类

构造函数
名称 说明
System_CAPS_pubmethod RijndaelManaged()
初始化 RijndaelManaged 类的新实例。

属性
名称 说明
System_CAPS_pubproperty BlockSize
获取或设置加密操作的块大小(以位为单位)。(继承自 SymmetricAlgorithm。)

System_CAPS_pubproperty FeedbackSize
获取或设置加密操作的反馈大小(以位为单位)。(继承自 SymmetricAlgorithm。)

System_CAPS_pubproperty IV
获取或设置对称算法的初始化向量 (IV)。(继承自 SymmetricAlgorithm。)

System_CAPS_pubproperty Key
获取或设置对称算法的密钥。(继承自 SymmetricAlgorithm。)

System_CAPS_pubproperty KeySize
获取或设置对称算法所用密钥的大小(以位为单位)。(继承自 SymmetricAlgorithm。)

System_CAPS_pubproperty LegalBlockSizes
获取对称算法支持的块大小(以位为单位)。(继承自 SymmetricAlgorithm。)

System_CAPS_pubproperty LegalKeySizes
获取对称算法支持的密钥大小(以位为单位)。(继承自 SymmetricAlgorithm。)

System_CAPS_pubproperty Mode
获取或设置对称算法的运算模式。(继承自 SymmetricAlgorithm。)

System_CAPS_pubproperty Padding
获取或设置对称算法中使用的填充模式。(继承自 SymmetricAlgorithm。)

方法
名称 说明
System_CAPS_pubmethod Clear()
释放 SymmetricAlgorithm 类使用的所有资源。(继承自 SymmetricAlgorithm。)

System_CAPS_pubmethod CreateDecryptor()
用当前的 Key 属性和初始化向量 (IV) 创建对称解密器对象。(继承自 SymmetricAlgorithm。)

System_CAPS_pubmethod CreateDecryptor(Byte[], Byte[])
创建对称 Rijndael 解密器对象具有指定 Key 和初始化向量 (IV)。(覆盖SymmetricAlgorithm.CreateDecryptor(Byte[], Byte[])。)

System_CAPS_pubmethod CreateEncryptor()
用当前的 Key 属性和初始化向量 (IV) 创建对称加密器对象。(继承自 SymmetricAlgorithm。)

System_CAPS_pubmethod CreateEncryptor(Byte[], Byte[])
创建对称 Rijndael 加密器对象具有指定 Key 和初始化向量 (IV)。(覆盖SymmetricAlgorithm.CreateEncryptor(Byte[], Byte[])。)

System_CAPS_pubmethod Dispose()
释放 SymmetricAlgorithm 类的当前实例所使用的所有资源。(继承自 SymmetricAlgorithm。)

System_CAPS_pubmethod Equals(Object)
确定指定的对象是否等于当前对象。(继承自 Object。)

System_CAPS_pubmethod GenerateIV()
生成的随机初始化向量 (IV) 要用于该算法。(覆盖 SymmetricAlgorithm.GenerateIV()。)

System_CAPS_pubmethod GenerateKey()
生成的随机 Key 要用于该算法。(覆盖 SymmetricAlgorithm.GenerateKey()。)

System_CAPS_pubmethod GetHashCode()
作为默认哈希函数。(继承自 Object。)

System_CAPS_pubmethod GetType()
获取当前实例的 Type。(继承自 Object。)

System_CAPS_pubmethod ToString()
返回表示当前对象的字符串。(继承自 Object。)

System_CAPS_pubmethod ValidKeySize(Int32)
确定指定的密钥大小对当前算法是否有效。(继承自 SymmetricAlgorithm。)

备注
此算法支持 128、 192 或 256 位的密钥长度。

示例
下面的示例演示如何进行加密和解密使用示例数据 RijndaelManaged 类。

C#
C++
VB
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 RijndaelManaged
// class. This generates a new key and initialization
// vector (IV).
using (RijndaelManaged myRijndael = new RijndaelManaged())
{
myRijndael.GenerateKey();
myRijndael.GenerateIV();
// 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 RijndaelManaged object // with the specified key and IV. using (RijndaelManaged rijAlg = new RijndaelManaged()) { rijAlg.Key = Key; rijAlg.IV = IV; // Create a decrytor 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 RijndaelManaged object // with the specified key and IV. using (RijndaelManaged rijAlg = new RijndaelManaged()) { rijAlg.Key = Key; rijAlg.IV = IV; // Create a decrytor 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; } } } 版本信息 .NET Framework 自 1.1 起可用 线程安全 此类型的所有公共静态(Visual Basic 中的 已共享 在 Visual Basic 中)成员都是线程安全的。不保证所有实例成员都是线程安全的。 另请参阅 System.Security.Cryptography 命名空间 加密服务 from:https://msdn.microsoft.com/zh-cn/library/system.security.cryptography.rijndaelmanaged(v=vs.110).aspx