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

namespace CallCenter.Utility
{
    public class EncryptHelper
    {
        private static readonly string AESKEY = "GHYU80DV3465QSFG";//16或者32位   
        private static readonly string DESKEY = "PORG56JK";//8位   

        /// <summary>   
        /// AES加密   
        /// </summary>   
        /// <param name="data">要加密的字符串</param>   
        /// <returns>加密后的字符串</returns>   
        public static string EncryptAES(string data)
        {
            try
            {
                var aes = new RijndaelManaged();
                byte[] bData = Encoding.UTF8.GetBytes(data);
                aes.Key = Encoding.UTF8.GetBytes(AESKEY);
                aes.IV = Encoding.UTF8.GetBytes(AESKEY);
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;

                ICryptoTransform iCryptoTransform = aes.CreateEncryptor();
                byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);

                return Convert.ToBase64String(bResult); //返回base64加密;   
                //return ByteToHex(bResult);                //返回十六进制数据;   
            }
            catch
            {
                return "Encrypt Faild";
            }
        }

        /// <summary>   
        /// AES加密   
        /// </summary>   
        /// <param name="data">要加密的字符串</param>   
        /// <param name="sKey">密钥串（16位或32位）</param>   
        /// <returns>加密后的字符串</returns>   
        public static string EncryptAES(string data, string sKey)
        {
            try
            {
                var aes = new RijndaelManaged();
                byte[] bData = Encoding.UTF8.GetBytes(data);
                aes.Key = Encoding.UTF8.GetBytes(sKey);
                aes.IV = Encoding.UTF8.GetBytes(sKey);
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;

                ICryptoTransform iCryptoTransform = aes.CreateEncryptor();
                byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);

                return Convert.ToBase64String(bResult); //返回base64加密;   
                //return ByteToHex(bResult);                //返回十六进制数据;   
            }
            catch
            {
                return "Encrypt Faild";
            }
        }

        /// <summary>   
        /// AES解密   
        /// </summary>   
        /// <param name="data">要解密的字符串</param>   
        /// <returns>解密后的字符串</returns>   
        public static string DecryptAES(string data)
        {
            try
            {
                 data = data.Replace(" ", "+");   //URL传参时应用，加密后字符串不含空格但含有+号，在通过URL传参时，+会被替换成空格
                var aes = new RijndaelManaged();
                byte[] bData = Convert.FromBase64String(data); //解密base64;   
                //byte[] bData = HexToByte(data);                  //16进制to byte[];   
                aes.Key = Encoding.UTF8.GetBytes(AESKEY);
                aes.IV = Encoding.UTF8.GetBytes(AESKEY);
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;

                ICryptoTransform iCryptoTransform = aes.CreateDecryptor();
                byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);
                return Encoding.UTF8.GetString(bResult);
            }
            catch
            {
                return "";
            }
        }

        /// <summary>   
        /// AES解密   
        /// </summary>   
        /// <param name="data">要解密的字符串</param>   
        /// <param name="sKey">密钥串（16位或32位字符串）</param>   
        /// <returns>解密后的字符串</returns>   
        public static string DecryptAES(string data, string sKey)
        {
            try
            {
                data = data.Replace(" ", "+");   //URL传参时应用，加密后字符串不含空格但含有+号，在通过URL传参时，+会被替换成空格
                var aes = new RijndaelManaged();
                byte[] bData = Convert.FromBase64String(data); //解密base64;   
                // byte[] bData = HexToByte(data);                  //16进制to byte[];   
                aes.Key = Encoding.UTF8.GetBytes(sKey);
                aes.IV = Encoding.UTF8.GetBytes(sKey);
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;

                ICryptoTransform iCryptoTransform = aes.CreateDecryptor();
                byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);
                return Encoding.UTF8.GetString(bResult);
            }
            catch
            {
                return "";
            }
        }


        /// <summary>   
        /// DES加密   
        /// </summary>   
        /// <param name="data">要加密的字符串</param>   
        /// <returns>加密后的字符串</returns>   
        public static string EncryptDES(string data)
        {
            try
            {
                DES des = new DESCryptoServiceProvider();
                des.Mode = CipherMode.ECB;
                des.Key = Encoding.UTF8.GetBytes(DESKEY);
                des.IV = Encoding.UTF8.GetBytes(DESKEY);

                byte[] bytes = Encoding.UTF8.GetBytes(data);
                byte[] resultBytes = des.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);

                return Convert.ToBase64String(resultBytes); //密文以base64返回;(可根据实际需要返回16进制数据;)   
                //return ByteToHex(resultBytes);//十六位   
            }
            catch
            {
                return "";
            }
        }

        /// <summary>   
        /// DES加密   
        /// </summary>   
        /// <param name="data">要加密的字符串</param>   
        /// <param name="key">密钥串（8位字符串）</param>   
        /// <returns>加密后的字符串</returns>   
        public static string EncryptDES(string data, string key)
        {
            try
            {
                DES des = new DESCryptoServiceProvider();
                des.Mode = CipherMode.ECB;
                des.Key = Encoding.UTF8.GetBytes(key);
                des.IV = Encoding.UTF8.GetBytes(key);

                byte[] bytes = Encoding.UTF8.GetBytes(data);
                byte[] resultBytes = des.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);

                return Convert.ToBase64String(resultBytes); //密文以base64返回;(可根据实际需要返回16进制数据;)   
                // return ByteToHex(resultBytes);//十六位   
            }
            catch
            {
                return "";
            }
        }

        /// <summary>   
        /// DES解密   
        /// </summary>   
        /// <param name="data">要解密的字符串</param>   
        /// <returns>解密后的字符串</returns>   
        public static string DecryptDES(string data)
        {
            try
            {
                //data = data.Replace(" ", "+");   //URL传参时应用，加密后字符串不含空格但含有+号，在通过URL传参时，+会被替换成空格
                DES des = new DESCryptoServiceProvider();
                des.Mode = CipherMode.ECB;
                des.Key = Encoding.UTF8.GetBytes(DESKEY);
                des.IV = Encoding.UTF8.GetBytes(DESKEY);

                byte[] bytes = Convert.FromBase64String(data);      //解密base64;  
                // byte[] bytes = HexToByte(data);        //16进制to byte[];   
                byte[] resultBytes = des.CreateDecryptor().TransformFinalBlock(bytes, 0, bytes.Length);

                return Encoding.UTF8.GetString(resultBytes);
            }
            catch
            {
                return "";
            }
        }

        /// <summary>   
        /// DES解密   
        /// </summary>   
        /// <param name="data">要解密的字符串</param>   
        /// <param name="key">密钥串（8位字符串）</param>   
        /// <returns>解密后的字符串</returns>   
        public static string DecryptDES(string data, string key)
        {
            try
            {
                //data = data.Replace(" ", "+");   //URL传参时应用，加密后字符串不含空格但含有+号，在通过URL传参时，+会被替换成空格
                DES des = new DESCryptoServiceProvider();
                des.Mode = CipherMode.ECB;
                des.Key = Encoding.UTF8.GetBytes(key);
                des.IV = Encoding.UTF8.GetBytes(key);

                byte[] bytes = Convert.FromBase64String(data);    //解密base64;  
                // byte[] bytes = HexToByte(data);           //16进制to byte[];   
                byte[] resultBytes = des.CreateDecryptor().TransformFinalBlock(bytes, 0, bytes.Length);

                return Encoding.UTF8.GetString(resultBytes);
            }
            catch
            {
                return "";
            }
        }

        // method to convert hex string into a byte array     
        private static byte[] HexToByte(string data)
        {
            data = data.Replace(" ", "");

            var comBuffer = new byte[data.Length / 2];

            for (int i = 0; i < data.Length; i += 2)
                comBuffer[i / 2] = Convert.ToByte(data.Substring(i, 2), 16);

            return comBuffer;
        }

        // method to convert a byte array into a hex string     
        private static string ByteToHex(byte[] comByte)
        {
            var builder = new StringBuilder(comByte.Length * 3);

            foreach (byte data in comByte)
                builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));

            return builder.ToString().ToUpper().Replace(" ", "");
        }
    }
}
