//run program buttons: private void btnRunEncryption_Click(object sender, EventArgs e) { //break when paths are missing: if (txtKeyfilePath.Text == null && txtEncryptFilePath.Text == null) { return; } //continue when paths are given. //reset some public vars before (re)using them: //clear buildlist: rebuildEncryptedDataList.Clear(); //clear backgroundWorkersCompleted: BackgroundWorkersCompleted = 0; //load the key file and file to encrypt: rwBinaryFile rwB = new rwBinaryFile(); keyFileData = rwB.readBinaryFile(txtKeyfilePath.Text.ToString()); byte[] sourceFileToEncrypt = rwB.readBinaryFile(txtEncryptFilePath.Text.ToString()); //Split the load of sourceFileToEncrypt to different Backgroundworkers: //WARNING! When sourceFileToEncrypt is odd, do not lose a byte when devission is not a round number! float fltDiv = (float)sourceFileToEncrypt.Count() / (float)BackgroundWorkersAmount; int intDiv = sourceFileToEncrypt.Count() / BackgroundWorkersAmount; int restDiv = sourceFileToEncrypt.Count() % BackgroundWorkersAmount; Debug.WriteLine("fltDiv: " + fltDiv); Debug.WriteLine("intDiv: " + intDiv); Debug.WriteLine("restDiv: " + restDiv); int sourceAStartPos = intDiv * 0; byte[] bw0Data = new byte[intDiv]; Array.Copy(sourceFileToEncrypt, sourceAStartPos, bw0Data, 0, intDiv); rebuildEncryptedDataList.Add(new UInt32[intDiv]); sourceAStartPos = intDiv * 1; byte[] bw1Data = new byte[intDiv]; Array.Copy(sourceFileToEncrypt, sourceAStartPos, bw1Data, 0, intDiv); rebuildEncryptedDataList.Add(new UInt32[intDiv]); sourceAStartPos = intDiv * 2; byte[] bw2Data = new byte[intDiv]; Array.Copy(sourceFileToEncrypt, sourceAStartPos, bw2Data, 0, intDiv); rebuildEncryptedDataList.Add(new UInt32[intDiv]); //last array has to carry the rest value: //disabled --> //sourceAStartPos = intDiv * 3; byte[] bw3Data = new byte[intDiv + restDiv]; Array.Copy(sourceFileToEncrypt, sourceAStartPos, bw3Data, 0, intDiv + restDiv); rebuildEncryptedDataList.Add(new UInt32[intDiv + restDiv]); //now encrypt the file: bw0.RunWorkerAsync(bw0Data); //keyfiledata is publicaly available. bw1.RunWorkerAsync(bw1Data); //disabled --> //bw2.RunWorkerAsync(bw2Data); bw3.RunWorkerAsync(bw3Data); }
private void btnRunDecrypt_Click(object sender, EventArgs e) { //break when paths are missing: if (txtKeyfilePath.Text == null && txtDecryptFilePath.Text == null) { return; } //continue when paths are given. //load the key file and file to decrypt: rwBinaryFile rwB = new rwBinaryFile(); byte[] keyFileData = rwB.readBinaryFile(txtKeyfilePath.Text.ToString()); UInt32[] sourceFileToDecrypt = rwB.readEncryptedFile(txtDecryptFilePath.Text.ToString()); //now decrypt the file: FileCrypt FC = new FileCrypt(); byte[] originalData = FC.decryptFile(keyFileData, sourceFileToDecrypt); //create filepath with correct extention: string FileExtention = rwB.getEncryptedFileExtention(txtDecryptFilePath.Text); string saveFilepath = saveFileToPath("Data Files (*." + FileExtention + ")|*." + FileExtention, FileExtention); //now write the data like an original file: rwB.writeBinaryFile(saveFilepath, originalData); }