Exemple #1
0
        // Decrypt
        private async void DecryptButton_Click(object sender, RoutedEventArgs e)
        {
            DisableUIs();

            int nQuestions = _decMpeFileInfo._nQuestions;

            string[] passwords = new string[nQuestions];
            for (int i = 0; i < passwords.Length; ++i)
            {
                var passwordTxt = _decPasswordTxts[i];
                if ((bool)DecTrimChk.IsChecked)
                {
                    passwordTxt.Text = passwordTxt.Text.Trim();
                }
                passwords[i] = passwordTxt.Text;
            }

            // output dir
            string outputDir = DecOutputDirTxt.Text;

            if ((bool)DecCreateDirChk.IsChecked)
            {
                string encryptedFilePath = DecEncryptedFileTxt.Text;
                if (encryptedFilePath.Contains(@"\"))
                {
                    encryptedFilePath = encryptedFilePath.Substring(encryptedFilePath.LastIndexOf(@"\") + 1);
                }
                if (encryptedFilePath.Contains("."))
                {
                    encryptedFilePath = encryptedFilePath.Substring(0, encryptedFilePath.IndexOf('.'));
                }
                outputDir = System.IO.Path.Combine(outputDir, encryptedFilePath);
            }

            for (int i = 0; i < passwords.Length; ++i)
            {
                passwords[i] = convertPassword(passwords[i], (AppConst.MPE_Flag)_decMpeFileInfo._flag);
            }

            var currentCursor = this.Cursor;

            try
            {
                this.Cursor = Cursors.Wait;
                var  p      = new Progress <string>(ShowProgress);
                bool result = await Task <bool> .Run(() => { return(MPECore.Decrypt(_decMpeFileInfo, passwords, outputDir, p)); });

                this.Cursor = currentCursor;
                if (result)
                {
                    var message = ResourceService.Current.getMessage("MsgIEndDecryption"); // Decryption is complete.
                    MessageBox.Show(message, "Succeed", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
                }
                else
                {
                    var message = ResourceService.Current.getMessage("MsgIPasswordIncorrect"); // Password is incorrect.
                    MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK);
                }
            }
            catch (Exception ex)
            {
                var message = "Error. detail:" + ex.Message;
                MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                this.Cursor = currentCursor;
            }

            EnableUIs();
        }
Exemple #2
0
        internal void openDecMpeFile(bool isShowMessage)
        {
            if (File.Exists(DecEncryptedFileTxt.Text) == false)
            {
                return;
            }
            var currentCursor = this.Cursor;

            this.Cursor = Cursors.Wait;

            string message;
            var    decMPEfileInfo = MPECore.readHeader(DecEncryptedFileTxt.Text);

            if (decMPEfileInfo._errorDetailMessage != "")
            {
                if (isShowMessage)
                {
                    message = ResourceService.Current.getMessage("MsgETargetNotMPE") + decMPEfileInfo._errorDetailMessage; // The specified file is not a Multiple Passwords Encrypted file.\r\n\r\nDetailed information:
                    MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                }
                goto EXIT;
            }
            int nQuestions         = decMPEfileInfo._nQuestions;
            int nRequiredPasswords = decMPEfileInfo._nRequiredPasswords;

            // create UI
            _decPasswordTxts = new TextBox[nQuestions];
            setFlag(decMPEfileInfo._flag);

            DecryptBtn.IsEnabled = true;
            DecNumRequiredPasswordsCmb.Items.Clear();
            DecNumRequiredPasswordsCmb.Items.Add(nRequiredPasswords);
            DecNumRequiredPasswordsCmb.SelectedIndex = 0;
            for (int i = 0; i < nQuestions; ++i)
            {
                var question = decMPEfileInfo._decQuestions[i];

                var border = new Border();
                border.BorderThickness = TH_BOADER;
                border.BorderBrush     = Brushes.Gray;
                border.Margin          = TH_MARGIN_T5_R5;

                var scrollViewer = new ScrollViewer();
                scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                scrollViewer.VerticalScrollBarVisibility   = ScrollBarVisibility.Disabled;
                scrollViewer.Padding = TH_PADDING_5;
                border.Child         = scrollViewer;

                var questionStackPanel = new StackPanel();
                questionStackPanel.Orientation = Orientation.Vertical;
                scrollViewer.Content           = questionStackPanel;

                var hintStackPanel = new StackPanel();
                hintStackPanel.Orientation = Orientation.Horizontal;
                questionStackPanel.Children.Add(hintStackPanel);

                var hintLabel = new Label();
                hintLabel.Content = "Hint" + (i + 1) + ":";
                hintLabel.Width   = HEADER_WIDTH;
                hintStackPanel.Children.Add(hintLabel);

                var hintTextBox = new TextBox();
                hintTextBox.MinWidth  = HINT_WIDTH;
                hintTextBox.IsEnabled = false;
                hintStackPanel.Children.Add(hintTextBox);
                hintTextBox.Text = question.hint;


                var questionRemoveButton = new Button();
                questionRemoveButton.Content   = "X";
                questionRemoveButton.Width     = HINT_REMOVE_BUTTON_WIDTH;
                questionRemoveButton.Margin    = TH_MARGIN_L10;
                questionRemoveButton.IsEnabled = false;
                hintStackPanel.Children.Add(questionRemoveButton);


                var passwordStackPanel = new StackPanel();
                passwordStackPanel.Orientation = Orientation.Horizontal;
                passwordStackPanel.Margin      = TH_MARGIN_T5;
                questionStackPanel.Children.Add(passwordStackPanel);

                var passwordLabel = new Label();
                passwordLabel.Content = "Password:"******"" && isShowMessage)
            {
                message = ResourceService.Current.getMessage("MsgWMPEInvalidFormat") + decMPEfileInfo._warningDetailMessage; // The specified file may not be decrypted properly.\r\n\r\nDetailed information:
                MessageBox.Show(message, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
            }

            _decMpeFileInfo = decMPEfileInfo;
EXIT:
            this.Cursor = currentCursor;
        }
Exemple #3
0
        // Encrypt
        private async void EncryptButton_Click(object sender, RoutedEventArgs e)
        {
            DisableUIs();

            // input file check
            string message;

            if (EncFileNameLb.Items.Count == 0)
            {
                message = ResourceService.Current.getMessage("MsgETargetNotSpecified"); // Encryption target file is not specified.
                MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                goto EXIT;
            }
            if (EncOutputFileTxt.Text.Trim() == "")
            {
                message = ResourceService.Current.getMessage("MsgEOutputNotSpecified"); // Output file is not specified.
                MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                goto EXIT;
            }


            var fileList         = new List <string>();
            var dirList          = new List <string>();
            var notExistFileList = new List <string>();
            var encTargetList    = new List <string>();

            foreach (var item in EncFileNameLb.Items)
            {
                string pathFile = item.ToString();
                if (File.Exists(pathFile))
                {
                    fileList.Add(System.IO.Path.GetFileName(pathFile));
                    encTargetList.Add(pathFile);
                }
                else if (Directory.Exists(pathFile))
                {
                    var dir = new DirectoryInfo(pathFile);
                    dirList.Add(dir.Name);
                    encTargetList.Add(pathFile);
                }
                else
                {
                    notExistFileList.Add(pathFile);
                }
            }
            if (fileList.Count != fileList.Distinct().Count() || dirList.Count != dirList.Distinct().Count())
            {
                message = ResourceService.Current.getMessage("MsgETargetDuplicated"); // Encryption is impossible due to duplicate file name or folder name.
                MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                goto EXIT;
            }
            if (notExistFileList.Count > 0)
            {
                message = ResourceService.Current.getMessage("MsgWFileNotExisted"); // The following file does not exist.
                foreach (var s in notExistFileList)
                {
                    message += "\r\n\t" + s;
                }
                if (EncFileNameLb.Items.Count == notExistFileList.Count)
                {
                    message += "\r\n\r\n" + ResourceService.Current.getMessage("MsgETargetNotExisted"); // There is no file to encrypt.
                    MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                    goto EXIT;
                }
                else
                {
                    message += "\r\n\r\n" + ResourceService.Current.getMessage("MsgCEncryptOnlyExistFile"); // Encrypt with existing files only?
                    if (MessageBox.Show(message, "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No) == MessageBoxResult.No)
                    {
                        goto EXIT;
                    }
                }
            }

            // input password check
            int nQuestions         = _encQuestionList.Count;
            int nRequiredPasswords = int.Parse(EncNumRequiredPasswordsCmb.Text);

            if ((bool)EncTrimChk.IsChecked)
            {
                foreach (var question in _encQuestionList)
                {
                    foreach (var passwordText in question._passwordStackPanel.Children.OfType <TextBox>())
                    {
                        passwordText.Text = passwordText.Text.Trim();
                    }
                }
            }

            string[]        hintList       = new string[nQuestions];
            List <string>[] passwordList   = new List <string> [nQuestions];
            int             nDummyQuestion = 0;

            for (int i = 0; i < _encQuestionList.Count; ++i)
            {
                var question = _encQuestionList[i];

                hintList[i]     = question._hintTextBox.Text;
                passwordList[i] = new List <string>();

                foreach (var passwordText in question._passwordStackPanel.Children.OfType <TextBox>())
                {
                    if (passwordText.Text != "")
                    {
                        passwordList[i].Add(passwordText.Text);
                    }
                }
                if (passwordList[i].Count == 0)
                {
                    nDummyQuestion += 1;
                }
            }

            if (nDummyQuestion > 0)
            {
                message = ResourceService.Current.getMessage("MsgWDummyQuestion"); // A Question without a password is a dummy Question.
                if (nQuestions - nDummyQuestion < nRequiredPasswords)
                {
                    message += "\r\n" + ResourceService.Current.getMessage("MsgWUndecrytableArchive"); // Because it is less than the number of passwords required for decryption, an archive that can not be decrypted is created.
                }
                MessageBox.Show(message, "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK);
            }

            if (File.Exists(EncOutputFileTxt.Text))
            {
                message = ResourceService.Current.getMessage("MsgCOverwriteOutputFile"); // The output file exists. Do you want to overwrite?
                if (MessageBox.Show(message, "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No) == MessageBoxResult.No)
                {
                    goto EXIT;
                }
            }

            FileStream outputFS;

            try
            {
                outputFS = File.Create(EncOutputFileTxt.Text);
            }
            catch (Exception ex)
            {
                message = ResourceService.Current.getMessage("MsgEFailCreateFile") + ex.Message; // Failed to create the output file.\r\n\r\nDetailed information:
                MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                goto EXIT;
            }

            message = ResourceService.Current.getMessage("MsgCStartEncryption"); // Start encryption.
            if (MessageBox.Show(message, "Confirmation", MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.Cancel) == MessageBoxResult.Cancel)
            {
                outputFS.Close();
                goto EXIT;
            }


            AppConst.MPE_Flag flag = createFlag();
            foreach (var list in passwordList)
            {
                for (int i = 0; i < list.Count; ++i)
                {
                    list[i] = convertPassword(list[i], flag);
                }
            }

            var currentCursor = this.Cursor;

            try
            {
                this.Cursor = Cursors.Wait;
                var p = new Progress <string>(ShowProgress);
                await Task.Run(() => { MPECore.Encrypt(encTargetList, hintList, passwordList, nRequiredPasswords, flag, outputFS, p); });
            }
            catch (Exception ex)
            {
                message = ResourceService.Current.getMessage("MsgEFailEncryption") + ex.Message; // Encryption failed.\r\n\r\nDetailed information:
                MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                goto EXIT;
            }
            finally
            {
                outputFS.Close();
                this.Cursor = currentCursor;
            }
            message = ResourceService.Current.getMessage("MsgIEndEncryption"); // Encryption is complete.
            MessageBox.Show(message, "Succeed", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);

EXIT:
            EnableUIs();
        }