是否有.NET加密库将AES 256加密实现为Stream?

时间:2011-02-17 16:12:09

标签: c# encryption aes

我希望能够在数据流入/流出磁盘时加密/解密数据。 我知道我可以编写自己的Stream并在那里实现加密,但我宁愿不冒险做错。是否存在与以下代码类似的库?

byte[] encryptionKey = ;
byte[] initVector = ;

var fileStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write);
var encryptionStream = new AesEncryptionStream(fileStream, initVector, encryptionKey);
var gzStream = new GZipStream(encryptionStream, CompressionMode.Compress);
var writer = new BinaryWriter(gzStream);

1 个答案:

答案 0 :(得分:10)

您正在寻找RijndaelManagedCryptoStream类:

var aes = new RijndaelManaged { Key = ..., IV = ... };

using (var encryptor = aes.CreateEncryptor()) 
using (var cryptoStream = new CryptoStream(gzStream, encryptor, CryptoStreamMode.Write))
using (var writer = new BinaryWriter(cryptoStream)) {
    ...
}