Ejemplo n.º 1
0
        //解密
        private async void button3_Click(object sender, EventArgs e)
        {
            using (_longOperation.Start())
            {
                await Task.Run(() =>
                {
                    string filePath      = this.descryFilePathTxt.Text.Trim();
                    string directoryPath = this.descTargetDirectoryTxt.Text.Trim();

                    //判断是否选择了文件
                    if (filePath.Length == 0)
                    {
                        MessageBox.Show("Please select a file", "Info", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                    {
                        //判断选择文件是否存在
                        if (!File.Exists(filePath))
                        {
                            MessageBox.Show("Please select a valid file", "Info", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }
                    //检查选择的输出目录
                    if (directoryPath.Length == 0)
                    {
                        MessageBox.Show("Please select target directory", "Info", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                    {
                        //不存在则创建
                        if (!Directory.Exists(directoryPath))
                        {
                            Directory.CreateDirectory(directoryPath);
                        }
                    }

                    //检查输入的密码
                    if (this.descpasswordTxt.Text.Trim().Length == 0)
                    {
                        MessageBox.Show("Please input password", "Info", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    //检查选择的加密模式
                    int CipherModeSelectedIndex = -1;
                    if (CipherModeComboBox.InvokeRequired)
                    {
                        CipherModeComboBox.Invoke(new MethodInvoker(delegate { CipherModeSelectedIndex = CipherModeComboBox.SelectedIndex; }));
                    }
                    if (CipherModeSelectedIndex == -1)
                    {
                        MessageBox.Show("Please select cipher mode", "Info", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    //同解密,转换为RijndaelManaged所需要CipherMode的枚举类型
                    CipherMode cm = CipherMode.CBC;
                    switch (CipherModeArray[CipherModeSelectedIndex])
                    {
                    case "CBC":
                        cm = CipherMode.CBC;
                        break;

                    case "ECB":
                        cm = CipherMode.ECB;
                        break;

                    case "CFB":
                        cm = CipherMode.CFB;
                        break;
                    }
                    //filePath
                    //得到被加密文件名(包含后缀
                    string sourcefileName = filePath.Remove(0, filePath.LastIndexOf("\\") + 1).Replace(cipherFileType, "");
                    //得到被加密文件的名称(不包含后缀
                    string sourceFileNameWithoutType = sourcefileName.Remove(sourcefileName.LastIndexOf("."));
                    //得到被加密文件的后缀(包含"."
                    string sourceFileType = sourcefileName.Remove(0, sourcefileName.LastIndexOf("."));

                    //设置输出文件完整路径,处理方式和加密运算一致,即存在则加数字
                    string desFileName      = String.Format(@"{0}\{1}", directoryPath, sourceFileNameWithoutType);
                    string tempFileName     = desFileName;
                    bool needChangeFileName = true;
                    int fileIndex           = 0;
                    while (needChangeFileName)
                    {
                        if (File.Exists(String.Format("{0}{1}", desFileName, sourceFileNameWithoutType)))
                        {
                            desFileName = String.Format("{0}_{1}", tempFileName, ++fileIndex);
                        }
                        else
                        {
                            needChangeFileName = false;
                        }
                    }
                    //此处得到解密后文件路径
                    desFileName = String.Format("{0}{1}", desFileName, sourceFileType);
                    //实例化EncryptDecryptHelper
                    EncryptDecryptHelper edHelper = new EncryptDecryptHelper(filePath, desFileName, this.descpasswordTxt.Text.Trim(), cm);
                    //添加Stopwatch监测运行时间
                    Stopwatch stopWatch = new Stopwatch();
                    stopWatch.Start();//stopWatch开始监测
                    //开始加密,并得到结果rm
                    ResultMsg rm = edHelper.DescryptEncryptHandler(DescrptAndEncrypt.Descrpt);
                    stopWatch.Stop();                     //stopWatch停止检测
                    TimeSpan ts      = stopWatch.Elapsed; //得到stopWatch监测时间数据
                    StringBuilder sb = new StringBuilder();
                    //弹出消息框,告知运算结果
                    MessageBox.Show(rm.status ? "Succeed" : "Failure", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (rm.status)
                    {
                        sb.Append(String.Format("{4}{5}\r\nSource Filename:{0}\r\nDescrypted Filename:{1}\r\nCipherMode:{2}\r\nOperation time:{3}ms", sourcefileName, desFileName, CipherModeArray[CipherModeSelectedIndex], ts.TotalMilliseconds, this.ResultTxt.Text.Length == 0 ? "" : "\r\n\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    }
                    else
                    {
                        sb.Append(String.Format("{0}{1}\r\n{2}", this.ResultTxt.Text.Length == 0 ? "" : "\r\n\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), rm.msg));
                    }
                    if (ResultTxt.InvokeRequired)
                    {
                        ResultTxt.Invoke(new MethodInvoker(delegate { this.ResultTxt.Text += sb.ToString(); }));
                    }
                });
            }
        }