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

namespace BootstrapBlazorApp11.Server.Hmc
{
    public class DesEncryptDecryptExtension
    {
        private static string desKey = "H1d3j5e5d6Y0E4C8E8H8a3bM";

        private static string desIv = "H6l8H8c8";

        [ThreadStatic]
        private static SymmetricAlgorithm sa;

        [ThreadStatic]
        private static ICryptoTransform ct;

        [ThreadStatic]
        private static MemoryStream ms;

        [ThreadStatic]
        private static CryptoStream cs;

        private static object lockObj = new object();

        private static SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();

        [STAThread]
        public static string DecryptByHexString(string decryptString, string decryptKey, Encoding encoding, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.Zeros)
        {
            byte[] bytes = encoding.GetBytes(decryptKey.Substring(0, 8));
            byte[] array = new byte[decryptString.Length / 2];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = Convert.ToByte(decryptString.Substring(i * 2, 2), 16);
            }

            byte[] bytes2 = DecryptByDesBytes(array, bytes, cipher, padding);
            return encoding.GetString(bytes2).TrimEnd('\0');
        }

        [STAThread]
        public static string EncryptDecryptByHexString(string str_in_data, string str_DES_KEY)
        {
            try
            {
                byte[] array = new byte[str_in_data.Length / 2];
                byte[] bytes = Encoding.UTF8.GetBytes(str_DES_KEY.Substring(0, 8));
                for (int i = 0; i < array.Length; i++)
                {
                    array[i] = Convert.ToByte(str_in_data.Substring(i * 2, 2), 16);
                }

                DES dES = new DESCryptoServiceProvider();
                dES.Mode = CipherMode.ECB;
                dES.Key = bytes;
                byte[] array2 = array;
                ICryptoTransform cryptoTransform = dES.CreateEncryptor();
                byte[] array3 = cryptoTransform.TransformFinalBlock(array2, 0, array2.Length);
                string text = "";
                byte[] array4 = array3;
                foreach (byte b in array4)
                {
                    text += b.ToString("X2");
                }

                return text.Substring(0, 16);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [STAThread]
        public static byte[] DecryptByDesBytes(byte[] decryptBytes, byte[] keyBytes, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.Zeros)
        {
            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider
            {
                Key = keyBytes,
                IV = keyBytes,
                Mode = cipher,
                Padding = padding
            };
            return dESCryptoServiceProvider.CreateDecryptor().TransformFinalBlock(decryptBytes, 0, decryptBytes.Length);
        }

        [STAThread]
        public static string DESEncrypt(string originalValue, string key = "", string IV = "")
        {
            if (string.IsNullOrEmpty(key))
            {
                key = desKey;
            }
            else if (key.Length < 24)
            {
                key = key.PadLeft(24, '0');
            }

            if (string.IsNullOrEmpty(IV))
            {
                IV = desIv;
            }

            sa = new TripleDESCryptoServiceProvider();
            sa.Key = Encoding.UTF8.GetBytes(key);
            sa.IV = Encoding.UTF8.GetBytes(IV);
            ct = sa.CreateEncryptor();
            byte[] bytes = Encoding.UTF8.GetBytes(originalValue);
            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(bytes, 0, bytes.Length);
            cs.FlushFinalBlock();
            cs.Close();
            return Convert.ToBase64String(ms.ToArray());
        }

        [STAThread]
        public static string DESDecrypst(string data, string key = "", string IV = "")
        {
            try
            {
                lock (lockObj)
                {
                    if (string.IsNullOrEmpty(key))
                    {
                        key = desKey;
                    }
                    else if (key.Length < 24)
                    {
                        key = key.PadLeft(24, '0');
                    }

                    if (string.IsNullOrEmpty(IV))
                    {
                        IV = desIv;
                    }

                    mCSP.Key = Encoding.UTF8.GetBytes(key);
                    mCSP.IV = Encoding.UTF8.GetBytes(IV);
                    ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
                    byte[] array = Convert.FromBase64String(data);
                    ms = new MemoryStream();
                    cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                    cs.Write(array, 0, array.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                    return Encoding.UTF8.GetString(ms.ToArray());
                }
            }
            catch (Exception innerException)
            {
                throw new Exception("DESDecrypst('" + data + "'),解密失败", innerException);
            }
        }

        public static string Encrypt(string Text)
        {
            if (!string.IsNullOrWhiteSpace(Text))
            {
                return Encrypt(Text, "HmCsoft###***");
            }

            return "";
        }

        public static string Encrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            byte[] bytes = Encoding.Default.GetBytes(Text);
            dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(Md5Encryption(sKey).Substring(0, 8));
            dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(Md5Encryption(sKey).Substring(0, 8));
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(bytes, 0, bytes.Length);
            cryptoStream.FlushFinalBlock();
            StringBuilder stringBuilder = new StringBuilder();
            byte[] array = memoryStream.ToArray();
            foreach (byte b in array)
            {
                stringBuilder.AppendFormat("{0:X2}", b);
            }

            return stringBuilder.ToString();
        }

        public static string EncryptUseByWxStore(string encryText, string sKey)
        {
            if (string.IsNullOrEmpty(encryText))
            {
                return "";
            }

            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            byte[] bytes = Encoding.Default.GetBytes(encryText);
            dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(sKey.Substring(0, 8));
            dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(sKey.Substring(0, 8));
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(bytes, 0, bytes.Length);
            cryptoStream.FlushFinalBlock();
            StringBuilder stringBuilder = new StringBuilder();
            byte[] array = memoryStream.ToArray();
            foreach (byte b in array)
            {
                stringBuilder.AppendFormat("{0:X2}", b);
            }

            return stringBuilder.ToString();
        }

        public static string DecryptUseByWxStore(string Text, string sKey)
        {
            if (string.IsNullOrEmpty(Text))
            {
                return "";
            }

            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            int num = Text.Length / 2;
            byte[] array = new byte[num];
            for (int i = 0; i < num; i++)
            {
                int num2 = Convert.ToInt32(Text.Substring(i * 2, 2), 16);
                array[i] = (byte)num2;
            }

            dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(sKey.Substring(0, 8));
            dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(sKey.Substring(0, 8));
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(array, 0, array.Length);
            cryptoStream.FlushFinalBlock();
            return Encoding.GetEncoding("UTF-8").GetString(memoryStream.ToArray());
        }

        public static string DecryptByKey(string Text, string sKey)
        {
            if (string.IsNullOrEmpty(Text))
            {
                return "";
            }

            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            int num = Text.Length / 2;
            byte[] array = new byte[num];
            for (int i = 0; i < num; i++)
            {
                int num2 = Convert.ToInt32(Text.Substring(i * 2, 2), 16);
                array[i] = (byte)num2;
            }

            dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(sKey.Substring(0, 8));
            dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(sKey.Substring(0, 8));
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(array, 0, array.Length);
            cryptoStream.FlushFinalBlock();
            return Encoding.Default.GetString(memoryStream.ToArray());
        }

        public static string Decrypt(string Text)
        {
            if (!string.IsNullOrEmpty(Text))
            {
                return Decrypt(Text, "HmCsoft###***");
            }

            return "";
        }

        public static string Decrypt(string Text, string sKey)
        {
            try
            {
                DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
                int num = Text.Length / 2;
                byte[] array = new byte[num];
                for (int i = 0; i < num; i++)
                {
                    int num2 = Convert.ToInt32(Text.Substring(i * 2, 2), 16);
                    array[i] = (byte)num2;
                }

                dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(Md5Encryption(sKey).Substring(0, 8));
                dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(Md5Encryption(sKey).Substring(0, 8));
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
                cryptoStream.Write(array, 0, array.Length);
                cryptoStream.FlushFinalBlock();
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                Encoding encoding = Encoding.GetEncoding("gb2312");
                return encoding.GetString(memoryStream.ToArray());
            }
            catch (Exception)
            {
                return Text;
            }
        }

        public string ObjDecrypt(string Text)
        {
            if (!string.IsNullOrEmpty(Text))
            {
                return ObjDecrypt(Text, "HmCsoft###***");
            }

            return "";
        }

        public string ObjDecrypt(string Text, string sKey)
        {
            try
            {
                DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
                int num = Text.Length / 2;
                byte[] array = new byte[num];
                for (int i = 0; i < num; i++)
                {
                    int num2 = Convert.ToInt32(Text.Substring(i * 2, 2), 16);
                    array[i] = (byte)num2;
                }

                dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(Md5Encryption(sKey).Substring(0, 8));
                dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(Md5Encryption(sKey).Substring(0, 8));
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
                cryptoStream.Write(array, 0, array.Length);
                cryptoStream.FlushFinalBlock();
                return Encoding.Default.GetString(memoryStream.ToArray());
            }
            catch (Exception)
            {
                return Text;
            }
        }

        [STAThread]
        public static string EncryptInvoice(string encryText, string sKey)
        {
            if (string.IsNullOrEmpty(encryText))
            {
                return "";
            }

            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(encryText);
            dESCryptoServiceProvider.Key = Encoding.GetEncoding("UTF-8").GetBytes(sKey.Substring(0, 8));
            dESCryptoServiceProvider.IV = Encoding.GetEncoding("UTF-8").GetBytes(sKey.Substring(0, 8));
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(bytes, 0, bytes.Length);
            cryptoStream.FlushFinalBlock();
            StringBuilder stringBuilder = new StringBuilder();
            byte[] array = memoryStream.ToArray();
            foreach (byte b in array)
            {
                stringBuilder.AppendFormat("{0:X2}", b);
            }

            return stringBuilder.ToString();
        }

        [STAThread]
        public static string DecryptInvoice(string Text, string sKey)
        {
            if (string.IsNullOrEmpty(Text))
            {
                return "";
            }

            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            int num = Text.Length / 2;
            byte[] array = new byte[num];
            for (int i = 0; i < num; i++)
            {
                int num2 = Convert.ToInt32(Text.Substring(i * 2, 2), 16);
                array[i] = (byte)num2;
            }

            dESCryptoServiceProvider.Key = Encoding.GetEncoding("UTF-8").GetBytes(sKey.Substring(0, 8));
            dESCryptoServiceProvider.IV = Encoding.GetEncoding("UTF-8").GetBytes(sKey.Substring(0, 8));
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(array, 0, array.Length);
            cryptoStream.FlushFinalBlock();
            return Encoding.GetEncoding("UTF-8").GetString(memoryStream.ToArray());
        }

        [STAThread]
        public static string DecryptByInvoice(string Text, string sKey)
        {
            if (string.IsNullOrEmpty(Text))
            {
                return "";
            }

            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            int num = Text.Length / 2;
            byte[] array = new byte[num];
            for (int i = 0; i < num; i++)
            {
                int num2 = Convert.ToInt32(Text.Substring(i * 2, 2), 16);
                array[i] = (byte)num2;
            }

            dESCryptoServiceProvider.Key = Encoding.GetEncoding("UTF-8").GetBytes(sKey.Substring(0, 8));
            dESCryptoServiceProvider.IV = Encoding.GetEncoding("UTF-8").GetBytes(sKey.Substring(0, 8));
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(array, 0, array.Length);
            cryptoStream.FlushFinalBlock();
            return Encoding.GetEncoding("UTF-8").GetString(memoryStream.ToArray());
        }

        [STAThread]
        public static string Md5Encryption(string ConvertString, int Code = 32)
        {
            if (ConvertString == null)
            {
                return null;
            }

            string result = string.Empty;
            switch (Code)
            {
                case 16:
                    {
                        MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
                        string text2 = BitConverter.ToString(mD5CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(ConvertString)), 4, 8);
                        return text2.Replace("-", "");
                    }
                case 32:
                    {
                        string text = "";
                        MD5 mD = MD5.Create();
                        byte[] array = mD.ComputeHash(Encoding.UTF8.GetBytes(ConvertString));
                        for (int i = 0; i < array.Length; i++)
                        {
                            text += array[i].ToString("X2");
                        }

                        result = text;
                        break;
                    }
            }

            return result;
        }

        [STAThread]
        public static string Md5EncryptionQiChaCha(string ConvertString)
        {
            MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
            byte[] array = mD5CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(ConvertString));
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < array.Length; i++)
            {
                stringBuilder.Append(array[i].ToString("x2"));
            }

