コード例 #1
0
 private void Open(string filePath)
 {
     try
     {
         Backup = new Backup(filePath);
     }
     catch (Exception exception)
     {
         MessageBox.Show(exception.Message, "Can't load " + filePath, MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
     if (Backup.PasswordHash.HasValue())
     {
         while (true)
         {
             using (var passwordDialog = new EnterPasswordForm())
             {
                 if (passwordDialog.ShowDialog() != DialogResult.OK)
                 {
                     return;
                 }
                 if (passwordDialog.Password.Text.HashEncryptionPassword() == Backup.PasswordHash)
                 {
                     Backup.Password = passwordDialog.Password.Text;
                     break;
                 }
             }
         }
     }
     if (Backup.RootDocument == null)
     {
         MessageBox.Show("There are no documents in this backup", "Empty backup", MessageBoxButtons.OK, MessageBoxIcon.Information);
         return;
     }
     if (Opened != null)
     {
         Opened(this, new EventArgs());
     }
 }
コード例 #2
0
        /// <summary>
        /// Starts the decryption process
        /// </summary>
        private async void startDecryptingBtn_Click(object sender, EventArgs e)
        {
            var dirDlg = new FolderBrowserDialog {
                Description = "Select a folder to which the files will be decrypted:"
            };

            if (dirDlg.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            var pwdDlg = new EnterPasswordForm();

            if (pwdDlg.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            _progressReporter.LockUI();
            _progressReporter.Report(-1, "Decrypting Files...");
            SelectedAlgorithm.Key = Utils.Keys.GetKey(pwdDlg.Password, (KeyType)KeySize);

            foreach (ListViewItem file in filesLst.Items)
            {
                file.ForeColor = Color.Gray;

                var fileInfo = new FileInfo(file.SubItems[3].Text);
                if (!fileInfo.Exists)
                {
                    filesLst.Items.Remove(file);
                    continue;
                }

                var decryptedFileInfo = new FileInfo(FileName.GetUnusedFileName(Path.Combine(dirDlg.SelectedPath, Path.GetFileNameWithoutExtension(fileInfo.Name))));

                _logger.Log(LoggingLevel.Information, "Decrypting `" + fileInfo.Name + "`...");
                _progressReporter.Report(-1, "Decrypting `" + fileInfo.Name + "`...");

                using (var from = fileInfo.OpenRead())
                    using (var to = decryptedFileInfo.OpenWrite())
                    {
                        _logger.Log(LoggingLevel.Information, $"Decrypting {fileInfo.Name}...");
                        var director = new CryptoDirector(SelectedAlgorithm, from, to, RedirectionMode.Decrypt, ChunkSize);

                        try
                        {
                            while (await director.ProcessChunk(
                                       p => _progressReporter.Report((int)(p * 100), $"{p * 100:0.00}% | Decrypting {fileInfo.Name}...")
                                       ))
                            {
                                ;
                            }

                            file.ForeColor = Color.Green;
                            _logger.Log(LoggingLevel.Information, "File `{0}` was decrypted successfully!", fileInfo.Name);
                        }
                        catch (Exception ex)
                        {
                            file.ForeColor = Color.Red;
                            _logger.Log(LoggingLevel.Error, "Could not Decrypt `{0}`!", fileInfo.Name);
                            _logger.Log(LoggingLevel.Trace, ex.Message);

                            if (decryptedFileInfo.Exists)
                            {
                                to.Close();
                                decryptedFileInfo.Delete();
                            }
                        }
                    }
            }

            MessageBox.Show($"Decrypted {filesLst.Items.OfType<ListViewItem>().Count(li => li.ForeColor == Color.Green)} Files Out Of {filesLst.Items.Count}!", "Summury", MessageBoxButtons.OK, MessageBoxIcon.Information);

            foreach (ListViewItem item in filesLst.Items)
            {
                if (item.ForeColor == Color.Green)
                {
                    if (!keepOldFilesCheck.Checked)
                    {
                        File.Delete(item.SubItems[3].Text);
                    }
                    filesLst.Items.Remove(item);
                }
            }

            _progressReporter.Report(0, "Ready");
            _progressReporter.UnlockUI();
        }