C# DES加密(原创)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Globalization;


namespace SoftRegister
{
    /// <summary>
    /// DES对称加密、解密
    /// </summary>
    public class DES
    {
        /// <summary>
        ///  创建加密密钥,随机产生
        /// </summary>
        private string CreateKey()
        {
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
            return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
        }

        /// <summary>
        /// 加密Key字符串
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private string EncryptKey(string key)
        {
            byte[] data = Encoding.UTF8.GetBytes(key);
            string v = "";
            for (int i = data.Length - 1; i >= 0; i--)
            {
                v += data[i].ToString("X2");
            }
            return v;
        }

        /// <summary>
        /// 解密Key字符串
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private string DecryptKey(string key)
        {
            List<string> list = new List<string>();
            string v = "";
            for (int i = 0; i < key.Length; i++)
            {
                list.Add(key[i].ToString() + key[++i].ToString());
            }
            for (int i = list.Count-1; i >= 0; i--)
            {
                v += ((char)(byte.Parse(list[i], NumberStyles.HexNumber))).ToString();
            }
            return v;
        }

        /// <summary>
        ///  加密字符串
        /// </summary>
        private string EncryptString(string inputString, string key)
        {
            byte[] data = Encoding.UTF8.GetBytes(inputString);
            DESCryptoServiceProvider dessp = new DESCryptoServiceProvider();
            dessp.Key = ASCIIEncoding.ASCII.GetBytes(key);
            dessp.IV = ASCIIEncoding.ASCII.GetBytes(key);
            ICryptoTransform desencrypt = dessp.CreateEncryptor();
            byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
            string v = "";
            for (int i = 0; i < result.Length; i++)
            {
                v += (result[i]).ToString("X2");
            }
            return v;
        }

        /// <summary>
        ///  解密字符串
        /// </summary>
        private string DecryptString(string inputString, string key)
        {
            List<string> sInput = new List<string>();
            for (int i = 0; i < inputString.Length; i++)
            {
                sInput.Add(inputString[i].ToString() + inputString[++i].ToString());
            }
            int leng = sInput.Count;
            byte[] data = new byte[leng];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
            }
            DESCryptoServiceProvider dessp = new DESCryptoServiceProvider();
            dessp.Key = ASCIIEncoding.ASCII.GetBytes(key);
            dessp.IV = ASCIIEncoding.ASCII.GetBytes(key);
            ICryptoTransform desencrypt = dessp.CreateDecryptor();
            byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
            return Encoding.UTF8.GetString(result);
        }

        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="sourceStr"></param>
        /// <returns></returns>
        public string EncryptString(string sourceStr)
        {
            string key = CreateKey();
            string value = EncryptString(sourceStr, key);
            key = EncryptKey(key);
            return value + key;
        }

        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="sourceStr"></param>
        /// <returns></returns>
        public string DecryptString(string sourceStr)
        {
            int keyLeng = 8 * 2;
            sourceStr = sourceStr.Trim();
            int leng = sourceStr.Length;
            string tStr = sourceStr.Substring(0, leng - keyLeng);
            string key = sourceStr.Substring(leng - keyLeng);
            key = DecryptKey(key);
            string v = DecryptString(tStr, key);
            return v;
        }

    }
}

posted @ 2011-12-06 09:58  怡馨  阅读(478)  评论(0编辑  收藏  举报