Justin G Justin G - 1 year ago 97
C# Question

C# Calculating Percentage of AES Encryption/Decryption

I'm trying to encrypt/decrypt a file using AES 256 bit with the code I got from here. The full code I am using is seen here. I was wondering how I could calculated the percentage done of both encryption/decryption in the while loop as it writes to the file. For example in encryption:

while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
cs.Write(buffer, 0, read);
//int percentage = Calculate percentage done here?

And in decryption:

while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
fsOut.Write(buffer, 0, read);
//int percentage = Calculate percentage done here?

Answer Source

You can compute the percentage complete as follows:

var percentComplete = (float)fsIn.Position * 100 / fsIn.Length;

How you display it is up to you. You can update a form control (you may need to use invoke if your cypto runs on a worker thread) or raise a custom event (e.g. ProgressChanged) and consume it in your UI thread.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download