using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GMUtilLib;
using PTMedicalInsurance.Variables;

namespace PTMedicalInsurance.Helper
{
    class EncryptHelper
    {
        //private string ak = "1G94963I20403F60C80A00005FF7A699";
        //private string sk = "APyE9G1D+C8g3qV3Z6VshXztOa55YJBgwN+P4WqU5n0x";
        //private string appid = "1G94963HS03V3F60C80A00000DB330D8";

        //测试
        //private string ak = "1H1INA1L90OH3F60C80A00008119D616";
        //private string sk = "APCIAgJqh3+AcK/IXL1WJD130i2q+6UblRxQzus3+sVw";
        //private string appid = "1H1INA1L30OG3F60C80A0000DEE43558";

        //正式
        private string ak = "1H62Q1KH205K76430B0A0000BF149773";
        private string sk = "YbNObZNMdUgwgLUEyK4ixNSkaCF9OPtCdDth9APWYKU=";
        private string appid = "1H62Q1KGP05J76430B0A00007144E257";
        //ak:1H62Q1KH205K76430B0A0000BF149773;sk:YbNObZNMdUgwgLUEyK4ixNSkaCF9OPtCdDth9APWYKU=;appid:1H62Q1KGP05J76430B0A00007144E257
        public string encrypt(string data)
        {
            string encryptData = "";
            try
            {
                encryptData = GMUtilLib.SignUtil.encryptMsg(appid, ak, sk, data);
                return encryptData;
            }
            catch (Exception ex)
            {
                encryptData = ex.Message;
                return encryptData;
            }
            finally
            {
                Global.writeLog("ak:" + ak +";sk:" + sk + ";appid:" + appid,data,encryptData);
            }
        }

        public string sign(string data)
        {
            string encryptData = "";
            try
            {
                encryptData = GMUtilLib.SignUtil.signSm3WithSm2(appid, ak, sk, data);
                return encryptData;
            }
            catch (Exception ex)
            {
                encryptData = ex.Message;
                return encryptData;
            }
            finally
            {
                Global.writeLog("ak:" + ak + ";sk:" + sk + ";appid:" + appid, data, encryptData);
            }
        }

        public int verify(string data,string encryptData)
        {
            string error ="";
            try
            {
                if (GMUtilLib.SignUtil.verifySm3WithSm2(data, ak, encryptData, sk)) return 0;  else return -1;
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return -1;
            }
            finally
            {
                Global.writeLog("ak:" + ak + ";sk:" + sk + ";appid:" + appid, data + ";" + encryptData, error);
            }
        }

        public string decrypt(string encryptData)
        {
            string data = "";
            try
            {
                data = GMUtilLib.SignUtil.decryptMsg(ak, sk, encryptData);
                return data;
            }
            catch (Exception ex)
            {
                data = ex.Message;
                return data;
            }
            finally
            {
                Global.writeLog("ak:" + ak + ";sk:" + sk + ";appid:" + appid, encryptData, data);
            }
        }
    }
}
