Procházet zdrojové kódy

fix: 签名数据优化

zhengjie před 1 rokem
rodič
revize
fd61eafa1e
5 změnil soubory, kde provedl 44 přidání a 66 odebrání
  1. 0 42
      Common/GmUtil.cs
  2. 5 12
      Common/SignUtils.cs
  3. 32 4
      Common/StringUtils.cs
  4. 6 2
      Helper/EncryptHelper.cs
  5. 1 6
      Helper/JsonHelper.cs

+ 0 - 42
Common/GmUtil.cs

@@ -21,13 +21,11 @@ namespace AnHuiMI.Common
 {
 	public class GmUtil
 	{
-		// Token: 0x06000006 RID: 6 RVA: 0x0000217E File Offset: 0x0000037E
 		public static byte[] SignSm3WithSm2(byte[] msg, byte[] userId, AsymmetricKeyParameter privateKey)
 		{
 			return GmUtil.RsAsn1ToPlainByteArray(GmUtil.SignSm3WithSm2Asn1Rs(msg, userId, privateKey));
 		}
 
-		// Token: 0x06000007 RID: 7 RVA: 0x00002190 File Offset: 0x00000390
 		public static byte[] SignSm3WithSm2Asn1Rs(byte[] msg, byte[] userId, AsymmetricKeyParameter privateKey)
 		{
 			byte[] result;
@@ -45,13 +43,11 @@ namespace AnHuiMI.Common
 			return result;
 		}
 
-		// Token: 0x06000008 RID: 8 RVA: 0x000021E0 File Offset: 0x000003E0
 		public static bool VerifySm3WithSm2(byte[] msg, byte[] userId, byte[] rs, AsymmetricKeyParameter publicKey)
 		{
 			return rs != null && msg != null && userId != null && rs.Length == 64 && GmUtil.VerifySm3WithSm2Asn1Rs(msg, userId, GmUtil.RsPlainByteArrayToAsn1(rs), publicKey);
 		}
 
-		// Token: 0x06000009 RID: 9 RVA: 0x00002204 File Offset: 0x00000404
 		public static bool VerifySm3WithSm2Asn1Rs(byte[] msg, byte[] userId, byte[] sign, AsymmetricKeyParameter publicKey)
 		{
 			bool result;
@@ -69,7 +65,6 @@ namespace AnHuiMI.Common
 			return result;
 		}
 
-		// Token: 0x0600000A RID: 10 RVA: 0x00002250 File Offset: 0x00000450
 		private static byte[] ChangeC1C2C3ToC1C3C2(byte[] c1c2c3)
 		{
 			int num = (GmUtil.x9ECParameters.Curve.FieldSize + 7) / 8 * 2 + 1;
@@ -80,7 +75,6 @@ namespace AnHuiMI.Common
 			return array;
 		}
 
-		// Token: 0x0600000B RID: 11 RVA: 0x000022B0 File Offset: 0x000004B0
 		private static byte[] ChangeC1C3C2ToC1C2C3(byte[] c1c3c2)
 		{
 			int num = (GmUtil.x9ECParameters.Curve.FieldSize + 7) / 8 * 2 + 1;
@@ -91,19 +85,16 @@ namespace AnHuiMI.Common
 			return array;
 		}
 
-		// Token: 0x0600000C RID: 12 RVA: 0x0000230D File Offset: 0x0000050D
 		public static byte[] Sm2Decrypt(byte[] data, AsymmetricKeyParameter key)
 		{
 			return GmUtil.Sm2DecryptOld(GmUtil.ChangeC1C3C2ToC1C2C3(data), key);
 		}
 
-		// Token: 0x0600000D RID: 13 RVA: 0x0000231B File Offset: 0x0000051B
 		public static byte[] Sm2Encrypt(byte[] data, AsymmetricKeyParameter key)
 		{
 			return GmUtil.ChangeC1C2C3ToC1C3C2(GmUtil.Sm2EncryptOld(data, key));
 		}
 
-		// Token: 0x0600000E RID: 14 RVA: 0x0000232C File Offset: 0x0000052C
 		public static byte[] Sm2EncryptOld(byte[] data, AsymmetricKeyParameter pubkey)
 		{
 			byte[] result;
@@ -120,7 +111,6 @@ namespace AnHuiMI.Common
 			return result;
 		}
 
-		// Token: 0x0600000F RID: 15 RVA: 0x00002374 File Offset: 0x00000574
 		public static byte[] Sm2DecryptOld(byte[] data, AsymmetricKeyParameter key)
 		{
 			byte[] result;
@@ -137,7 +127,6 @@ namespace AnHuiMI.Common
 			return result;
 		}
 
-		// Token: 0x06000010 RID: 16 RVA: 0x000023B4 File Offset: 0x000005B4
 		public static byte[] Sm3(byte[] bytes)
 		{
 			byte[] result;
@@ -154,7 +143,6 @@ namespace AnHuiMI.Common
 			return result;
 		}
 
-		// Token: 0x06000011 RID: 17 RVA: 0x000023F0 File Offset: 0x000005F0
 		private static byte[] BigIntToFixexLengthBytes(BigInteger rOrS)
 		{
 			byte[] array = rOrS.ToByteArray();
@@ -176,7 +164,6 @@ namespace AnHuiMI.Common
 			throw new ArgumentException("err rs: " + Hex.ToHexString(array));
 		}
 
-		// Token: 0x06000012 RID: 18 RVA: 0x00002464 File Offset: 0x00000664
 		private static byte[] RsAsn1ToPlainByteArray(byte[] rsDer)
 		{
 			Asn1Sequence instance = Asn1Sequence.GetInstance(rsDer);
@@ -188,7 +175,6 @@ namespace AnHuiMI.Common
 			return array3;
 		}
 
-		// Token: 0x06000013 RID: 19 RVA: 0x000024C8 File Offset: 0x000006C8
 		private static byte[] RsPlainByteArrayToAsn1(byte[] sign)
 		{
 			if (sign.Length != 64)
@@ -212,7 +198,6 @@ namespace AnHuiMI.Common
 			return result;
 		}
 
-		// Token: 0x06000014 RID: 20 RVA: 0x00002550 File Offset: 0x00000750
 		public static AsymmetricCipherKeyPair GenerateKeyPair()
 		{
 			AsymmetricCipherKeyPair result;
@@ -229,13 +214,11 @@ namespace AnHuiMI.Common
 			return result;
 		}
 
-		// Token: 0x06000015 RID: 21 RVA: 0x00002598 File Offset: 0x00000798
 		public static ECPrivateKeyParameters GetPrivatekeyFromD(BigInteger d)
 		{
 			return new ECPrivateKeyParameters(d, GmUtil.ecDomainParameters);
 		}
 
-		// Token: 0x06000016 RID: 22 RVA: 0x000025A8 File Offset: 0x000007A8
 		public static ECPublicKeyParameters GetPublickeyFromXY(byte[] pubkey)
 		{
 			ECPoint q = GMNamedCurves.GetByName("SM2P256V1").Curve.DecodePoint(pubkey);
@@ -243,13 +226,11 @@ namespace AnHuiMI.Common
 			return new ECPublicKeyParameters(q, parameters);
 		}
 
-		// Token: 0x06000017 RID: 23 RVA: 0x000025F4 File Offset: 0x000007F4
 		public static ECPublicKeyParameters GetPublickeyFromXY(BigInteger x, BigInteger y)
 		{
 			return new ECPublicKeyParameters(GmUtil.x9ECParameters.Curve.CreatePoint(x, y), GmUtil.ecDomainParameters);
 		}
 
-		// Token: 0x06000018 RID: 24 RVA: 0x00002614 File Offset: 0x00000814
 		public static AsymmetricKeyParameter GetPublickeyFromX509File(FileInfo file)
 		{
 			FileStream fileStream = null;
@@ -271,7 +252,6 @@ namespace AnHuiMI.Common
 			return null;
 		}
 
-		// Token: 0x06000019 RID: 25 RVA: 0x00002670 File Offset: 0x00000870
 		private static byte[] ToByteArray(int i)
 		{
 			return new byte[]
@@ -283,7 +263,6 @@ namespace AnHuiMI.Common
 			};
 		}
 
-		// Token: 0x0600001A RID: 26 RVA: 0x000026A8 File Offset: 0x000008A8
 		private static byte[] Join(params byte[][] byteArrays)
 		{
 			List<byte> list = new List<byte>();
@@ -294,7 +273,6 @@ namespace AnHuiMI.Common
 			return list.ToArray();
 		}
 
-		// Token: 0x0600001B RID: 27 RVA: 0x000026D8 File Offset: 0x000008D8
 		private static byte[] KDF(byte[] Z, int klen)
 		{
 			int num = 1;
@@ -332,7 +310,6 @@ namespace AnHuiMI.Common
 			return null;
 		}
 
-		// Token: 0x0600001C RID: 28 RVA: 0x000027A0 File Offset: 0x000009A0
 		public static byte[] Sm4DecryptCBC(byte[] keyBytes, byte[] cipher, byte[] iv, string algo)
 		{
 			if (keyBytes.Length != 16)
@@ -362,7 +339,6 @@ namespace AnHuiMI.Common
 			return result;
 		}
 
-		// Token: 0x0600001D RID: 29 RVA: 0x00002820 File Offset: 0x00000A20
 		public static byte[] Sm4EncryptCBC(byte[] keyBytes, byte[] plain, byte[] iv, string algo)
 		{
 			if (keyBytes.Length != 16)
@@ -392,7 +368,6 @@ namespace AnHuiMI.Common
 			return result;
 		}
 
-		// Token: 0x0600001E RID: 30 RVA: 0x000028A0 File Offset: 0x00000AA0
 		public static byte[] Sm4EncryptECB(byte[] keyBytes, byte[] plain, string algo)
 		{
 			if (keyBytes.Length != 16)
@@ -414,7 +389,6 @@ namespace AnHuiMI.Common
 			return result;
 		}
 
-		// Token: 0x0600001F RID: 31 RVA: 0x000028FC File Offset: 0x00000AFC
 		public static byte[] Sm4DecryptECB(byte[] keyBytes, byte[] cipher, string algo)
 		{
 			if (cipher.Length % 16 != 0)
@@ -436,7 +410,6 @@ namespace AnHuiMI.Common
 			return result;
 		}
 
-		// Token: 0x06000020 RID: 32 RVA: 0x00002958 File Offset: 0x00000B58
 		public static GmUtil.Sm2Cert readSm2File(byte[] pem, string pwd)
 		{
 			GmUtil.Sm2Cert sm2Cert = new GmUtil.Sm2Cert();
@@ -463,7 +436,6 @@ namespace AnHuiMI.Common
 			return result;
 		}
 
-		// Token: 0x06000021 RID: 33 RVA: 0x00002A4C File Offset: 0x00000C4C
 		public static GmUtil.Sm2Cert ReadSm2X509Cert(byte[] cert)
 		{
 			GmUtil.Sm2Cert sm2Cert = new GmUtil.Sm2Cert();
@@ -482,7 +454,6 @@ namespace AnHuiMI.Common
 			return result;
 		}
 
-		// Token: 0x06000022 RID: 34 RVA: 0x00002AA4 File Offset: 0x00000CA4
 		public static byte[] ZeroIv(string algo)
 		{
 			byte[] result;
@@ -499,7 +470,6 @@ namespace AnHuiMI.Common
 			return result;
 		}
 
-		// Token: 0x06000023 RID: 35 RVA: 0x00002AE4 File Offset: 0x00000CE4
 		public static void Main2(string[] s)
 		{
 			foreach (object obj in GMNamedCurves.Names)
@@ -533,37 +503,25 @@ namespace AnHuiMI.Common
 			GmUtil.SignSm3WithSm2(bytes3, bytes2, (ECPrivateKeyParameters)sm2Cert.privateKey);
 		}
 
-		// Token: 0x04000003 RID: 3
 		private static X9ECParameters x9ECParameters = GMNamedCurves.GetByName("sm2p256v1");
 
-		// Token: 0x04000004 RID: 4
 		private static ECDomainParameters ecDomainParameters = new ECDomainParameters(GmUtil.x9ECParameters.Curve, GmUtil.x9ECParameters.G, GmUtil.x9ECParameters.N);
 
-		// Token: 0x04000005 RID: 5
-		private const int RS_LEN = 32;
 
-		// Token: 0x04000006 RID: 6
 		public const string SM4_ECB_NOPADDING = "SM4/ECB/NoPadding";
 
-		// Token: 0x04000007 RID: 7
 		public const string SM4_ECB_PKCS7PADDING = "SM4/ECB/PKCS7Padding";
 
-		// Token: 0x04000008 RID: 8
 		public const string SM4_CBC_NOPADDING = "SM4/CBC/NoPadding";
 
-		// Token: 0x04000009 RID: 9
 		public const string SM4_CBC_PKCS7PADDING = "SM4/CBC/PKCS7Padding";
 
-		// Token: 0x02000011 RID: 17
 		public class Sm2Cert
 		{
-			// Token: 0x04000073 RID: 115
 			public AsymmetricKeyParameter privateKey;
 
-			// Token: 0x04000074 RID: 116
 			public AsymmetricKeyParameter publicKey;
 
-			// Token: 0x04000075 RID: 117
 			public string certId;
 		}
 	}

