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

namespace ChuPiao.Common.Core.Security
{
    /// <summary>
    /// DES加密解密工具类
    /// </summary>
    public class DesUtil
    {
        //DES密钥（必须8位）
        private static string _desKey = "gtd!@#$%";
        //DES初始化向量
        private static string _desIV = "wbw^&*()";

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="nonEncrypt"></param>
        /// <returns></returns>
        public static string Encrypt(string nonEncrypt)
        {
            byte[] nonEncrypts = Encoding.GetEncoding("gb2312").GetBytes(nonEncrypt);
            byte[] arr2 = Encrypt(_desKey, _desIV, nonEncrypts);
            return Convert.ToBase64String(arr2);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="Encrypt"></param>
        /// <returns></returns>
        public static string Decrypt(string Encrypt)
        {
            byte[] Encrypts = Convert.FromBase64String(Encrypt);
            byte[] arr2 = Decrypt(_desKey, _desIV, Encrypts);
            return Encoding.GetEncoding("gb2312").GetString(arr2);
        }

        /// <summary>
        /// 加密byte数组
        /// </summary>
        /// <param name="desKey">密钥</param>
        /// <param name="desIV">向量</param>
        /// <param name="inputByte">待加密byte数组</param>
        /// <returns>加密后的byte数组</returns>
        public static byte[] Encrypt(string desKey, string desIV, byte[] inputByte)
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(desKey);
            byte[] rgbIV = Encoding.UTF8.GetBytes(desIV);
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByte, 0, inputByte.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                        byte[] result = ms.ToArray();
                        ms.Close();
                        return result;
                    }
                }
            }
        }

        /// <summary> 
        /// 解密byte数组
        /// </summary> 
        /// <param name="desKey">密钥</param>
        /// <param name="desIV">向量</param>
        /// <param name="inputByte">加密了的byte数组</param>
        /// <returns>解密后的byte数组</returns>
        public static byte[] Decrypt(string desKey, string desIV, byte[] inputByte)
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(desKey);
            byte[] rgbIV = Encoding.UTF8.GetBytes(desIV);
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByte, 0, inputByte.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                        byte[] result = ms.ToArray();
                        ms.Close();
                        return result;
                    }
                }
            }
        }
    }
}
