.NET使用DES加密解密

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;
using System.Security.Cryptography;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Hosting;  

 

namespace Test1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        //DES密钥(必须8字节)可以使字母可以是数字也可以是组合
        private string _DesKey = "@#$$%^A1";
        private string _ivkey = "zhucheng";
     
        public string DesKey
        {
            set
            {
                DesKey = value;
            }
        }
        #region
        public string DESEncrypt(string toEncrypt)
        {
            //定义DES加密服务提供类
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            //加密字符串转换为byte数组
            byte[] inputByte = System.Text.ASCIIEncoding.UTF8.GetBytes(toEncrypt);
            //加密密匙转化为byte数组
            //byte[] key = Encoding.ASCII.GetBytes(_DesKey); //DES密钥(必须8字节)
            byte[] key = ASCIIEncoding.ASCII.GetBytes(_DesKey);
            byte[] ivkey = ASCIIEncoding.ASCII.GetBytes(_ivkey);
            des.Key = key;
            des.IV =ivkey;
            //创建其支持存储区为内存的流
            MemoryStream ms = new MemoryStream();
            //定义将数据流链接到加密转换的流
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByte, 0, inputByte.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                //向可变字符串追加转换成十六进制数字符串的加密后byte数组。
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();

        }
        #endregion
        #region
        public string DESDecrypt(string toDecrypt)
        {
           
            //定义DES加密解密服务提供类
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            //加密密匙转化为byte数组
            //byte[] key = Encoding.ASCII.GetBytes(_DesKey);
            byte[] key = ASCIIEncoding.ASCII.GetBytes(_DesKey);
            byte[] ivkey = ASCIIEncoding.ASCII.GetBytes(_ivkey);
            des.Key = key;
            des.IV = ivkey;
            //将被解密的字符串每两个字符以十六进制解析为byte类型,组成byte数组
            int length = (toDecrypt.Length / 2);
            byte[] inputByte = new byte[length];
            for (int index = 0; index < length; index++)
            {
                string substring = toDecrypt.Substring(index * 2, 2);
                inputByte[index] =(byte) Convert.ToInt32(substring,16);
            }
          
            //创建其支持存储区为内存的流
            MemoryStream ms = new MemoryStream();
            //定义将数据流链接到加密转换的流
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByte, 0, inputByte.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();

            //HttpContext context = new HttpContext(;
            //context.Server.UrlDecode("asdfsdf");
            //return HttpContext.Current.Server.UrlDecode(System.Text.Encoding.Default.GetString(ms.ToArray()));
            //return System.Text.Encoding.UTF8.GetString(ms.ToArray());
           //上面的解密多少有点问题,还是这个好
            return System.Web.HttpUtility.UrlDecode(ms.ToArray(), System.Text.Encoding.UTF8);
           


        }
        #endregion

  

 

        private void btnencrypt_Click(object sender, EventArgs e)
        {
            string str = this.tbbefore.Text;
            string strencryptdata = DESEncrypt(str);
            this.tbjbefore.Text = strencryptdata;
            this.tbafter.Text = strencryptdata;
        }

        private void btndecrypt_Click(object sender, EventArgs e)
        {
            string dcry = this.tbjbefore.Text;
            this.tbjafter.Text = DESDecrypt(dcry);
        }
    }
}

 

你可能感兴趣的:(.NET使用DES加密解密)