コード例 #1
0
        private void BtnStart_Click(object sender, EventArgs e)
        {
            //容错
            string ERRMSG = "错误";

            if (TxtPassword.Text.Length == 0)
            {
                ERRMSG += "\n请输入密码!";
            }
            if (TxtSave.Text.Length == 0)
            {
                ERRMSG += "\n请指定输出路径!";
            }
            if (LstFile.Items.Count <= 0)
            {
                ERRMSG += "\n请添加加密文件!";
            }
            if (ERRMSG != "错误")
            {
                MessageBox.Show(ERRMSG, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //读取文件
            var ask = MessageBox.Show("确定开始加密吗?\n提示:加密需要耐心,请不要操作程序~", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (ask == DialogResult.No)
            {
                return;
            }
            Visible = false;
            FrmWaitng frm = new FrmWaitng();

            frm.Show();
            FileStream fout = File.Create(TxtSave.Text);

            if (fout == null)
            {
                ERRMSG += "\n文件写失败,请考虑是否被其他程序占用";
                MessageBox.Show(ERRMSG, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            make_head(fout); //写文件头
            foreach (ListViewItem lvi in LstFile.Items)
            {
                FileStream fin = File.OpenRead(lvi.SubItems[1].Text);//遍历列表,加密
                if (fin == null)
                {
                    ERRMSG += "\n文件读失败,请考虑是否被其他程序占用";
                    MessageBox.Show(ERRMSG, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                make_file_head(fin, fout);            //制作单文件头
                Encrypt(fin, fout, TxtPassword.Text); //加密并写入
                fin.Close();                          //关
            }
            fout.Close();                             //关
            frm.Close();
            Visible = true;
            MessageBox.Show("加密完成!", "加密成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
コード例 #2
0
        private void BtnStart_Click(object sender, EventArgs e)
        {
            string ERRMSG = "错误";//错误处理

            if (TxtPassword.Text.Length == 0)
            {
                ERRMSG += "\n请输入密码!";
            }
            if (TxtSave.Text.Length == 0)
            {
                ERRMSG += "\n请指定输出路径!";
            }
            if (ERRMSG != "错误")
            {
                MessageBox.Show(ERRMSG, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            var ask = MessageBox.Show("确定开始解密吗?\n提示:解密需要耐心,请不要操作程序~", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (ask == DialogResult.No)
            {
                return;
            }
            Visible = false;
            FrmWaitng frm = new FrmWaitng();

            frm.Show();
            FileStream fin = File.OpenRead(PickerIn.FileName);//读输入文件

            if (fin == null)
            {
                ERRMSG += "\n文件读失败,请考虑是否被其他程序占用";
                MessageBox.Show(ERRMSG, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            chkhead(fin);                     //再次检验,防止之间的修改
            while (fin.Position < fin.Length) //没有读完,就一直读
            {
                FileStream fout = null;
                long       len  = read_file_head(fin, ref fout); //读单文件头,更新fout指针
                if (fout == null)                                //容错
                {
                    ERRMSG += "\n文件写失败,请考虑是否被其他程序占用";
                    MessageBox.Show(ERRMSG, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                Decrypt(fin, fout, TxtPassword.Text, len);//解密单文件
                fout.Close();
            }
            frm.Close();
            Visible = true;
            MessageBox.Show("解密完成!", "解密成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }