Exemplo n.º 1
0
        //找到隐藏文件,改回后缀,解密,恢复为隐藏的已解密的txt文件/
        public static FileStream DecryptionFile(string filePath, string encryptKey)
        {
            FileStream fs             = null;
            string     sourcefileName = filePath;

            if (!File.Exists(sourcefileName))
            {
                return(fs);
            }

            string destfileName = System.IO.Path.ChangeExtension(sourcefileName, ".txt");

            File.Move(sourcefileName, destfileName);
            File.SetAttributes(destfileName, FileAttributes.Normal);
            FileEncrypt.DecryptFile(destfileName, encryptKey, (int max, int value) => { });
            fs = new FileStream(destfileName, FileMode.Open);
            fs = new FileStream(sourcefileName, FileMode.Open);
            return(fs);
        }
Exemplo n.º 2
0
        private void _decrypt_file()
        {
            //未实例化密钥管理类
            if (_key_manager == null)
            {
                return;
            }
            try
            {
                if (File.Exists(_output_path + ".decrypting"))
                {
                    File.Delete(_output_path + ".decrypting"); //删除已有的冲突文件
                }
                //重命名
                if (_output_path.EndsWith(".bcsd"))
                {
                    //xxxxx.bcsd -> xxxxx.decrypting
                    _output_path = _output_path.Substring(0, _output_path.Length - 5);
                    File.Move(_output_path + ".bcsd", _output_path + ".decrypting");
                }
                else
                {
                    File.Move(_output_path, _output_path + ".decrypting"); //xxxxx.xx -> xxxxx.xx.decrypting
                }
                var fs         = new FileStream(_output_path + ".decrypting", FileMode.Open, FileAccess.Read, FileShare.None);
                var identifier = fs.ReadByte(); //读取文件首字节判断加密类型
                fs.Close();

                if (identifier == FileEncrypt.FLG_DYNAMIC_KEY && _key_manager.HasRsaKey)
                {
                    try
                    {
                        //解密文件并删除加密文件
                        FileEncrypt.DecryptFile(_output_path + ".decrypting", _output_path, _key_manager.RSAPrivateKey);
                        File.Delete(_output_path + ".decrypting");
                    }
                    catch
                    {
                        //出错后取消解密,将文件重新命名
                        File.Move(_output_path + ".decrypting", _output_path);
                    }
                }
                else if (identifier == FileEncrypt.FLG_STATIC_KEY && _key_manager.HasAesKey)
                {
                    try
                    {
                        FileEncrypt.DecryptFile(_output_path + ".decrypting", _output_path, _key_manager.AESKey, _key_manager.AESIV);
                        File.Delete(_output_path + ".decrypting");
                    }
                    catch
                    {
                        File.Move(_output_path + ".decrypting", _output_path);
                    }
                }
                else
                {
                    //加密类型不匹配,重新命名
                    File.Move(_output_path + ".decrypting", _output_path);
                }
            }
            catch (Exception ex)
            {
                Tracer.GlobalTracer.TraceError(ex);
            }
        }
Exemplo n.º 3
0
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            #region 验证
            if (txtPwd.Text == "")
            {
                MessageBox.Show("密码不能为空", "提示");
                return;
            }
            #endregion

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj)
                {
                    try
                    {
                        InvokeDelegate invokeDelegate = delegate()
                        {
                            pbFile.Value            = 0;
                            lblProgressFile.Text    = "0%";
                            pbDir.Visible           = false;
                            lblProgressDir.Visible  = false;
                            pbFile.Visible          = false;
                            lblProgressFile.Visible = false;
                            lblShowPath.Text        = "解密文件:" + openFileDialog1.FileName;
                            lblShowPath.Visible     = true;
                            DisableBtns();
                        };
                        InvokeUtil.Invoke(this, invokeDelegate);
                        DateTime t1 = DateTime.Now;
                        FileEncrypt.DecryptFile(openFileDialog1.FileName, txtPwd.Text, RefreshFileProgress);
                        DateTime t2 = DateTime.Now;
                        string t    = t2.Subtract(t1).TotalSeconds.ToString("0.00");
                        if (MessageBox.Show("解密成功,耗时" + t + "秒", "提示") == DialogResult.OK)
                        {
                            invokeDelegate = delegate()
                            {
                                EnableBtns();
                            };
                            InvokeUtil.Invoke(this, invokeDelegate);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (MessageBox.Show("解密失败:" + ex.Message, "提示") == DialogResult.OK)
                        {
                            InvokeDelegate invokeDelegate = delegate()
                            {
                                EnableBtns();
                                tryCount++;
                                if (tryCount == maxTryCount)
                                {
                                    this.Close();
                                }
                            };
                            InvokeUtil.Invoke(this, invokeDelegate);
                        }
                    }
                }));
                thread.Start();
            }
        }