C# DES加密解密

加密后生成Base64字符串,并去除'='字符。

加密后替换掉'+',这样加密后的字符串可以作为url参数传递。

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

namespace QuaEdu.Helper
{
    /// <summary>
    /// DES加密/解密类/// </summary>
    public class DESHelper
    {
        #region 默认密钥
        /// <summary>
        /// 默认密钥
        /// </summary>
        private static readonly string m_key = "zcl";
        #endregion

        #region ========加密========
        /// <summary> 
        /// 加密数据 
        /// </summary> 
        /// <param name="Text">原文</param> 
        /// <returns>密文</returns> 
        public static string Encrypt(string Text)
        {
            return Encrypt(Text, m_key);
        }

        /// <summary> 
        /// 加密数据 
        /// </summary> 
        /// <param name="Text">原文</param> 
        /// <param name="sKey">密钥</param> 
        /// <returns>密文</returns> 
        public static string Encrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider desKey = new DESCryptoServiceProvider();

            byte[] inputByteArray = Encoding.Default.GetBytes(Text);
            byte[] keyByteArray = Encoding.Default.GetBytes(sKey);

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            md5.ComputeHash(keyByteArray);

            desKey.Key = HalveByteArray(md5.Hash);
            desKey.IV = HalveByteArray(md5.Hash);

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, desKey.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            return Convert.ToBase64String(ms.ToArray()).Replace("=", "").Replace("+", "-").Replace("/", "*");
        }
        #endregion

        #region ========解密========
        /// <summary> 
        /// 解密数据 
        /// </summary> 
        /// <param name="Text">密文</param> 
        /// <returns>原文</returns> 
        public static string Decrypt(string Text)
        {
            return Decrypt(Text, m_key);
        }

        /// <summary> 
        /// 解密数据 
        /// </summary> 
        /// <param name="Text">密文</param> 
        /// <param name="sKey">密钥</param> 
        /// <returns>原文</returns> 
        public static string Decrypt(string Text, string sKey)
        {
            StringBuilder sb = new StringBuilder();
            if (Text.Length % 4 != 0)
            {
                for (int i = 0; i < 4 - Text.Length % 4; i++)
                {
                    sb.Append("=");
                }
            }
            Text = Text.Replace("-", "+").Replace("*", "/") + sb.ToString();

            DESCryptoServiceProvider desKey = new DESCryptoServiceProvider();

            byte[] inputByteArray = Convert.FromBase64String(Text);
            byte[] keyByteArray = Encoding.Default.GetBytes(sKey);

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            md5.ComputeHash(keyByteArray);

            desKey.Key = HalveByteArray(md5.Hash);
            desKey.IV = HalveByteArray(md5.Hash);

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, desKey.CreateDecryptor(), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            return Encoding.Default.GetString(ms.ToArray());
        }
        #endregion

        #region 把字节数组缩短一半
        /// <summary>
        /// 把字节数组缩短一半
        /// </summary>
        private static byte[] HalveByteArray(byte[] data)
        {
            byte[] result = new byte[data.Length / 2];

            for (int i = 0; i < result.Length; i++)
            {
                int x = (data[i * 2] + data[i * 2 + 1]) / 2;
                result[i] = (byte)x;
            }

            return result;
        }
        #endregion

    }
}

 

posted @ 2016-12-16 14:31  可乐•雪碧•咖啡•茶  阅读(228)  评论(0编辑  收藏  举报