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); }
//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); }
//put all data inside 1 array and write it to file: void buildEncryptedArrayAndWriteToFile(int threadID, UInt32[] _encryptedData) { rebuildEncryptedDataList.Insert(threadID, _encryptedData); //check if all workers are done before saving data: if (BackgroundWorkersCompleted < BackgroundWorkersAmount) { return; } if (progressBar1.Value < 100) { MessageBox.Show("Error encrypting file."); return; } //rebuild list with array (2D) to 1D array: List <UInt32> dataCompleteList = new List <UInt32>(); foreach (UInt32[] bwData in rebuildEncryptedDataList) { foreach (UInt32 encDataLine in bwData) { if (encDataLine != 0) //skip '0' chars, no idea where comming from :(. { dataCompleteList.Add(encDataLine); } } } //now write the data to file: rwBinaryFile rwB = new rwBinaryFile(); //create save file containing filetype: string savePath = saveFileToPath(); string saveFileExtention = rwB.getBinaryFileExtention(txtEncryptFilePath.Text); //add extetion to filename as dualextention: string[] savePathBuf = savePath.Split('.'); savePath = ""; //clear savePath; for (int I = 0; I < savePathBuf.Count(); I++) { if (I == savePathBuf.Count() - 1) //add file extention; { savePath += '.' + saveFileExtention + '.'; //this could probably give errors when filepath already contains multiple '.' } savePath += savePathBuf[I]; } Debug.Print(savePath); rwB.writeEncryptedFile(savePath, dataCompleteList.ToArray()); }