public async Task ReadData(string path, byte[] keystream, byte keyrepeat, IProgress <int> sizeReport, IProgress <int> PerformStep, string password) { ExceptionEncountered = false; try { listByte.Clear(); int chunk = 1024 * 1024 * 10; byte[] chunkData = new byte[chunk]; long readChunk = 0; bool less100mb = false; enchipher = new EBCEA(keystream, keyrepeat); if (size < chunk) { chunk = (int)size; less100mb = true; } int rounds = 0; if (size % chunk != 0) { rounds = (int)(size / chunk) + 1; } else { rounds = (int)size / chunk; } sizeReport.Report(rounds); int i = 0; while (readChunk != size) { if (!less100mb) { if ((size - readChunk) < (1024 * 1024 * 10)) { chunk = (int)(size - readChunk); chunkData = new byte[chunk]; } } chunkData = enchipher.encyptData(ReadChunk(path, readChunk, chunk, true), password); i++; PerformStep.Report(i); readChunk += chunk; listByte.Add(chunkData); await Task.Delay(100); } } catch (Exception ErrorMessage) { ExceptionEncountered = true; MessageBox.Show(string.Format("Encountered an exception while reading file.\n\n{0}", ErrorMessage.Message), "Cuttlefish", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public async Task writeData(string path, long size, byte[] keystream, byte keyrepeat, IProgress <long> reportWroteData, List <byte[]> data, string password) { ExceptionEncountered = false; try { enchipher = new EBCEA(keystream, keyrepeat); long wroteDataLenght = 0; reportWroteData.Report(wroteDataLenght); FileStream fs = null; if (File.Exists(path)) { fs = new FileStream(path, FileMode.Truncate, FileAccess.Write); } else { fs = new FileStream(path, FileMode.Create, FileAccess.Write); } using (fs) { for (int i = 0; i < data.Count; i++) { byte[] temp = decryptDataStream(data[i], password); if ((size - wroteDataLenght) < temp.Length) { fs.Write(temp, 0, (int)(size - wroteDataLenght)); wroteDataLenght += (size - wroteDataLenght); reportWroteData.Report(wroteDataLenght); } else { fs.Write(temp, 0, temp.Length); wroteDataLenght += temp.Length; reportWroteData.Report(wroteDataLenght); } await Task.Delay(100); } } fs.Dispose(); } catch (Exception ErrorMessage) { ExceptionEncountered = true; MessageBox.Show(string.Format("Opps, file access denied. Please check if file is in use by another program \n{0}", ErrorMessage.Message), "Cuttlefish", MessageBoxButtons.OK, MessageBoxIcon.Error); } }