using LJLib.Tools.Encry;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace LJLib.Tools.DEncrypt
{
	/// <summary>
	/// DES加密/解密类。
    /// Copyright (C) AIFMB
	/// </summary>
	public class DESEncrypt
	{
		public DESEncrypt()
		{			
		}

		#region ========加密======== 
 
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
		public static string Encrypt(string Text) 
		{
			return Encrypt(Text,"C6F87DF747B948BC90C5864E160743DD");
		}
		/// <summary> 
		/// 加密数据 
		/// </summary> 
		/// <param name="Text"></param> 
		/// <param name="sKey"></param> 
		/// <returns></returns> 
        public static string Encrypt(string pToEncrypt, string sKey) 
		{
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
                des.Key = BitConverter.GetBytes(CRC64.ComputeAsUTF8String(sKey));
                des.IV = BitConverter.GetBytes(CRC64.ComputeAsUTF8String(sKey));
                using (MemoryStream ms = new MemoryStream())
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                    return Convert.ToBase64String(ms.ToArray());
                }
            }
		} 

		#endregion
		
		#region ========解密======== 
   
 
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
		public static string Decrypt(string Text) 
		{
            return Decrypt(Text, "C6F87DF747B948BC90C5864E160743DD");
		}
		/// <summary> 
		/// 解密数据 
		/// </summary> 
		/// <param name="Text"></param> 
		/// <param name="sKey"></param> 
		/// <returns></returns> 
        public static string Decrypt(string pToDecrypt, string sKey) 
		{
            if (string.IsNullOrEmpty(pToDecrypt))
            {
                return string.Empty;
            }
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
                des.Key = BitConverter.GetBytes(CRC64.ComputeAsUTF8String(sKey));
                des.IV = BitConverter.GetBytes(CRC64.ComputeAsUTF8String(sKey));
                using (MemoryStream ms = new MemoryStream())
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                    return Encoding.UTF8.GetString(ms.ToArray());
                }
            }
		} 
 
		#endregion 


	}
}
