コード例 #1
0
 private void DecryptWorker(object sender, DoWorkEventArgs e)
 {
     time1             = DateTime.Now;
     fileprocessresult = -1;
     fileprocessresult = decryption.Decrypt(inFilePath, outFilePath);
     decryption        = null;
 }
コード例 #2
0
 private void RealTimeDecryptActivation(object sender, RoutedEventArgs e)
 {
     try
     {
         if (privateKeyFilePathTextBox.Text != "")
         {
             decryption = new Decryption(File.ReadAllText(privateKeyFilePathTextBox.Text));
         }
         else
         {
             decryption = null;
         }
     }
     catch (FileNotFoundException)
     {
     }
     catch (Exception error)
     {
         MessageBox.Show(error.ToString());
     }
 }
コード例 #3
0
        private void Decrypt(object sender, RoutedEventArgs e)
        {
            try
            {
                if (privateKeyFilePathTextBox.Text == "")
                {
                    throw new ArgumentException("私钥文件路径为空!");
                }
                if (!File.Exists(privateKeyFilePathTextBox.Text))
                {
                    throw new ArgumentException("私钥文件不存在!");
                }
                statusBar.Visibility    = Visibility.Visible;
                statusBarTextBlock.Text = "读取私钥文件";
                decryption = new Decryption(File.ReadAllText(privateKeyFilePathTextBox.Text));
                if (decryptionTypeComboBox.SelectedIndex == 0)
                {
                    if (encryptedtextTextBox.Text == "")
                    {
                        throw new ArgumentException("密文为空!");
                    }
                    if ((encryptedtextTextBox.Text.Trim().Length - 58) % 32 != 0)
                    {
                        throw new ArgumentException("密文格式错误!");
                    }
                    int    encryptedlength = encryptedtextTextBox.Text.Trim().Length >> 1;
                    byte[] encrypted       = new byte[encryptedlength];
                    for (int i = 0; i < encryptedlength; i++)
                    {
                        encrypted[i] = Byte.Parse(encryptedtextTextBox.Text.Trim().Substring(i * 2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                    }
                    byte[] decrypted = decryption.Decrypt(encrypted);
                    if (decryption.flag == 0)
                    {
                        decryptedtextTextBox.Text = Encoding.Default.GetString(decrypted);
                        statusBar.Visibility      = Visibility.Collapsed;
                        statusBarTextBlock.Text   = "";
                        MessageBox.Show("解密完成");
                    }
                    else if (decryption.flag == 1)
                    {
                        decryptedtextTextBox.Text = "";
                        statusBar.Visibility      = Visibility.Collapsed;
                        statusBarTextBlock.Text   = "";
                        MessageBox.Show("加密数据已损坏!");
                    }
                    else if (decryption.flag == 2)
                    {
                        decryptedtextTextBox.Text = "";
                        statusBar.Visibility      = Visibility.Collapsed;
                        statusBarTextBlock.Text   = "";
                        MessageBox.Show("私钥文件不匹配!");
                    }
                    decryption = null;
                }
                else if (decryptionTypeComboBox.SelectedIndex == 1)
                {
                    if (encryptedFilePathTextBox.Text == "")
                    {
                        throw new ArgumentException("加密文件路径为空!");
                    }
                    if (decryptedFilePathTextBox.Text == "")
                    {
                        throw new ArgumentException("解密文件保存路径为空!");
                    }
                    if (!File.Exists(encryptedFilePathTextBox.Text))
                    {
                        throw new ArgumentException("加密文件不存在!");
                    }
                    if (File.Exists(decryptedFilePathTextBox.Text))
                    {
                        if (new FileInfo(decryptedFilePathTextBox.Text).IsReadOnly)
                        {
                            throw new ArgumentException("解密文件只读,无法覆盖!");
                        }
                    }
                    inFilePath              = encryptedFilePathTextBox.Text;
                    outFilePath             = decryptedFilePathTextBox.Text;
                    datasize                = 0;
                    statusBarTextBlock.Text = "解密文件中...";

                    if (decryptworker.IsBusy != true)
                    {
                        decryptworker.RunWorkerAsync();
                    }
                    else
                    {
                        MessageBox.Show("上一个解密操作尚未完成!");
                    }
                }
            }
            catch (ArgumentException error)
            {
                statusBar.Visibility    = Visibility.Collapsed;
                statusBarTextBlock.Text = "";
                MessageBox.Show(error.Message.ToString());
            }
            catch (FileNotFoundException error)
            {
                statusBar.Visibility    = Visibility.Collapsed;
                statusBarTextBlock.Text = "";
                MessageBox.Show(error.Message.ToString());
            }
            catch (UnauthorizedAccessException error)
            {
                MessageBox.Show(error.Message.ToString());
            }
            catch (Exception error)
            {
                MessageBox.Show(error.ToString());
            }
        }