RijndaelManaged

RijndaelManaged 是 .NET Framework 中的一个类,用于实现 Rijndael 加密算法。Rijndael 是一种对称密钥加密算法,也是 AES(高级加密标准)的基础。RijndaelManaged 提供了对 Rijndael 算法的托管实现,允许开发者在应用程序中使用这种加密算法。
以下是一个使用 RijndaelManaged 进行加密和解密的示例代码:

示例代码

加密方法

csharp
复制
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public class RijndaelExample
{
    public static byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] iv)
    {
        // 检查输入参数
        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;

        // 创建一个 RijndaelManaged 对象
        using (RijndaelManaged rijndael = new RijndaelManaged())
        {
            // 设置密钥和初始化向量
            rijndael.Key = key;
            rijndael.IV = iv;

            // 创建一个加密器
            ICryptoTransform encryptor = rijndael.CreateEncryptor(rijndael.Key, rijndael.IV);

            // 使用加密器加密数据
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        // 写入明文
                        swEncrypt.Write(plainText);
                    }
                }
                encrypted = msEncrypt.ToArray();
            }
        }

        return encrypted;
    }
}
 

解密方法

csharp
复制
public static string DecryptStringFromBytes(byte[] cipherText, byte[] key, byte[] iv)
{
    // 检查输入参数
    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");

    string plaintext = null;

    // 创建一个 RijndaelManaged 对象
    using (RijndaelManaged rijndael = new RijndaelManaged())
    {
        // 设置密钥和初始化向量
        rijndael.Key = key;
        rijndael.IV = iv;

        // 创建一个解密器
        ICryptoTransform decryptor = rijndael.CreateDecryptor(rijndael.Key, rijndael.IV);

        // 使用解密器解密数据
        using (MemoryStream msDecrypt = new MemoryStream(cipherText))
        {
            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            {
                using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                {
                    // 读取解密后的明文
                    plaintext = srDecrypt.ReadToEnd();
                }
            }
        }
    }

    return plaintext;
}
 

测试代码

csharp
复制
public class Program
{
    public static void Main()
    {
        string original = "Hello World!";
        byte[] key = Encoding.UTF8.GetBytes("32位长度的密钥123456780000"); // 密钥长度必须是16/24/32
        byte[] iv = Encoding.UTF8.GetBytes("16位长度的IV"); // IV长度必须是16

        // 加密
        byte[] encrypted = RijndaelExample.EncryptStringToBytes(original, key, iv);
        Console.WriteLine("Encrypted: " + Convert.ToBase64String(encrypted));

        // 解密
        string decrypted = RijndaelExample.DecryptStringFromBytes(encrypted, key, iv);
        Console.WriteLine("Decrypted: " + decrypted);
    }
}
 

使用说明

  1. 密钥和IV:
    • 密钥(key)和初始化向量(IV)必须是正确的长度。对于 Rijndael,密钥长度可以是16、24或32字节,IV长度必须是16字节。
    • 在实际应用中,密钥和IV应该是安全生成和存储的,而不是硬编码在代码中。
  2. 加密和解密:
    • EncryptStringToBytes 方法用于加密字符串。
    • DecryptStringFromBytes 方法用于解密字节数组。
  3. 测试代码:
    • Main 方法提供了一个简单的测试示例,展示了如何使用 RijndaelManaged 进行加密和解密操作。

注意事项

  1. 安全性:
    • 密钥和IV的管理非常重要。在生产环境中,应使用安全的方式生成和存储密钥和IV,例如使用 Aes.Create() 方法生成密钥和IV。
  2. 性能:
    • RijndaelManaged 是一个托管实现,性能可能不如 Aes 类。在需要高性能的场景中,建议使用 Aes 类。
  3. 异常处理:
    • 在实际应用中,应添加适当的异常处理逻辑,以处理可能的错误情况,如密钥或IV长度不正确、加密或解密失败等。

总结

RijndaelManaged 是一个强大的工具,用于实现 Rijndael 加密算法。通过上述示例代码,你可以轻松地在应用程序中实现加密和解密功能。在使用时,注意密钥和IV的管理,确保加密操作的安全性。
posted @ 2025-08-20 13:48  yinghualeihenmei  阅读(16)  评论(0)    收藏  举报