/// <summary> /// /// </summary> /// <param name="inputFile"></param> /// <param name="outputFile"></param> /// <param name="skey"></param> public async void EncryptFile(string inputFile, string outputFile, string skey) { Debug.WriteLine(DateTime.Now.ToString() + " --> Starting Encryption"); var e = new CryptoEventArgs(); Stopwatch sw = new Stopwatch(); sw.Start(); await Task.Factory.StartNew(() => { try { using (FileStream fsCrypt = new FileStream(outputFile, FileMode.Create)) using (ICryptoTransform encryptor = CreateAESKey(skey).CreateEncryptor()) using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write)) using (FileStream fsIn = new FileStream(inputFile, FileMode.Open)) { int data; int counter = 1; while ((data = fsIn.ReadByte()) != -1) { cs.WriteByte((byte)data); e.CompletionPercentage = (int)((double)fsIn.Position / (double)fsIn.Length * 100); //Through event on no more then a percentage if (e.CompletionPercentage > counter) { Debug.WriteLine("% " + e.CompletionPercentage + "\tPosition: " + fsIn.Position + "\tLength: " + fsIn.Length); WorkHandler(this, e); counter++; } } } } catch (Exception ex) { throw new Exception("Encryption Failed", ex); } }); sw.Stop(); var c = new CryptoCompleteEventArgs(); c.ElapsedTime = sw.Elapsed; CompleteHandler(this, c); Debug.WriteLine(DateTime.Now.ToString() + " <-- Finishing Encryption after " + sw.ElapsedMilliseconds); }
void crypto_CompleteHandler(object sender, CryptoCompleteEventArgs e) { this.Dispatcher.Invoke((Action)(() => { //Reset Progress Bar ProgressBar.Visibility = Visibility.Hidden; ProgressBar.Value = 0; // Setup Completion label if (e.ElapsedTime.CompareTo(new TimeSpan(0, 0, 1)) < 0) { FinishedLabel.Content = "Completed in less then a second."; } else { FinishedLabel.Content = "Completed in " + e.ElapsedTime.ToString(@"mm\:ss"); } DisableUI(false); })); }