Ejemplo n.º 1
0
        public static void ProcessFile(FileInfo fileInfo, string password, ProcessTypes processType, bool destructOriginalFile, bool isAlone)
        {
            var substitutionByteList = GetSubstitutionByteList(password);

            completionPercentage = 0;

            try
            {
                using (var readStream = new FileStream(fileInfo.FullName, FileMode.Open))
                {
                    var newFileName = processType == ProcessTypes.encrypt ? fileInfo.FullName + ".crypted" : fileInfo.FullName.Substring(0, fileInfo.FullName.LastIndexOf('.'));
                    using (var writeStream = new FileStream(newFileName, FileMode.CreateNew))
                    {
                        var cpt = 0;

                        var buffer = new byte[1];
                        while (readStream.Read(buffer, 0, 1) > 0)
                        {
                            byte[] dataToWrite = new byte[1];

                            if (processType == ProcessTypes.encrypt)
                            {
                                var index = defaultByteList.IndexOf(buffer[0]);
                                dataToWrite[0] = substitutionByteList[index];
                            }
                            else
                            {
                                var index = substitutionByteList.IndexOf(buffer[0]);
                                dataToWrite[0] = defaultByteList[index];
                            }

                            writeStream.Write(dataToWrite, 0, 1);


                            if (isAlone)
                            {
                                DisplayCompletionMeter(cpt, (int)readStream.Length - 1);
                                cpt++;
                            }
                        }
                    }
                }
            }
            catch (IOException)
            {
                ConsoleManager.WriteLine(fileInfo.Name + " : IO error... unlucky", ConsoleManager.Colors.Error);
                return;
            }
            catch (Exception)
            {
                ConsoleManager.WriteLine(fileInfo.Name + " : Something went wrong... unlucky", ConsoleManager.Colors.Error);
                return;
            }

            if (destructOriginalFile)
            {
                File.Delete(fileInfo.FullName);
            }

            if (!isAlone)
            {
                processedFiles += 1;
                Console.WriteLine("- {0} / {1} -   {2}", processedFiles, totalFiles, fileInfo.Name);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            if (args.Length >= 2)
            {
                ConsoleManager.Init();

                var path  = args[0];
                var files = new List <string>();

                Security.ProcessTypes method = PromptMethod(args[1] == "folder");
                var  password      = PromptPassword(false, method);
                bool destructFiles = PromptDestruct(args[1] == "folder");


                Console.Write("\nTarget:  ");
                ConsoleManager.WriteLine(path, ConsoleManager.Colors.File);
                if (args[1] == "folder")
                {
                    Console.Write("Warning : the folder encryption / decryption is ");
                    ConsoleManager.WriteLine("RECURSIVE", ConsoleManager.Colors.Important);
                }

                if (args[1] == "file")
                {
                    if (File.Exists(path))
                    {
                        files.Add(path);
                    }
                }
                else
                {
                    if (Directory.Exists(path))
                    {
                        foreach (var file in GetFilesInFolder(path))
                        {
                            files.Add(file);
                        }
                    }
                }

                Console.WriteLine("\nAre you ready to launch the process ? (Press ENTER... or CTRL + C to quit)");
                Console.ReadLine();
                Console.Clear();
                if (files.Count != 1)
                {
                    Console.WriteLine("Processed files :");
                }
                else
                {
                    Console.WriteLine("Progress :");
                }

                Security.totalFiles = files.Count;
                List <Task> tasks = new List <Task>();
                foreach (var file in files)
                {
                    tasks.Add(Task.Run(() => { Security.ProcessFile(new FileInfo(file), password, method, destructFiles, files.Count == 1); }));
                }

                Task.WaitAll(tasks.ToArray());

                Console.WriteLine("\n\n----------\nDone !");
                Console.ReadLine();
            }
        }