/// <summary>
        /// 加解密函数
        /// </summary>
        /// <param name="_deMode">加密还是解密</param>
        /// <returns>输出结果</returns>
        public ResultMsg DescryptEncryptHandler(DescrptAndEncrypt _deMode)
        {
            //设置默认返回结果
            ResultMsg rm = new ResultMsg()
            {
                status = true,
                msg    = ""
            };
            //实例化RijndaelManaged
            RijndaelManaged aes = new RijndaelManaged();

            //设置BlockSize
            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            //设置KeySize
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;
            //生成Key
            Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt, iterations);

            aes.Key = key.GetBytes(aes.KeySize / 8);
            aes.IV  = key.GetBytes(aes.BlockSize / 8);
            //设置加解密模式
            aes.Mode = cipherMode;

            try
            {
                //输出文件流
                using (FileStream destination = new FileStream(destinationFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))
                {
                    //根据参数_deMode 判断此操作是加密还是解密,
                    //下面意思是如果_deMode为解密,则aes.CreateDecryptor(aes.Key, aes.IV),否则 aes.CreateEncryptor(aes.Key, aes.IV)
                    //(_deMode == DescrptAndEncrypt.Descrpt) ? aes.CreateDecryptor(aes.Key, aes.IV) : aes.CreateEncryptor(aes.Key, aes.IV)
                    using (CryptoStream cryptoStream = new CryptoStream(destination, (_deMode == DescrptAndEncrypt.Descrpt) ? aes.CreateDecryptor(aes.Key, aes.IV) : aes.CreateEncryptor(aes.Key, aes.IV), CryptoStreamMode.Write))
                    {
                        //输入文件流
                        using (FileStream source = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            //写入
                            source.CopyTo(cryptoStream);
                            //得到运算结果
                            rm.msg = String.Format("{0} successfully", (_deMode == DescrptAndEncrypt.Descrpt) ? "Descpt" : "Encrpt");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                File.Delete(destinationFilename);
                rm.status = false;
                rm.msg    = ex.Message;
            }
            return(rm);
        }
Пример #2
0
        private async void Button1_Click(object sender, EventArgs e)
        {
            using (_longOperation.Start())
            {
                await Task.Run(() =>
                {
                    string filePath      = this.encryFilePathTxt.Text.Trim();
                    string directoryPath = this.TargetDirectoryTxt.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.passwordTxt.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; }));
                    }
                    //选择值为-1则表示未选择
                    if (CipherModeSelectedIndex == -1)
                    {
                        MessageBox.Show("Please select cipher mode", "Info", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    //根据选择的加密模式设置RijndaelManaged算法所需的(enum枚举类型)
                    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);
                    //获取选择的文件名(不包含后缀名)
                    string sourceFileNameWithoutType = sourcefileName.Remove(sourcefileName.LastIndexOf("."));
                    //获取选择的文件类型名(包含"." 比如".txt")
                    string sourceFileType = sourcefileName.Remove(0, sourcefileName.LastIndexOf("."));

                    string desFileName  = String.Format(@"{0}\{1}", directoryPath, sourcefileName);
                    string tempFileName = sourceFileNameWithoutType;

                    //判断生成的加密文件是否存在,若存在则在文件名后加数字
                    bool needChangeFileName = true;
                    int fileIndex           = 0;
                    while (needChangeFileName)
                    {
                        if (File.Exists(String.Format("{0}{1}", desFileName, cipherFileType)))
                        {
                            desFileName = String.Format("{0}_{1}{2}", tempFileName, ++fileIndex, sourceFileType);
                        }
                        else
                        {
                            needChangeFileName = false;
                        }
                    }
                    //此处已经设定好输出文件完整路径及名称
                    desFileName = String.Format("{0}{1}", desFileName, cipherFileType);

                    //实例化一个EncryptDecryptHelper对象
                    EncryptDecryptHelper edHelper = new EncryptDecryptHelper(filePath, desFileName, this.passwordTxt.Text.Trim(), cm);

                    //添加进程运行时间工具Stopwatch
                    Stopwatch stopWatch = new Stopwatch();
                    stopWatch.Start();//开始计时
                    // 加密运算。rm得到运算结果
                    ResultMsg rm = edHelper.DescryptEncryptHandler(DescrptAndEncrypt.Encrypt);
                    stopWatch.Stop();                     //运算结束
                    TimeSpan ts      = stopWatch.Elapsed; //得到stopWatch所监测到的时间
                    StringBuilder sb = new StringBuilder();

                    //弹出消息框,告知用户结果
                    MessageBox.Show(rm.status ? "Succeed" : "Failure", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    //以下是记录一些稍微详细的信息,记录在界面右侧Textbox中
                    if (rm.status)
                    {
                        sb.Append(String.Format("{4}{5}Source Filename:{0}\r\nEncrypted 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(); }));
                    }
                });
            }
        }
Пример #3
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(); }));
                    }
                });
            }
        }