效果图:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Example46
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void label2_Click(object sender, EventArgs e)
        {
           
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text.Length == 4)
            {
                textBox3.Text = new Encrypt().ToEncryt(textBox2.Text, textBox1.Text);
            }
            else
            {
                MessageBox.Show("密钥长度不对", "提示");
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox5.Text.Length == 4)
            {
                textBox6.Text = new Encrypt().ToDecryt(textBox5.Text, textBox4.Text);
            }
            else
            {
                MessageBox.Show("密钥长度不对", "提示");
            }
        }
    }
}
类文件 :

using System;

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Example46
{
    public class Encrypt
    {
        internal string ToEncryt(string encryptKey, string encryptStr)
        {
            try
            {
                byte[] P_str_key = Encoding.Unicode.GetBytes(encryptKey); //获得密钥字节序列
                byte[] P_str_str = Encoding.Unicode.GetBytes(encryptStr); //获取需要加密的字符串字节序列
                MemoryStream P_stream_MS = new MemoryStream(); //创建内存流
                CryptoStream P_stream_CS = new CryptoStream(   //创建加密流对象 
                    P_stream_MS, new DESCryptoServiceProvider().CreateEncryptor(P_str_key, P_str_key),
                    CryptoStreamMode.Write);
                P_stream_CS.Write(P_str_str, 0, P_str_str.Length); //向加密流中写入字节序列
                P_stream_CS.FlushFinalBlock(); //将数据压入基础流
                byte[] P_str_temp = P_stream_MS.ToArray(); //从内存流中获取数据
                P_stream_CS.Close();  //关闭流
                P_stream_MS.Close();  //关闭流
                return Convert.ToBase64String(P_str_temp);
            }
            catch (CryptographicException ce)
            {
                throw new Exception(ce.Message);
            }
        }
        internal string ToDecryt(string encryptKey, string encryptStr)
        {
            try
            {
                byte[] P_str_key = Encoding.Unicode.GetBytes(encryptKey); //将密钥转换为字符串
                byte[] P_str_str = Convert.FromBase64String(encryptStr); //将加密后的字符串转换为字节序列
                MemoryStream P_stream_MS = new MemoryStream(P_str_str);  //创建内存流对象并写入数据
                CryptoStream P_stream_CS = new CryptoStream(P_stream_MS, new DESCryptoServiceProvider().
                    CreateDecryptor(P_str_key, P_str_key), CryptoStreamMode.Read);  //创建加密流对象 
                byte[] P_string_TP = new byte[200];   //创建字节流对象
                MemoryStream P_stream_TP = new MemoryStream();  //
                int iCount = 0; // 创建计数器
                while ((iCount = P_stream_CS.Read(P_string_TP, 0, P_string_TP.Length))>0)
                {
                    P_stream_TP.Write(P_string_TP, 0, iCount); //将解密后的数据放入 内存流     
                }
                return Encoding.Unicode.GetString(P_stream_TP.ToArray()); //返回 内存流 中的字符串
            }
            catch (CryptographicException ce)
            {
                throw new Exception(ce.Message);
            }
        }
    }
}
posted on 2011-10-23 12:33  C#_初学者  阅读(273)  评论(0编辑  收藏  举报