Esempio n. 1
0
        private void RegistrationButton_Click(object sender, RoutedEventArgs e)
        {
            RegistrationWindow registrationWindow = new RegistrationWindow()
            {
                Owner = this
            };

            registrationWindow.ShowDialog();
            this.AuthUser = registrationWindow.RegistredUser;
            if (this.AuthUser != null)
            {
                byte[] serData    = BinarySerializer.Serialize(this.AuthUser);
                byte[] encSerData = Encrypter.EncryptWithAesAndRsa(serData, Encrypter.DefaultKeyContainerName, false);
                File.WriteAllBytes(MainWindow.UserDirectoryPath + this.AuthUser.Login + ".mcd", encSerData);
                this.Close();
            }
        }
Esempio n. 2
0
        private byte[] TryDecryptData(byte[] data)
        {
            bool signTrue = false;

            try
            {
                signTrue = Encrypter.CheckSign(data, this.EmailBox.XmlStringSignKeyContainerName);
            }
            catch (Exception)
            {
                MessageBox.Show("Подпись данных повреждена или отсутствует!", "Ошибка",
                                MessageBoxButton.OK, MessageBoxImage.Error);
                return(data);
            }

            if (!signTrue)
            {
                MessageBox.Show("Подпись файла не совпадает!", "Ошибка",
                                MessageBoxButton.OK, MessageBoxImage.Error);
                return(data);
            }

            byte[] decData;

            try
            {
                byte[] encData = Encrypter.ReturnDataWithoutHash(data);
                decData = Encrypter.DecryptWithAesAndRsa(encData, this.EmailBox.UserKeyContainerName, false);
            }
            catch (Exception)
            {
                MessageBox.Show("Данные повреждены!", "Ошибка",
                                MessageBoxButton.OK, MessageBoxImage.Error);
                return(data);
            }

            return(decData);
        }
Esempio n. 3
0
        private void SendButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.encryptMessage.IsChecked == true &&
                this.EmailBox.XmlStringChipherKeyContainerName is null)
            {
                MessageBox.Show("Для шифрования сообщения следует импортировать открытый ключ!", "Ошибка",
                                MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (this.toTextBox.Text is null)
            {
                MessageBox.Show("Требуется ввести имя получателя.", "Ошибка",
                                MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            MailBuilder mailBuilder = new MailBuilder();

            mailBuilder.From.Add(new MailBox(this.EmailBox.EmailAddress));

            try
            {
                mailBuilder.To.Add(new MailBox(this.toTextBox.Text.Trim(' ')));
            }
            catch (ArgumentException)
            {
                MessageBox.Show("Имя получателя написано в неверном формате.", "Ошибка",
                                MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            mailBuilder.Subject = this.subjectTextBox.Text.Trim(' ');

            if (encryptMessage.IsChecked == true)
            {
                byte[] data    = this.GetBytesFromRichTextBoxText(this.textRichTextBox);
                byte[] encData = Encrypter.EncryptWithAesAndRsa(data,
                                                                this.EmailBox.XmlStringChipherKeyContainerName, true);
                byte[] signedData = Encrypter.SignData(encData,
                                                       this.EmailBox.UserKeyContainerName);
                mailBuilder.Rtf = Convert.ToBase64String(signedData);
            }
            else
            {
                mailBuilder.Rtf = this.GetTextFromRichTextBox(this.textRichTextBox);
            }

            for (int i = 0; i < this.Attachments.Count; i++)
            {
                if (encryptMessage.IsChecked == true)
                {
                    byte[] data    = this.Attachments[i];
                    byte[] encData = Encrypter.EncryptWithAesAndRsa(data,
                                                                    this.EmailBox.XmlStringChipherKeyContainerName, true);
                    byte[] signedData = Encrypter.SignData(encData,
                                                           this.EmailBox.UserKeyContainerName);

                    MimeData mime = mailBuilder.AddAttachment(signedData);
                    mime.FileName = (string)this.attachmentsListBox.Items[i];
                }
                else
                {
                    MimeData mime = mailBuilder.AddAttachment(this.Attachments[i]);
                    mime.FileName = (string)this.attachmentsListBox.Items[i];
                }
            }

            IMail mail = mailBuilder.Create();

            if (sendThread != null && sendThread.IsAlive)
            {
                sendThread.Abort();
                sendThread.Join();
            }

            sendThread = new Thread(() =>
            {
                this.Dispatcher.Invoke(() =>
                {
                    this.sendButton.IsEnabled = false;
                    Mouse.OverrideCursor      = Cursors.AppStarting;
                });

                this.EmailBox.Smtp.SendMessage(mail);

                this.Dispatcher.Invoke(() =>
                {
                    this.Close();
                    this.sendButton.IsEnabled = true;
                    Mouse.OverrideCursor      = null;
                });
            });
            sendThread.Start();
        }
Esempio n. 4
0
 private void EncryptAndSerializeCurrentUser()
 {
     byte[] serData    = BinarySerializer.Serialize(this.CurrentUser);
     byte[] encSerData = Encrypter.EncryptWithAesAndRsa(serData, Encrypter.DefaultKeyContainerName, false);
     File.WriteAllBytes(MainWindow.UserDirectoryPath + this.CurrentUser.Login + ".mcd", encSerData);
 }