天天看点

C# 关于压缩、加密、解压问题

本文探讨的是关于C#TXT文件的压缩、加密以及解压问题,采用的是密钥方式,可以先进行加密再进行压缩包导入到桌面。

界面如下:

C# 关于压缩、加密、解压问题

源文件:想要压缩的TXT文件,里边必须有东西,不然代码会报错

压缩文件:要压缩到桌面的路径

代码如下:

public partial class 压缩文件 : Form
    {
        public 压缩文件()
        {
            InitializeComponent();
        }
        string str = "";
        string a = "";
        private void btnYasuo_Click(object sender, EventArgs e)
        {
            if(String.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("请选择源文件","信息提示");
                return;
            }
            if (String.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("请输入压缩文件名", "信息提示");
                return;
            }
            string str1 = textBox1.Text;
            string str2 = textBox2.Text.Trim() + ".zip";
            str = str2;
            byte[] myByte = null;
            FileStream myStream = null;
            FileStream myDesStream = null;
            GZipStream myComStream = null;
            try
            {
                myStream = new FileStream(str1, FileMode.Open, FileAccess.Read, FileShare.Read);
                myByte = new byte[myStream.Length];
                myStream.Read(myByte, 0, myByte.Length);
                myDesStream = new FileStream(str2, FileMode.OpenOrCreate, FileAccess.Write);
                myComStream = new GZipStream(myDesStream, CompressionMode.Compress, true);
                myComStream.Write(myByte, 0, myByte.Length);
                MessageBox.Show("压缩文件完成");
            }
            catch(Exception )
            {

            }
            finally
            {
                myStream.Close();
                myComStream.Close();
                myDesStream.Close();
                
            }
        }

        private void btnJiami_Click(object sender, EventArgs e)
        {
            if(textBox1 .Text =="")
            {
                MessageBox.Show("请选择要加密的文件");
            }
            else
            {
                try
                {
                    string strPath = textBox1.Text;
                    int intLent = strPath.LastIndexOf("\\") + 1;
                    int intLong = strPath.Length;
                    string strName = strPath.Substring(intLent, intLong - intLent);
                    int intTxt = strName.LastIndexOf(".");
                    int intTextLeng = strName.Length;
                    string strTxt = strName.Substring(intTxt, intTextLeng - intTxt);
                    strName = strName.Substring(0, intTxt);
                    string strOutName = strPath.Substring(0, strPath.LastIndexOf("\\") + 1) + strName +"Out"+ strTxt;
                    a = strOutName;
                    str = strPath.Substring(0, strPath.LastIndexOf("\\") + 1) + strName + "Out";
                    byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 27, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
                    byte[] IV = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 27, 98, 26, 67, 29, 99 };
                    RijndaelManaged myRijndael = new RijndaelManaged();
                    FileStream fsOut = File.Open(strOutName, FileMode.Create, FileAccess.Write);
                    FileStream fsIn = File.Open(strPath, FileMode.Open, FileAccess.Read);
                    CryptoStream csDexrypt = new CryptoStream(fsOut, myRijndael.CreateEncryptor(key, IV), CryptoStreamMode.Write);
                    BinaryReader br = new BinaryReader(fsIn);
                    csDexrypt.Write(br.ReadBytes((int)fsIn.Length), 0, (int)fsIn.Length);
                    csDexrypt.FlushFinalBlock();
                    csDexrypt.Close();
                    fsIn.Close();
                    fsOut.Close();
                    if(MessageBox.Show ("加密成功!加密后的文件名及路径为:\n"+strOutName +",是否删除源文件","信息提示",MessageBoxButtons.YesNo)==DialogResult .Yes )
                    {
                        File.Delete(strPath);
                        //textBox1.Text = "";

                    }
                    else
                    {
                        //textBox1.Text = "";
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    yasuo();
                }

            }
        }

        private void btnJiemi_Click(object sender, EventArgs e)
        {
            if(textBox1 .Text =="")
            {
                MessageBox.Show("请选择解密的文件路径");
            }
            else
            {
                string strPath = textBox1.Text;
                int intLent = strPath.LastIndexOf("\\") + 1;
                int intLong = strPath.Length;
                string strName = strPath.Substring(intLent, intLong - intLent);
                int intTxt = strName.LastIndexOf(".");
                int intTextLeng = strName.Length;
                string strTxt = strName.Substring(intTxt, intTextLeng - intTxt);
                strName = strName.Substring(0, intTxt);
                if(strName .LastIndexOf ("Out")!=-1)
                {
                    strName = strName.Substring(0, strName.LastIndexOf("Out"));

                }
                else
                {
                    strName = strName + "In";
                }
                string strInName = strPath.Substring(0, strPath.LastIndexOf("\\") + 1) + strName + ".txt";
                byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 27, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
                byte[] IV = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 27, 98, 26, 67, 29, 99 };
                RijndaelManaged myRijndael = new RijndaelManaged();
                FileStream fsOut = File.Open(strPath, FileMode.Open, FileAccess.Read);
                CryptoStream csDexrypt = new CryptoStream(fsOut, myRijndael.CreateEncryptor(key, IV), CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(csDexrypt);
                StreamWriter sw = new StreamWriter(strInName);
                sw.Write(sr.ReadToEnd());
                sw.Flush();
                sw.Close();
                sr.Close();
                fsOut.Close();
                if (MessageBox.Show("解密成功!解密后的文件名及路径为:\n" + strInName + ",是否删除源文件", "信息提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    File.Delete(strPath);
                    textBox1.Text = "";

                }
                else
                {
                    textBox1.Text = "";
                }
            }
        }
        private void yasuo()
        {
            if (String.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("请选择源文件", "信息提示");
                return;
            }
            if (String.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("请输入压缩文件名", "信息提示");
                return;
            }
            string str1 = a;
            string str2 = str+ ".zip";
            
            byte[] myByte = null;
            FileStream myStream = null;
            FileStream myDesStream = null;
            GZipStream myComStream = null;
            try
            {
                myStream = new FileStream(str1, FileMode.Open, FileAccess.Read, FileShare.Read);
                myByte = new byte[myStream.Length];
                myStream.Read(myByte, 0, myByte.Length);
                myDesStream = new FileStream(str2, FileMode.OpenOrCreate, FileAccess.Write);
                myComStream = new GZipStream(myDesStream, CompressionMode.Compress, true);
                myComStream.Write(myByte, 0, myByte.Length);
                MessageBox.Show("压缩文件完成");
            }
            catch (Exception)
            {

            }
            finally
            { 
                myStream.Close();
                myComStream.Close();
                myDesStream.Close();

            }
}

           

本文代码中可能对于上述按钮实现的功能不太对应,可能点击压缩或者加密按钮,直接就会实现两个功能,因为两个方法之间会有调用关系,我写的是加密完成之后直接进行压缩到桌面,而不是说压缩、加密分开实现,当然你也可以把方法中调用的另一个方法剥离开即可实现按钮对应的功能。

谢谢大家!