using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace PTMedicalInsurance.Common
{
    class Encrypt
    {
        public static string ToBase64hmac(string strText, string strKey)

        {

            HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.UTF8.GetBytes(strKey));

            byte[] byteText = myHMACSHA1.ComputeHash(Encoding.UTF8.GetBytes(strText));

            return System.Convert.ToBase64String(byteText);

        }

        public static string HMACSHA1Text(string EncryptText, string EncryptKey)
        {
            //HMACSHA1加密
            string message;
            string key;
            message = EncryptText;
            key = EncryptKey;

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(key);
            HMACSHA1 hmacsha1 = new HMACSHA1(keyByte);
            byte[] messageBytes = encoding.GetBytes(message);
            byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);

            //return ByteToString(hashmessage);
            return Convert.ToBase64String(hashmessage);
        }

        public static string HMACSHA1Text2(string EncryptText, string EncryptKey)

        {
            //HMACSHA1加密
            HMACSHA1 hmacsha1 = new HMACSHA1();
            hmacsha1.Key = System.Text.Encoding.UTF8.GetBytes(EncryptKey);

            byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(EncryptText);
            byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
            return Convert.ToBase64String(hashBytes);
        }

        public static string SHA256EncryptStr(string data)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(data);
            byte[] hash = SHA256Managed.Create().ComputeHash(bytes);

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                builder.Append(hash[i].ToString("x2"));
            }
            return builder.ToString();
        }
    }

    class HaErBinEncrypt
    {
        [DllImport("HaErBinEncrypt.dll", CharSet = CharSet.Ansi, EntryPoint = "Signature", CallingConvention = CallingConvention.StdCall)]
        private static extern int Signature(byte[] userid, byte[] sm2key, byte[] sm4key, byte[] plain, byte[] type, ref IntPtr pSignature, ref IntPtr pCipher);


        [DllImport("HaErBinEncrypt.dll", CharSet = CharSet.Ansi, EntryPoint = "EncryptBySM4", CallingConvention = CallingConvention.StdCall)]
        private static extern IntPtr EncryptBySM4(byte[] sm4key, byte[] plain);

        [DllImport("HaErBinEncrypt.dll", CharSet = CharSet.Ansi, EntryPoint = "DecryptBySM4_ECB", CallingConvention = CallingConvention.StdCall)]
        private static extern IntPtr DecryptBySM4_ECB(byte[] sm4key, byte[] pCipher);

        [DllImport("HaErBinEncrypt.dll", CharSet = CharSet.Ansi, EntryPoint = "EncryptBySM3HASH", CallingConvention = CallingConvention.StdCall)]
        private static extern IntPtr EncryptBySM3HASH(byte[] plain);

        const string sm2key = "AYvxpsjPGd06aErh6w8uMFKqXwpV6V0pXJYuP4KfD0s=";
        const string sm4key = "RNSNupzo1RAizTKU";


        public string Signature(string plain, ref string cipher)
        {
            byte[] bUID = Encoding.UTF8.GetBytes("");
            byte[] bSm2Key = Encoding.UTF8.GetBytes(sm2key);
            byte[] bSm4Key = Encoding.UTF8.GetBytes(sm4key);
            byte[] bPlain = Encoding.UTF8.GetBytes(plain);
            byte[] bType = Encoding.UTF8.GetBytes("asn1");
            byte[] btSignature = new byte[10240];

            try
            {
                IntPtr pSignatrue = IntPtr.Zero;
                IntPtr pCiper = IntPtr.Zero;
                int i = Signature(bUID, bSm2Key, bSm4Key, bPlain, bType, ref pSignatrue, ref pCiper);


                cipher = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pCiper);
                return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pSignatrue); ;
            }
            catch (Exception ex)
            {
                return "签名异常:" + ex.Message;
            }
        }

        public string Encrypt(string plain)
        {
            byte[] bSm4Key = Encoding.UTF8.GetBytes(sm4key);
            byte[] bPlain = Encoding.UTF8.GetBytes(plain);
            IntPtr pCipher = IntPtr.Zero;
            try
            {
                pCipher = EncryptBySM4(bSm4Key, bPlain);
                return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pCipher);
            }
            catch (Exception ex)
            {
                return "解密异常:" + ex.Message;
            }
        }

        public string Decrypt(string cipher)
        {
            byte[] bSm4Key = Encoding.UTF8.GetBytes(sm4key);
            byte[] bCipher = Encoding.UTF8.GetBytes(cipher);
            IntPtr pPlain = IntPtr.Zero;
            try
            {
                pPlain = DecryptBySM4_ECB(bSm4Key, bCipher);
                return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pPlain);
            }
            catch (Exception ex)
            {
                return "解密异常:" + ex.Message;
            }
        }

        public static string EncryptBySM3HASH(string plain)
        {
            byte[] bPlain = Encoding.UTF8.GetBytes(plain);
            IntPtr pCipher = IntPtr.Zero;
            try
            {
                pCipher = EncryptBySM3HASH(bPlain);
                return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pCipher);
            }
            catch (Exception ex)
            {
                return  "解密异常:" + ex.Message;
            }
        }
    }
}