开发者

Is there a .NET encryption library that implements AES 256 encryption as a Stream?

开发者 https://www.devze.com 2023-02-11 06:24 出处:网络
I\'d like to be able to encrypt / decrypt data as it\'s streamed to/from disk. I know I could write my own Stream and implement the encryption there, but I\'d rather not risk doing it wrong. Is there

I'd like to be able to encrypt / decrypt data as it's streamed to/from disk. I know I could write my own Stream and implement the encryption there, but I'd rather not risk doing it wrong. Is there a library that works in a similar way to the following code?

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);


You're looking for the RijndaelManaged and CryptoStream classes:

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)) {
    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消