C#对字符串进行加密解密

首先上效果图

 

 加解密接口

 internal string ToEncrypt(string encryptKey, string str)
        {
            try
            {
                byte[] P_byte_key = //将密钥字符串转换为字节序列
                    Encoding.Unicode.GetBytes(encryptKey);
                byte[] P_byte_data = //将字符串转换为字节序列
                    Encoding.Unicode.GetBytes(str);
                MemoryStream P_Stream_MS = //创建内存流对象
                    new MemoryStream();               
                {
                    using (CryptoStream P_CryptStream_Stream = new CryptoStream(P_Stream_MS, new DESCryptoServiceProvider().CreateEncryptor(P_byte_key, P_byte_key),CryptoStreamMode.Write))
                    {
                        P_CryptStream_Stream.Write(P_byte_data, 0, P_byte_data.Length);//向加密流中写入字节序列
                        P_CryptStream_Stream.FlushFinalBlock();//将数据压入基础流
                        byte[] P_bt_temp = P_Stream_MS.ToArray();//从内存流中获取字节序列
                        return Convert.ToBase64String(P_bt_temp);
                    }
                }
               
            }
            catch (CryptographicException ce)
            {
                throw new Exception(ce.Message);
            }
        }

        internal string ToDecrypt(string encryptKey, string str)
        {
            try
            {
                byte[] P_byte_key = //将密钥字符串转换为字节序列
                    Encoding.Unicode.GetBytes(encryptKey);
                byte[] P_byte_data = //将加密后的字符串转换为字节序列
                    Convert.FromBase64String(str);
                MemoryStream P_Stream_MS =//创建内存流对象并写入数据
                    new MemoryStream(P_byte_data);
                CryptoStream P_CryptStream_Stream = //创建加密流对象
                    new CryptoStream(P_Stream_MS,new DESCryptoServiceProvider().
                    CreateDecryptor(P_byte_key, P_byte_key),CryptoStreamMode.Read);
                byte[] P_bt_temp = new byte[200];//创建字节序列对象
                MemoryStream P_MemoryStream_temp =//创建内存流对象
                    new MemoryStream();
                int i = 0;//创建记数器
                while ((i = P_CryptStream_Stream.Read(//使用while循环得到解密数据
                    P_bt_temp, 0, P_bt_temp.Length)) > 0)
                {
                    P_MemoryStream_temp.Write(//将解密后的数据放入内存流
                        P_bt_temp, 0, i);
                }
                return //方法返回解密后的字符串
                    Encoding.Unicode.GetString(P_MemoryStream_temp.ToArray());
            }
            catch (CryptographicException ce)
            {
                throw new Exception(ce.Message);
            }
        }

调用过程

        private void btn_Encrypt_Click(object sender, EventArgs e)
        {
            if (txt_password.Text.Length == 4)//判断加密密钥长度是否正确
            {
                try
                {
                    txt_EncryptStr.Text = //调用实例ToEncrypt方法得到加密后的字符串
                        new Encrypt().ToEncrypt(
                        txt_password.Text, txt_str.Text);                   
                }
                catch (Exception ex)//捕获异常
                {
                    MessageBox.Show(ex.Message);//输出异常信息
                }
            }
            else
            {
                MessageBox.Show("密钥长度不符!", "提示");//提示用户输入密钥长度不正确
            }
        }
        private void btn_UnEncrypt_Click(object sender, EventArgs e)
        {
            if (txt_password2.Text.Length == 4)//判断加密密钥长度是否正确
            {
                try
                {
                    txt_str2.Text = //调用ToDecrypt方法得到解密后的字符串
                        new Encrypt().ToDecrypt(
                        txt_password2.Text, txt_EncryptStr2.Text);
                }
                catch (Exception ex)//捕获异常
                {
                    MessageBox.Show(ex.Message);//输出异常信息
                }
            }
            else
            {
                MessageBox.Show("密钥长度不符!", "提示");//提示用户输入密钥长度不正确
            }
        }

 

posted @ 2022-07-09 23:29  明志德道  阅读(2630)  评论(0编辑  收藏  举报