C#小程序—字符串加密

         字符串是经常使用的类型,怎样保存字符串才能最安全呢?答案就是加密。

         可以利用C# 中 CryptoStream 来加密和解密字符串。CryptoStream方法如下:

          

               程序运行结果如下:

             

         主窗口响应代码如下:

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

          自定义类代码如下:

public class Encrypt
    {

        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();
                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();
                P_CryptStream_Stream.Close();//关闭加密流
                P_Stream_MS.Close();//关闭内存流
                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);
            }
        }
    }

                  代码下载地址:下载

               

posted @ 2012-12-08 16:25  Finlay Liu  阅读(374)  评论(0编辑  收藏  举报