I'm writing an encryption program to encrypt files (large and small) to do this, my current method is to read 1024 bytes from a file, encrypt those bytes, and write them to a temporary file, and repeat until finish. Once this process finishes, the original file is deleted and the temporary file is renamed to take the name of the original.
Here is a sample piece of code that processes n bytes (n being 1024):
private void processChunk(BinaryReader Input, BinaryWriter Output, int n)
{
// Read n bytes from the input fileStream
Byte[] Data = Input.ReadBytes(n);
// Read n bytes from the streamCipher
Byte[] cipherData = StreamCipher.OutputBytes(n);
for (int x = 0; x < n; x++)
// XOR a byte of the input stream with a corresponding byte of the streamCipher
Data[x] ^= cipherData[x];
// Write n bytes to the output fileStream
Output.Write(Data);
}
So I'm pretty sure I can't multi-thread the encryption algorithm because the bytes are generated as a keystream and depend on the bytes generated before, but reading and writing from files and cpu operations can be?
What's the best strategy to take here?