Lowering speed on my hybride encryption function for large files
Multithreaded Approach

Since you are using WinForms you can use BackgroundWorker.  BackgroundWorker will automatically use threads from the ThreadPool, and importantly, they have a notification event for when the thread completes.

There are several ways this could be done, and also keep in mind that I only have moderate experience with multithreading in C#.

You will need to create several BackgroundWorkers.  I would start with 4 and adjust from there. We can also simplify reassembling the blocks by writing them to the output file as they complete.  

Here is a high level overview for what you should do:


Put the workers in a list of available threads.
In the main thread, check if there is a free worker in the list.


If Yes, read the next block from the file, pass it and the block index to the worker and start it. Remove the worker from the list
If No, then sleep.

After the worker has completed, it will call the RunWorkerCompleted event, where it will return its result (which should be the encrypted block and the block index.
The event handler should write the encrypted block to the corresponding index in the output file, and then add the worker back to the list.
Repeat until all the blocks have been processed.


Some Notes

The RunWorkerCompleted event may run on the background thread (or you may get errors about it) and we don't want that.  First, any updates to the UI, like updating the progress after a block is written, have to happen on the main UI thread.  Secondly, we don't want multiple threads trying to write to the output file at the same time.  There is a simple trick to get the RunWorkCompleted event to run on the main UI thread.

private void bgworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{ 
    //if we aren't on the main thread, recursively call on main thread
    if(this.InvokeRequired)
    Invoke( ( MethodInvoker ) delegate {
        bgworker_RunWorkerCompleted(sender, e);
        return;  // don't forget to return or this method will do it twice
    });

   //Write to file
   //Update Progress
   //Add worker back to list
} 


The BackgroundWorker.RunWorkerAsync() and RunWorkerCompletedEventArgs only accept/return a single object and you need to pass two (the block index and the block itself).  You will need to either use a tuple/struct/class or something else to hold both objects when passing back and forth.

Finally, You may need special handling for the last block of the file.  Some encryption algorithms (zero) pad a short block to the expected length (1K in this case).  This means that your encrypted file may be slightly longer than your unencrypted file. If this is the case for your chosen algorithm, your allocated output file size may be slightly longer than the original input.  Depending on the file contents, this may or may not be an problem, however you should be aware of it as a potential issue.



Update: What I mean by Block Index

Let's say a file is 10K.  The first block has index 0 and is 1K in length.  The second block has index 1, the third index 2, etc.  The file has 10 blocks with indexes 0-9.

Now lets say that I queue things up and block index 4 finishes first.  In my output file I seek to position 1000*blockIndex, or 4000 and write the 1K bytes.  Then let's say that block 2 finishes.  I seek to position 2000 and write the 1K.  My output file now looks like [0|0|*|0|*|0|0|0|0|0] where '*' denotes valid data and '0' denotes empty/nothing written.

For performance purposes, I'd probably let the UI thread open the output file at the beginning and only close it after the last block is written.  You know the file is done when all of your workers are sitting in the list.  (Workers also have a property to check their status :IsBusy)

Links


Simple tutorial on BackgroundWorker
BackgroundWorker documentation