private void worker_DoWork(object sender, DoWorkEventArgs e) { ControllerArgs args = (ControllerArgs)e.Argument; ControllerArgs res = args.Clone(); int progress = 0; e.Cancel = false; // initialize res.last = false; res.error = false; res.current_trace = res.CurrentNum; pcModule.SetKey(res.key); DESCrypto.SetKey(res.key); targetModule.Reset(); targetModule.SetModeEncrypt(true); targetModule.SetKey(res.key); worker.ReportProgress(0, (object)res); //string fold = "D:\\powertrace\\C2power traces"; string fold = res.path + "\\C2power traces"; string backupfold = res.path + "\\backup" + "\\C2power traces"; string filename; filewrite.openfile(); Filemove filemove = new Filemove(res.path); filemove.open(0); while (res.endless || res.current_trace < res.traces) { int bytenum = 0; res.answer = null; res.ciphertext = null; res.difference = null; res.current_trace++; sw.Reset(); sw.Start(); /* * if (res.current_trace % 1000 == 0) * { * filewrite.closefile(); * filewrite.openfile(); * }*/ if (!res.endless) { progress = (int)(100 * res.current_trace / res.traces); } if (res.Algorithm == "AES") { bytenum = 16; if (res.randomGeneration) { res.plaintext = res.rand.generatePlaintext(bytenum); } pcModule.Encrypt(ref res.answer, res.key, res.plaintext); if (args.check) { targetModule.RunAES(ref res.ciphertext, res.plaintext, 0); } else { targetModule.RunAES(ref res.ciphertext, res.plaintext, args.wait); } } else if (res.Algorithm == "DES") { bytenum = 8; if (res.randomGeneration) { res.plaintext = res.rand.generatePlaintext(bytenum); } DESCrypto.Encrypt(ref res.answer, res.plaintext); if (args.check) { targetModule.RunDES(ref res.ciphertext, res.plaintext, 0); } else { targetModule.RunDES(ref res.ciphertext, res.plaintext, args.wait); } } else { break; } res.diff = Utils.differenceByteArray(ref res.difference, res.answer, res.ciphertext); //*************************************************************************************************************************** bool skip = false; if (args.check) { /*检测文件是否被创建*/ filename = fold + (res.current_trace - 1).ToString("d5") + ".dat"; Stopwatch timeout = new Stopwatch(); while (File.Exists(filename) == false) { System.Threading.Thread.Sleep(10); timeout.Stop(); if (timeout.ElapsedMilliseconds > 1000) { skip = true; res.current_trace -= 1; break; } else { timeout.Start(); } } if (timeout.IsRunning == true) { timeout.Stop(); } /*把上一个文件移入PowerTrace文件*/ if (res.current_trace >= 2 && skip == false) { filemove.move(res.current_trace - 2); } /*检测文件是否完成写入*/ timeout.Reset(); timeout.Start(); while (skip == false) { // FileInfo file = new FileInfo(filename); // if (file.Length > 100000) break; if (fileisOpen(filename) == false) { break; } timeout.Stop(); if (timeout.ElapsedMilliseconds > 1000) { break; } else { timeout.Start(); } System.Threading.Thread.Sleep(10); } if (timeout.IsRunning == true) { timeout.Stop(); } System.Threading.Thread.Sleep(args.wait); } sw.Stop(); res.elapsed = ((double)sw.ElapsedMilliseconds) / 1000; //wanganl_FileWrite(temp_num); // wanganl:把每次加密的编号(4位16进制数)写入data.txt if (skip == false) { filewrite.write(res.plaintext, res.key, res.ciphertext); } //**************************************************************************************************************************** worker.ReportProgress(progress, (object)res); if (res.diff) { res.error = true; if (!res.continueIfError) { break; } } if (worker.CancellationPending) { e.Cancel = true; break; } if (res.single) { progress = 100; break; } } //while loop end here filewrite.closefile(); filemove.move(res.current_trace - 1); filemove.close(); res.last = true; worker.ReportProgress(progress, (object)res); e.Result = (object)res; }
public void Run(ControllerArgs args) { worker.RunWorkerAsync((object)args); }