常用属性与方法
AesManaged类提供了许多属性和方法,以下是一些常用的:
- Key:获取或设置加密算法的密钥。
- IV:获取或设置加密算法的初始化向量。
- BlockSize:获取加密算法的块大小(以位为单位)。
- KeySize:获取或设置加密算法的密钥大小(以位为单位)。
- Padding:获取或设置加密算法的填充模式。
- CreateEncryptor():创建用于加密数据的ICryptoTransform对象。
- CreateDecryptor():创建用于解密数据的ICryptoTransform对象。
基本用法
以下是一个简单的示例代码,演示了如何使用AES加密和解密数据:
static void Main()
{
// 原始文本
string plainText = "Hello, world!";
// 生成随机的密钥和初始化向量
byte[] key = GenerateRandomKey();
byte[] iv = GenerateRandomIV();
// 加密数据
byte[] encryptedData = Encrypt(Encoding.UTF8.GetBytes(plainText), key, iv);
// 解密数据
string decryptedText = Decrypt(encryptedData, key, iv);
// 输出结果
Console.WriteLine("Original Text: " + plainText);
Console.WriteLine("Encrypted Data: " + Convert.ToBase64String(encryptedData));
Console.WriteLine("Decrypted Text: " + decryptedText);
}
// 生成随机的密钥
static byte[] GenerateRandomKey()
{
using (AesManaged aes = new AesManaged())
{
aes.GenerateKey();
return aes.Key;
}
}
// 生成随机的初始化向量
static byte[] GenerateRandomIV()
{
using (AesManaged aes = new AesManaged())
{
aes.GenerateIV();
return aes.IV;
}
}
// 使用指定的密钥和初始化向量加密数据
static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
{
using (AesManaged aes = new AesManaged())
{
aes.Key = key;
aes.IV = iv;
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
return memoryStream.ToArray();
}
}
}
}
// 使用指定的密钥和初始化向量解密数据
static string Decrypt(byte[] data, byte[] key, byte[] iv)
{
using (AesManaged aes = new AesManaged())
{
aes.Key = key;
aes.IV = iv;
using (MemoryStream memoryStream = new MemoryStream(data))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Read))
{
byte[] decryptedData = new byte[data.Length];
int bytesRead = cryptoStream.Read(decryptedData, 0, decryptedData.Length);
return Encoding.UTF8.GetString(decryptedData, 0, bytesRead);
}
}
}
}
图片
AES通用类
public class AesHelper
{
private byte[] key;
private byte[] iv;
public AesHelper(byte[] key, byte[] iv)
{
this.key = key;
this.iv = iv;
}
public byte[] Encrypt(byte[] data)
{
using (AesManaged aes = new AesManaged())
{
aes.Key = key;
aes.IV = iv;
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
return memoryStream.ToArray();
}
}
}
}
public byte[] Decrypt(byte[] data)
{
using (AesManaged aes = new AesManaged())
{
aes.Key = key;
aes.IV = iv;
using (MemoryStream memoryStream = new MemoryStream(data))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Read))
{
byte[] decryptedData = new byte[data.Length];
int bytesRead = cryptoStream.Read(decryptedData, 0, decryptedData.Length);
return decryptedData[..bytesRead];
}
}
}
}
}
static void Main()
{
byte[] key = Encoding.UTF8.GetBytes("MySecretKey12345");
byte[] iv = Encoding.UTF8.GetBytes("1234567890ABCDEF");
AesHelper aesHelper = new AesHelper(key, iv);
string plainText = "Hello, world!";
byte[] encryptedData = aesHelper.Encrypt(Encoding.UTF8.GetBytes(plainText));
byte[] decryptedData = aesHelper.Decrypt(encryptedData);
string decryptedText = Encoding.UTF8.GetString(decryptedData);
Console.WriteLine("Original Text: " + plainText);
Console.WriteLine("Encrypted Data: " + Convert.ToBase64String(encryptedData));
Console.WriteLine("Decrypted Text: " + decryptedText);
}
注意:iv长度
System.Security.Cryptography.CryptographicException: 'Specified initialization vector (IV) does not match the block size for this algorithm.'
这个异常通常是因为指定的初始化向量(IV)的长度与所使用的算法的块大小不匹配导致的。AES算法的块大小为128位(16字节),因此初始化向量的长度应为16字节。
图片
总结
使用C#中的AesManaged类,我们可以轻松地实现AES加密和解密。本文介绍了基本的用法,包括生成随机密钥和IV、加密和解密数据等操作。此外,还介绍了一些常用的属性和方法,以便更好地理解和使用AesManaged类。根据实际需求和场景,我们可以灵活地使用这些功能来保护敏感数据的安全性。