public void TEAEcnryption(FileInfo file, FormModel model) { bool threadSuccesfull = false; var timeStarted = DateTime.Now; try { //OutputFileName string outputFileName = FileNameCreator.CreateFileEncryptedName( model.Folders.OutputFolder, file.Name, model.AlgorithmName); //Log loggerController.Add(" ! File enc: " + file.Name + ", Alg: " + model.AlgorithmName); //Read a file char by char, and encrypt it using (FileStream fsw = new FileStream(outputFileName, FileMode.Create)) { using (BinaryWriter bw = new BinaryWriter(fsw, new ASCIIEncoding())) { //Writing the extension char[] extension = file.Extension.Substring(1, file.Extension.Length - 1).ToCharArray(); char extensionLength = (char)extension.Length; bw.Write(extensionLength); for (var k = 0; k < extension.Length; k++) { bw.Write(extension[k]); } //Reading and encrypting files var readedValue = File.ReadAllBytes(file.FullName); var encryptedValue = TEA.Encrypt(readedValue); bw.Write(encryptedValue); 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(" ? Enc exception: " + ex.Message); threadSuccesfull = false; } finally { this.ThreadEnds(file, threadSuccesfull, timeStarted); } }
public void TEABMPEcnryption(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 = FileNameCreator.CreateFileEncryptedNameBMP( model.Folders.OutputFolder, file.Name, model.AlgorithmName); //Log loggerController.Add(" ! File enc: " + file.Name + ", Alg: " + model.AlgorithmName); //Read a file char by char, and encrypt it 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 encryptedValue = TEA.Encrypt(data); encryptedValue = header.Concat(encryptedValue).ToArray(); bw.Write(encryptedValue); 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(" ? Enc exception: " + ex.Message); threadSuccesfull = false; } finally { this.ThreadEnds(file, threadSuccesfull, timeStarted); } }