ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2022-07-09 23:33:37  阅读:289  来源: 互联网

标签:加密 MemoryStream Stream C# 解密 temp new byte txt


首先上效果图

 

 加解密接口

 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("密钥长度不符!", "提示");//提示用户输入密钥长度不正确
            }
        }

 

标签:加密,MemoryStream,Stream,C#,解密,temp,new,byte,txt
来源: https://www.cnblogs.com/for-easy-fast/p/16462251.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有