I am new to encrypting and encrypting files using the following method:
private static void encryptFile(string filePath, byte[] password, byte[] salt)
{
Rfc2898DeriveBytes rdb = new Rfc2898DeriveBytes(password, salt, 1000);
AesManaged algorithm = new AesManaged();
byte[] rgbKey = rdb.GetBytes(algorithm.KeySize / 8);
byte[] rgbIV = rdb.GetBytes(algorithm.BlockSize / 8);
GCHandle keyHandle = GCHandle.Alloc(rgbKey, GCHandleType.Pinned);
GCHandle IVHandle = GCHandle.Alloc(rgbIV, GCHandleType.Pinned);
ICryptoTransform cryptoAlgorithm = algorithm.CreateEncryptor(rgbKey, rgbIV);
using (FileStream readStream = File.Open(filePath, FileMode.Open))
{
using (FileStream writeStream = new FileStream(filePath + ".enc", FileMode.Create, FileAccess.Write))
{
using (CryptoStream cryptoStream = new CryptoStream(writeStream, cryptoAlgorithm, CryptoStreamMode.Write))
{
while (readStream.Position < readStream.Length)
{
byte[] buffer = new byte[4096];
int amountRead = readStream.Read(buffer, 0, buffer.Length);
cryptoStream.Write(buffer, 0, amountRead);
}
cryptoStream.Flush();
}
}
}
UtilityMethods.destroyBytes(rgbKey);
UtilityMethods.destroyBytes(rgbIV);
keyHandle.Free();
IVHandle.Free();
}
What I want to do is a multi-threaded process for faster encryption. Using a single stream, it took more than 5 minutes to encrypt a ~ 3 GB file. I am looking to make this encryption in less than 1 minute, if possible (less than 30 seconds will be fantastic, but I think I can stretch).
I believe that the answer is to create multiple streams (although I'm not sure), assigning a fragment of the file for encryption to each stream, but I'm not sure how to “split the file” to assign a piece for each stream or “put the file back together” after of how each part went through the stream to which it was assigned. Can someone point me in the right direction?
!
P.S. ( Rijndael CryptoStream: / ?), (ECB, CBC?), , , ?
!