void crypto_WorkHandler(object sender, CryptoEventArgs e) { this.Dispatcher.Invoke((Action)(() => { ProgressBar.Value = e.CompletionPercentage; })); }
/// <summary> /// /// </summary> /// <param name="inputFile"></param> /// <param name="outputFile"></param> /// <param name="skey"></param> public async void DecryptFile(string inputFile, string outputFile, string skey) { System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString() + " --> Starting Decryption"); var e = new CryptoEventArgs(); Stopwatch sw = new Stopwatch(); sw.Start(); await Task.Factory.StartNew(() => { try { using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open)) using (FileStream fsOut = new FileStream(outputFile, FileMode.Create)) using (ICryptoTransform decryptor = CreateAESKey(skey).CreateDecryptor()) using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read)) { int data; int counter = 1; while ((data = cs.ReadByte()) != -1) { fsOut.WriteByte((byte)data); e.CompletionPercentage = (int)((double)fsOut.Position / (double)fsOut.Length * 100); //Through event on no more then a percentage if (e.CompletionPercentage > counter) { Debug.WriteLine("% " + e.CompletionPercentage + "\tPosition: " + fsOut.Position + "\tLength: " + fsOut.Length); WorkHandler(this, e); counter++; } } } } catch (Exception ex) { throw new Exception("Decryption Failed", ex); } }); sw.Stop(); var c = new CryptoCompleteEventArgs(); c.ElapsedTime = sw.Elapsed; CompleteHandler(this, c); Debug.WriteLine(DateTime.Now.ToString() + " <-- Finishing Decryption after " + sw.ElapsedMilliseconds); }