public void TEABMPDecryption(FileInfo file, FormModel model) { bool threadSuccesfull = false; var timeStarted = DateTime.Now; try { if (!file.Extension.Contains("bmp")) { throw new Exception("File is not bmp!"); } //OutputFileName string outputFileName = ""; //Log loggerController.Add(" ! File dec: " + file.Name + ", Alg: " + model.AlgorithmName); //OutputFileName outputFileName = FileNameCreator.CreateFileDecryptedName( model.Folders.OutputFolder, file.Name, ".bmp"); using (FileStream fsw = new FileStream(outputFileName, FileMode.Create)) { using (BinaryWriter bw = new BinaryWriter(fsw, new ASCIIEncoding())) { //Reading and encrypting files var readedValue = File.ReadAllBytes(file.FullName); long pos = readedValue[10] + 256 * (readedValue[11] + 256 * (readedValue[12] + 256 * readedValue[13])); byte[] header = new byte[pos]; for (int i = 0; i < header.Length; i++) { header[i] = readedValue[i]; } byte[] data = readedValue.Skip(header.Length).ToArray(); var decryptedValue = TEA.Decrypt(data); decryptedValue = header.Concat(decryptedValue).ToArray(); bw.Write(decryptedValue); if (LoadedFilesController._END_OF_ENC_DEC_THREADS) { bw.Dispose(); fsw.Dispose(); File.Delete(outputFileName); Thread.CurrentThread.Abort(); } } } threadSuccesfull = true; Thread.Sleep(250); } catch (Exception ex) { loggerController.Add(" ? Dec exception: " + ex.Message); threadSuccesfull = false; } finally { this.ThreadEnds(file, threadSuccesfull, timeStarted); } }
public void TEADecryption(FileInfo file, FormModel model) { bool threadSuccesfull = false; var timeStarted = DateTime.Now; try { //OutputFileName string outputFileName = ""; //Log loggerController.Add(" ! File dec: " + file.Name + ", Alg: " + model.AlgorithmName); //Read a file char by char, and decrypt it //Reading the extension var readedValues = File.ReadAllBytes(file.FullName); int lenght = 0; var extensionLength = (int)readedValues[0]; lenght++; char[] extension = new char[extensionLength]; int j = 0; for (var i = 1; i <= extensionLength; i++) { extension[j] = (char)readedValues[i]; j++; lenght++; } var finalExtesnion = "." + new string(extension); //OutputFileName outputFileName = FileNameCreator.CreateFileDecryptedName( model.Folders.OutputFolder, file.Name, finalExtesnion); using (FileStream fsw = new FileStream(outputFileName, FileMode.Create)) { using (BinaryWriter bw = new BinaryWriter(fsw, new ASCIIEncoding())) { //DEC byte[] newValue = new byte[readedValues.Length - lenght]; int k = 0; for (int i = lenght; i < readedValues.Length; i++) { newValue[k] = readedValues[i]; k++; } var decryptedValue = TEA.Decrypt(newValue); bw.Write(decryptedValue); if (LoadedFilesController._END_OF_ENC_DEC_THREADS) { bw.Dispose(); fsw.Dispose(); File.Delete(outputFileName); Thread.CurrentThread.Abort(); } } } threadSuccesfull = true; Thread.Sleep(250); } catch (Exception ex) { loggerController.Add(" ? Dec exception: " + ex.Message); threadSuccesfull = false; } finally { this.ThreadEnds(file, threadSuccesfull, timeStarted); } }