+ 5 - 12
Common/SignUtils.cs

@@ -11,14 +11,12 @@ 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);
@@ -28,26 +26,24 @@ namespace AnHuiMI.Common
 			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,string ts)
+		public static string getSignText(string chnlId, string sm4key, string data,long ts)
 		{
 			JObject jobject = new JObject();
 			jobject.Add("appId", chnlId);
-			jobject.Add("data", JObject.Parse(data));
+			jobject.Add("data", data);
 			jobject.Add("encType", "SM4");
 			jobject.Add("signType", "SM2");
 			jobject.Add("timestamp", ts); ;
 			jobject.Add("version", "2.0.1");
 
-			string value = StringUtils.SortJson(jobject.ToString(Formatting.None, null));
+			string value = StringUtils.SortJson(jobject.ToString());
 
 			return StringUtils.Json2sign(value) + "&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)
 		{
-			string ts = DateTime.Now.ToString("yyyyMMddHHmmss");
+			long ts = StringUtils.CurrentTimeStamp();
 			string signText = SignUtils.getSignText(chnlId, sm4key, data,ts);
 			string signData = SignUtils.signSm3WithSm2(chnlId, sm4key, prvkey, signText);
 			string encData = SignUtils.sm4Encrypt(chnlId, sm4key, data);
@@ -64,10 +60,9 @@ namespace AnHuiMI.Common
 			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)
 		{
-			string ts = DateTime.Now.ToString("yyyyMMddHHmmss");
+			long ts = StringUtils.CurrentTimeStamp();
 			string signText = SignUtils.getSignText(chnlId, sm4key, data, ts);
 			string value = SignUtils.signSm3WithSm2(chnlId, sm4key, prvkey, signText);
 			string value2 = SignUtils.sm4Encrypt(chnlId, sm4key, data);
@@ -104,7 +99,6 @@ namespace AnHuiMI.Common
 			}.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));
