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);
        }
    }
}


本文转载:CSDN博客