            return stringBuilder.ToString();
        }

        public static string NewMD5(string strData, string md5str)
        {
            string text = "";
            using (MD5 mD = MD5.Create())
            {
                byte[] array = mD.ComputeHash(Encoding.UTF8.GetBytes(strData));
                text = BitConverter.ToString(array);
                text = text.Replace("-", "").ToUpper();
            }

            return text;
        }

        public static string JCEncrypt(string encryText, string sKey, string encoding = "UTF-8")
        {
            if (string.IsNullOrEmpty(encryText))
            {
                return "";
            }

            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            byte[] bytes = Encoding.GetEncoding(encoding).GetBytes(encryText);
            dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(NewMD5(sKey, "md5").Substring(0, 8));
            dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(NewMD5(sKey, "md5").Substring(0, 8));
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(bytes, 0, bytes.Length);
            cryptoStream.FlushFinalBlock();
            StringBuilder stringBuilder = new StringBuilder();
            byte[] array = memoryStream.ToArray();
            foreach (byte b in array)
            {
                stringBuilder.AppendFormat("{0:X2}", b);
            }

            return stringBuilder.ToString();
        }

        public static MemoryStream JCEncryptToMs(byte[] inputEncByteArray, string sKey, string encoding = "UTF-8")
        {
            if (inputEncByteArray == null || inputEncByteArray.Count() == 0)
            {
                return new MemoryStream();
            }

            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(NewMD5(sKey, "md5").Substring(0, 8));
            dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(NewMD5(sKey, "md5").Substring(0, 8));
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(inputEncByteArray, 0, inputEncByteArray.Length);
            cryptoStream.FlushFinalBlock();
            return memoryStream;
        }

        public static string JCDecrypt(string decryptText, string sKey, string encoding = "UTF-8")
        {
            if (string.IsNullOrEmpty(decryptText))
            {
                return "";
            }

            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            int num = decryptText.Length / 2;
            byte[] array = new byte[num];
            for (int i = 0; i < num; i++)
            {
                int num2 = Convert.ToInt32(decryptText.Substring(i * 2, 2), 16);
                array[i] = (byte)num2;
            }

            dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(NewMD5(sKey, "md5").Substring(0, 8));
            dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(NewMD5(sKey, "md5").Substring(0, 8));
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(array, 0, array.Length);
            cryptoStream.FlushFinalBlock();
            return Encoding.GetEncoding(encoding).GetString(memoryStream.ToArray());
        }
    }
}
