C#中DES加密和解密

using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Sample3
{
    class Program
    {
        static void Main(string[] args)
        {
            var a = "0234061adsa=d&张三?#"; //待加密字符串
            var key = "66669999"; //加密密匙
            var b = EncryptDES(a, key); //加密后的字符串
            var c = DecryptDES(b, key); //解密后的字符串
            Console.WriteLine(b);
            Console.WriteLine(c);
            Console.ReadKey();
        }

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密匙,要求为8位</param>
        /// <returns></returns>
        public static string EncryptDES(string encryptString, string encryptKey)
        {
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                des.Key = UTF8Encoding.UTF8.GetBytes(encryptKey);// ASCIIEncoding.ASCII.GetBytes(encryptKey);
                des.IV = UTF8Encoding.UTF8.GetBytes(encryptKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }

                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:x2}", b);
                }
                ret.ToString();

                return ret.ToString();
            }
        }

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns></returns>
        public static string DecryptDES(string decryptString, string decryptKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            //put  the  input  string  into  the  byte  array  
            byte[] inputbytearray = new byte[decryptString.Length / 2];
            for (int x = 0; x < decryptString.Length / 2; x++)
            {
                int i = (Convert.ToInt32(decryptString.Substring(x * 2, 2), 16));
                inputbytearray[x] = (byte)i;
            }

            //建立加密对象的密钥和偏移量,此值重要,不能修改  
            des.Key = UTF8Encoding.UTF8.GetBytes(decryptKey);
            des.IV = UTF8Encoding.UTF8.GetBytes(decryptKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            //flush  the  data  through  the  crypto  stream  into  the  memory  stream  
            cs.Write(inputbytearray, 0, inputbytearray.Length);
            cs.FlushFinalBlock();

            //get  the  decrypted  data  back  from  the  memory  stream  
            //建立stringbuild对象,createdecrypt使用的是流对象,必须把解密后的文本变成流对象  
            StringBuilder ret = new StringBuilder();

            return Encoding.UTF8.GetString(ms.ToArray(), 0, ms.ToArray().Length);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

smartsmile2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值