using System;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Encoders;

namespace AnHuiMI.Common
{
    class SignUtils
    {
		// Token: 0x0600008F RID: 143 RVA: 0x000088EC File Offset: 0x00006AEC
		public static string signSm3WithSm2(string chnlId, string sm4key, string prvkey, string data)
		{
			ECPrivateKeyParameters privatekeyFromD = GmUtil.GetPrivatekeyFromD(new BigInteger(Convert.FromBase64String(prvkey)));
			return Convert.ToBase64String(GmUtil.SignSm3WithSm2(Encoding.UTF8.GetBytes(data), Encoding.UTF8.GetBytes(sm4key), privatekeyFromD));
		}

		// Token: 0x06000090 RID: 144 RVA: 0x0000892C File Offset: 0x00006B2C
		public static bool verifySm3WithSm2(string msg, string sm4key, string signData, string pubKey)
		{
			byte[] array = Convert.FromBase64String(pubKey);
			BitConverter.ToString(array);
			AsymmetricKeyParameter publickeyFromXY = GmUtil.GetPublickeyFromXY(array);
			byte[] rs = Convert.FromBase64String(signData);
			return GmUtil.VerifySm3WithSm2(Encoding.UTF8.GetBytes(msg), Encoding.UTF8.GetBytes(sm4key), rs, publickeyFromXY);
		}

		// Token: 0x06000091 RID: 145 RVA: 0x00008970 File Offset: 0x00006B70
		public static string getSignText(string chnlId, string sm4key, string data, long ts)
		{
			string value = StringUtils.SortJson(data);
			JObject jobject = new JObject();
			jobject.Add("appId", chnlId);
			jobject.Add("data", value);
			jobject.Add("encType", "SM4");
			jobject.Add("signType", "SM2");
			if (ts == 0L)
			{
				jobject.Add("timestamp", Convert.ToString(StringUtils.CurrentTimeStamp(true)));
			}
			else
			{
				jobject.Add("timestamp", Convert.ToString(ts));
			}
			jobject.Add("version", "2.0.1");
			return StringUtils.Json2sign(jobject.ToString(Formatting.None, null)) + "&key=" + sm4key;
		}

		// Token: 0x06000092 RID: 146 RVA: 0x00008A3C File Offset: 0x00006C3C
		public static string encryptMsg(string chnlId, string sm4key, string prvkey, string data, ref string rtSignPlain)
		{
			long num = StringUtils.CurrentTimeStamp(true);
			string signText = SignUtils.getSignText(chnlId, sm4key, data, num);
			string value = SignUtils.signSm3WithSm2(chnlId, sm4key, prvkey, signText);
			string value2 = SignUtils.sm4Encrypt(chnlId, sm4key, data);
			JObject jobject = new JObject();
			jobject.Add("appId", chnlId);
			jobject.Add("encData", value2);
			jobject.Add("encType", "SM4");
			jobject.Add("signData", value);
			jobject.Add("signType", "SM2");
			jobject.Add("timestamp", Convert.ToString(num));
			jobject.Add("version", "2.0.1");
			rtSignPlain = signText;
			return jobject.ToString(Formatting.None, null);
		}

		// Token: 0x06000093 RID: 147 RVA: 0x00008B08 File Offset: 0x00006D08
		public static string encryptMsg(string chnlId, string sm4key, string prvkey, string data)
		{
			long num = StringUtils.CurrentTimeStamp(true);
			string signText = SignUtils.getSignText(chnlId, sm4key, data, num);
			string value = SignUtils.signSm3WithSm2(chnlId, sm4key, prvkey, signText);
			string value2 = SignUtils.sm4Encrypt(chnlId, sm4key, data);
			return new JObject
			{
				{
					"appId",
					chnlId
				},
				{
					"encData",
					value2
				},
				{
					"encType",
					"SM4"
				},
				{
					"signData",
					value
				},
				{
					"signType",
					"SM2"
				},
				{
					"timestamp",
					Convert.ToString(num)
				},
				{
					"version",
					"2.0.1"
				}
			}.ToString(Formatting.None, null);
		}

		// Token: 0x06000094 RID: 148 RVA: 0x00008BD0 File Offset: 0x00006DD0
		public static string sm4Encrypt(string chnlId, string sm4key, string message)
		{
			byte[] bytes = Encoding.UTF8.GetBytes(chnlId.Substring(0, 16));
			byte[] bytes2 = Encoding.UTF8.GetBytes(sm4key);
			byte[] bytes3 = Encoding.UTF8.GetBytes(message);
			string text = Hex.ToHexString(GmUtil.Sm4EncryptECB(bytes, bytes2, "SM4/ECB/PKCS7Padding")).ToUpper();
			return Hex.ToHexString(GmUtil.Sm4EncryptECB(Encoding.UTF8.GetBytes(text.Substring(0, 16)), bytes3, "SM4/ECB/PKCS7Padding"));
		}

		// Token: 0x06000095 RID: 149 RVA: 0x00008C44 File Offset: 0x00006E44
		public static string sm4Decrypt(string chnlId, string sm4key, string message)
		{
			byte[] bytes = Encoding.UTF8.GetBytes(chnlId.Substring(0, 16));
			byte[] bytes2 = Encoding.UTF8.GetBytes(sm4key);
			byte[] cipher = Hex.Decode(message);
			string text = BitConverter.ToString(GmUtil.Sm4EncryptECB(bytes, bytes2, "SM4/ECB/PKCS7Padding"), 0).Replace("-", string.Empty).ToUpper();
			byte[] bytes3 = GmUtil.Sm4DecryptECB(Encoding.UTF8.GetBytes(text.Substring(0, 16)), cipher, "SM4/ECB/PKCS7Padding");
			return Encoding.UTF8.GetString(bytes3);
		}
	}
}
