.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 = "zero";
     
        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);
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
压缩包 : MD5 skey8位加密(文件).zip 列表 MD5 skey8位加密(文件)/ MD5 skey8位加密(文件)/bin/ MD5 skey8位加密(文件)/Form1.Designer.vb MD5 skey8位加密(文件)/Form1.resx MD5 skey8位加密(文件)/Form1.vb MD5 skey8位加密(文件)/MD5 skey8位加密(文件).vbproj MD5 skey8位加密(文件)/MD5 skey8位加密(文件).vbproj.user MD5 skey8位加密(文件)/My Project/ MD5 skey8位加密(文件)/My Project/Application.Designer.vb MD5 skey8位加密(文件)/My Project/Application.myapp MD5 skey8位加密(文件)/My Project/AssemblyInfo.vb MD5 skey8位加密(文件)/My Project/Resources.Designer.vb MD5 skey8位加密(文件)/My Project/Resources.resx MD5 skey8位加密(文件)/My Project/Settings.Designer.vb MD5 skey8位加密(文件)/My Project/Settings.settings MD5 skey8位加密(文件)/obj/ MD5 skey8位加密(文件)/obj/Debug/ MD5 skey8位加密(文件)/obj/Debug/CoreCompileInputs.cache MD5 skey8位加密(文件)/obj/Debug/DesignTimeResolveAssemblyReferences.cache MD5 skey8位加密(文件)/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).exe MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).pdb MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).vbproj.FileListAbsolute.txt MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).vbproj.GenerateResource.Cache MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).vbprojResolveAssemblyReference.cache MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).xml MD5 skey8位加密(文件)/obj/Debug/TempPE/ MD5 skey8位加密(文件)/obj/Debug/TempPE/My Project.Resources.Designer.vb.dll MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.exe MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.Form1.resources MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.pdb MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.Resources.resources MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.vbproj.FileListAbsolute.txt MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.vbproj.GenerateResource.Cache MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.xml MD5 skey8位加密(文件)/obj/Release/ MD5 skey8位加密(文件).sln MD5 skey8位加密(文件).v11.suo
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值