在C#开发中,处理敏感文件时通过AES算法进行加密解密是常见的安全方案,AES算法属于对称加密,加密和解密使用同一个密钥,安全性和执行效率都能满足大多数业务场景的需求。

AES加密核心参数说明
使用AES算法进行文件加密时,需要重点关注两个核心参数,这两个参数的安全性直接影响加密结果的安全性:
- 密钥(Key):AES支持的密钥长度有128位、192位、256位,长度越长安全性越高,实际开发中推荐使用256位密钥。
- 初始化向量(IV):用于增加加密的随机性,避免相同内容加密后出现相同的密文,长度固定为128位也就是16字节。
文件加密实现步骤
文件加密的核心逻辑是读取原始文件字节,使用AES算法对字节进行加密,再将加密后的字节写入新的文件。下面是完整的实现代码:
using System;
using System.IO;
using System.Security.Cryptography;
public class AesFileEncryption
{
// 生成随机的256位密钥和16字节IV
public static (byte[] key, byte[] iv) GenerateAesKeyAndIv()
{
using (Aes aes = Aes.Create())
{
aes.KeySize = 256;
aes.GenerateKey();
aes.GenerateIV();
return (aes.Key, aes.IV);
}
}
// 加密文件方法
public static void EncryptFile(string inputFilePath, string outputFilePath, byte[] key, byte[] iv)
{
// 检查原始文件是否存在
if (!File.Exists(inputFilePath))
{
throw new FileNotFoundException("待加密的原始文件不存在", inputFilePath);
}
using (Aes aes = Aes.Create())
{
aes.Key = key;
aes.IV = iv;
// 创建加密器
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (FileStream inputFileStream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read))
using (FileStream outputFileStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
using (CryptoStream cryptoStream = new CryptoStream(outputFileStream, encryptor, CryptoStreamMode.Write))
{
// 将原始文件内容拷贝到加密流,自动完成加密
inputFileStream.CopyTo(cryptoStream);
}
}
}
}
文件解密实现步骤
文件解密是加密的逆过程,需要使用和加密完全相同的密钥和IV,才能正确还原原始文件内容。实现代码如下:
public class AesFileDecryption
{
// 解密文件方法,密钥和IV需要和加密时完全一致
public static void DecryptFile(string inputFilePath, string outputFilePath, byte[] key, byte[] iv)
{
// 检查加密文件是否存在
if (!File.Exists(inputFilePath))
{
throw new FileNotFoundException("待解密的加密文件不存在", inputFilePath);
}
using (Aes aes = Aes.Create())
{
aes.Key = key;
aes.IV = iv;
// 创建解密器
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (FileStream inputFileStream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read))
using (FileStream outputFileStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
using (CryptoStream cryptoStream = new CryptoStream(inputFileStream, decryptor, CryptoStreamMode.Read))
{
// 将加密流内容拷贝到输出文件,自动完成解密
cryptoStream.CopyTo(outputFileStream);
}
}
}
}
完整调用示例
下面是生成密钥、加密文件、解密文件的完整调用示例,方便开发者直接参考使用:
class Program
{
static void Main(string[] args)
{
try
{
string originalFile = "test.txt";
string encryptedFile = "test_encrypted.bin";
string decryptedFile = "test_decrypted.txt";
// 生成密钥和IV
var (key, iv) = AesFileEncryption.GenerateAesKeyAndIv();
Console.WriteLine("生成密钥成功,密钥长度:" + key.Length + "字节");
Console.WriteLine("生成IV成功,IV长度:" + iv.Length + "字节");
// 加密文件
AesFileEncryption.EncryptFile(originalFile, encryptedFile, key, iv);
Console.WriteLine("文件加密完成,加密文件路径:" + encryptedFile);
// 解密文件
AesFileDecryption.DecryptFile(encryptedFile, decryptedFile, key, iv);
Console.WriteLine("文件解密完成,解密文件路径:" + decryptedFile);
}
catch (Exception ex)
{
Console.WriteLine("操作失败:" + ex.Message);
}
}
}
开发注意事项
在实际使用AES进行文件加密解密时,需要注意以下几点避免出现问题:
- 密钥和IV需要妥善保存,丢失后无法解密文件,不要将密钥硬编码在代码中,建议通过安全的配置存储方式管理。
- 加密后的文件不要使用原来的文件扩展名,避免被恶意识别尝试破解。
- 加密大文件时,上面的流式处理方式不会占用过多内存,适合处理任意大小的文件。
- 如果需要跨平台使用加密后的文件,要确保AES的参数配置一致,比如填充模式、块模式最好使用默认的PKCS7和CBC,兼容性更好。