C# 加密解密2

//加密文件

        private void button26_Click(object sender, EventArgs e)

        {

            DirectoryInfo info = new DirectoryInfo(Directory.GetCurrentDirectory() + "\\data\\");

            foreach (FileInfo info2 in info.GetFiles("*.txt"))

            {

                try

                {

                    string path = info2.FullName;

                    var text = File.ReadAllText(path);

                    string Encrypt = EncryptString(text, "12345678");

                    if (File.Exists(path))

                    {

                        File.Delete(path);

                    }

                    using (TextWriter textWriter = File.CreateText(path))

                    {

                        textWriter.Write(Encrypt);

                        textWriter.Flush();

                    }

                }

                catch (Exception)

                {

                }

            }

            MessageBox.Show("加密成功");

        }

        public string EncryptString(string data, string key)

        {

            string str = string.Empty;

            if (string.IsNullOrEmpty(data))

            {

                return str;

            }

            MemoryStream ms = new MemoryStream();

            byte[] myKey = Encoding.UTF8.GetBytes(key);

            byte[] myIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            DESCryptoServiceProvider myProvider = new DESCryptoServiceProvider();

            CryptoStream cs = new CryptoStream(ms, myProvider.CreateEncryptor(myKey, myIV), CryptoStreamMode.Write);

            try

            {

                byte[] bs = Encoding.UTF8.GetBytes(data);

                cs.Write(bs, 0, bs.Length);

                cs.FlushFinalBlock();

                str = Convert.ToBase64String(ms.ToArray());

            }

            finally

            {

                cs.Close();

                ms.Close();

            }

            return str;

        }

        //解密文件

        private void button27_Click(object sender, EventArgs e)

        {

            DirectoryInfo info = new DirectoryInfo(Directory.GetCurrentDirectory() + "\\data\\");

            foreach (FileInfo info2 in info.GetFiles("*.txt"))

            {

                try

                {

                    string path = info2.FullName;

                    var text = File.ReadAllText(path);

                    string Encrypt = DecryptString(text, "12345678");

                    if (File.Exists(path))

                    {

                        File.Delete(path);

                    }

                    using (TextWriter textWriter = File.CreateText(path))

                    {

                        textWriter.Write(Encrypt);

                        textWriter.Flush();

                    }

                }

                catch (Exception)

                {

                }

            }

            MessageBox.Show("解密成功");

        }

        public string DecryptString(string data, string key)

        {

            string str = string.Empty;

            if (string.IsNullOrEmpty(data))

            {

                throw new Exception("data is empty");

            }

            MemoryStream ms = new MemoryStream();

            byte[] myKey = Encoding.UTF8.GetBytes(key);

            byte[] myIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            DESCryptoServiceProvider myProvider = new DESCryptoServiceProvider();

            CryptoStream cs = new CryptoStream(ms, myProvider.CreateDecryptor(myKey, myIV), CryptoStreamMode.Write);

            try

            {

                byte[] bs = Convert.FromBase64String(data);

                cs.Write(bs, 0, bs.Length);

                cs.FlushFinalBlock();

                str = Encoding.UTF8.GetString(ms.ToArray());

            }

            finally

            {

                cs.Close();

                ms.Close();

            }

            return str;

        }

你可能感兴趣的:(C# 加密解密2)