Although encryption can be slow, I would not expect this to be a problem. I suspect that byte byte IO is causing unnecessary overhead. The easiest way to fix this is with a sensible call to Stream.CopyTo - and while you are on it, you should use the using statements to properly clear it:
private void AesEncrypt(string inputFile, string outputFile, byte[] passwordBytes, byte[] saltBytes) { var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000); RijndaelManaged aes = new RijndaelManaged { KeySize = 256, BlockSize = 128, Key = key.GetBytes(AES.KeySize / 8), IV = key.GetBytes(AES.BlockSize / 8), Padding = PaddingMode.Zeros, Mode = CipherMode.CBC }; using (var output = File.Create(outputFile)) { using (var crypto = new CryptoStream(output, aes.CreateEncryptor(), CryptoStreamMode.Write)) { using (var input = File.OpenRead(inputFile)) { input.CopyTo(crypto); } } } }
As noted in other answers, this is not a good way to generate IV. In general, I would rather use Rijndael.Create() instead of specifying RijndaelManaged - and you probably want to use the using statement for it as well.
source share