@@ -114,7 +108,6 @@ namespace AnHuiMI.Common
 			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));

+ 32 - 4
Common/StringUtils.cs

@@ -10,14 +10,12 @@ namespace AnHuiMI.Common
 {
 	class StringUtils
 	{
-		// Token: 0x06000098 RID: 152 RVA: 0x00008CE8 File Offset: 0x00006EE8
 		public static long CurrentTimeStamp(bool isMinseconds = false)
 		{
 			TimeSpan timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
 			return Convert.ToInt64(isMinseconds ? timeSpan.TotalMilliseconds : timeSpan.TotalSeconds);
 		}
 
-		// Token: 0x06000099 RID: 153 RVA: 0x00008D28 File Offset: 0x00006F28
 		public static SortedDictionary<string, object> KeySort(JObject obj)
 		{
 			var dic = new SortedDictionary<string, object>();
@@ -39,6 +37,37 @@ namespace AnHuiMI.Common
 
 		}
 
+		/// <summary>
+		/// 删除为空值的属性
+		/// </summary>
+		/// <param name="joInput"></param>
+		/// <returns></returns>
+		public static JObject removeEmptyProperty(JObject joInput)
+		{
+			JObject joOutput = new JObject();
+			foreach (var x in joInput)
+			{
+				if (x.Value is JValue)
+				{
+					if (!string.IsNullOrEmpty(x.Value.ToString()))
+					{
+						joOutput.Add(x.Key, x.Value);
+					}
+				}
+				else if (x.Value is JObject) joOutput.Add(x.Key, removeEmptyProperty((JObject)x.Value));
+				else if (x.Value is JArray)
+				{
+					JArray tmp = new JArray();
+					for (var i = 0; i < x.Value.Count(); i++)
+					{
+						tmp.Add(removeEmptyProperty((JObject)x.Value[i]));
+					}
+					joOutput.Add(x.Key, tmp);
+				}
+			}
+			return joOutput;
+		}
+
 		public static string SortJson(string json)
 		{
 			SortedDictionary<string, object> keyValues = KeySort(JObject.Parse(json));
@@ -47,8 +76,7 @@ namespace AnHuiMI.Common
 			return JsonConvert.SerializeObject(keyValues);
 		}
 
-			// Token: 0x0600009B RID: 155 RVA: 0x00009080 File Offset: 0x00007280
-			public static string Json2sign(string json)
+		public static string Json2sign(string json)
 		{
 			Dictionary<string, object> dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
 			string text = "";

+ 6 - 2
Helper/EncryptHelper.cs

@@ -26,11 +26,15 @@ namespace PTMedicalInsurance.Helper
         //private string sk = "YbNObZNMdUgwgLUEyK4ixNSkaCF9OPtCdDth9APWYKU=";
         //private string appid = "1H62Q1KGP05J76430B0A00007144E257";
 
+        public string getNotEmptyJson(JObject joInput)
+        {
+            return StringUtils.removeEmptyProperty(joInput).ToString();
+        }
 
         public string getSignText(string data)
         {
-            string ts = DateTime.Now.ToString("yyyyMMddHHmmss");
-            return SignUtils.getSignText(appid, ak, data,ts);
+            //string ts = DateTime.Now.ToString("yyyyMMddHHmmss");
+            return SignUtils.getSignText(appid, ak, data,StringUtils.CurrentTimeStamp());
         }
         public string encrypt(string data,ref string signText)
         {

+ 1 - 6
Helper/JsonHelper.cs

@@ -242,16 +242,11 @@ namespace PTMedicalInsurance.Helper
         {
             EncryptHelper encrypt = new EncryptHelper();
 
-            string txtData = joInput.ToString();
+            string txtData = encrypt.getNotEmptyJson(joInput);
             string signData = "";
 
             string output = encrypt.encrypt(txtData,ref signData);
-            Global.writeLog("签名:"+signData);
-            Global.writeLog("加密后参数:"+output);
             
-            int rtn  = encrypt.verify(txtData, signData);
-
-            Global.writeLog("验证结果:"+rtn);
             return output;
         }