Browse Source

fix: 签名算法修改

zhengjie 1 year ago
parent
commit
fd1612b92a

+ 2 - 1
Common/GmUtil.cs

@@ -32,7 +32,8 @@ namespace AnHuiMI.Common
 			try
 			{
 				ISigner signer = SignerUtilities.GetSigner("SM3withSM2");
-				signer.Init(true, new ParametersWithRandom(privateKey));
+				//signer.Init(true, new ParametersWithRandom(privateKey));
+				signer.Init(true, new ParametersWithID(privateKey, userId));
 				signer.BlockUpdate(msg, 0, msg.Length);
 				result = signer.GenerateSignature();
 			}

+ 465 - 0
Common/SMLib/EasyGmUtils.cs

@@ -0,0 +1,465 @@
+/*
+* @Description: 
+* @Author: ylz-lichong
+* @Date: 2022-06-16 17:46:23
+*/
+using System;
+using Org.BouncyCastle.Utilities.Encoders;
+using Org.BouncyCastle.Asn1;
+using Org.BouncyCastle.Asn1.GM;
+using Org.BouncyCastle.Asn1.X9;
+using Org.BouncyCastle.Crypto;
+using Org.BouncyCastle.Crypto.Digests;
+using Org.BouncyCastle.Crypto.Engines;
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Math;
+using Org.BouncyCastle.Security;
+using Org.BouncyCastle.Utilities;
+using System.IO;
+
+namespace GMCrypto.Lib
+{
+    class EasyGmUtils
+    {
+        private static X9ECParameters x9ECParameters = GMNamedCurves.GetByName("sm2p256v1");
+        private static ECDomainParameters ecDomainParameters = new ECDomainParameters(x9ECParameters.Curve, x9ECParameters.G, x9ECParameters.N);
+
+
+        /**
+          *
+          * @param msg
+          * @param userId
+          * @param privateKey
+          * @return r||s,直接拼接byte数组的rs
+          */
+        public static byte[] signSm3WithSm2(byte[] msg, byte[] userId, byte[] privateKeyBytes)
+        {
+            ECPrivateKeyParameters privateKeyParameters=getPrivatekeyFromD(new BigInteger(1, privateKeyBytes));
+            return rsAsn1ToPlainByteArray(signSm3WithSm2Asn1Rs(msg, userId, privateKeyParameters));
+        }
+
+
+        /**
+          * @param msg
+          * @param userId
+          * @param privateKey
+          * @return rs in <b>asn1 format</b>
+          */
+        public static byte[] signSm3WithSm2Asn1Rs(byte[] msg, byte[] userId, AsymmetricKeyParameter privateKey)
+        {
+            try
+            {
+                ISigner signer = SignerUtilities.InitSigner("SM3withSM2", true, privateKey, new SecureRandom());
+                signer.BlockUpdate(msg, 0, msg.Length);
+                byte[] sig = signer.GenerateSignature();
+                return sig;
+            }
+            catch (Exception e)
+            {
+                //log.Error("SignSm3WithSm2Asn1Rs error: " + e.Message, e);
+                return null;
+            }
+        }
+
+
+        /**
+      *
+      * @param msg
+      * @param userId
+      * @param rs r||s,直接拼接byte数组的rs
+      * @param publicKey
+      * @return
+      */
+        public static bool verifySm3WithSm2(byte[] msg, byte[] userId, byte[] rs, byte[] publicKeyBytes)
+        {
+            if (rs == null || msg == null || userId == null) return false;
+            if (rs.Length != RS_LEN * 2) return false;
+
+            if (publicKeyBytes.Length != 64 && publicKeyBytes.Length != 65) throw new ArgumentException("err key length");
+            BigInteger x, y;
+            if (publicKeyBytes.Length > 64)
+            {
+                x = fromUnsignedByteArray(publicKeyBytes, 1, 32);
+                y = fromUnsignedByteArray(publicKeyBytes, 33, 32);
+            }
+            else
+            {
+                x = fromUnsignedByteArray(publicKeyBytes, 0, 32);
+                y = fromUnsignedByteArray(publicKeyBytes, 32, 32);
+            }
+            ECPublicKeyParameters publicKey = getPublickeyFromXY(x, y);
+            return verifySm3WithSm2Asn1Rs(msg, userId, rsPlainByteArrayToAsn1(rs), publicKey);
+        }
+
+        public static BigInteger fromUnsignedByteArray(byte[] var0, int var1, int var2)
+        {
+            byte[] var3 = var0;
+            if (var1 != 0 || var2 != var0.Length)
+            {
+                var3 = new byte[var2];
+                Array.Copy(var0, var1, var3, 0, var2);
+            }
+
+            return new BigInteger(1, var3);
+        }
+
+        /**
+         *
+         * @param msg
+         * @param userId
+         * @param rs in <b>asn1 format</b>
+         * @param publicKey
+         * @return
+         */
+
+        public static bool verifySm3WithSm2Asn1Rs(byte[] msg, byte[] userId, byte[] sign, AsymmetricKeyParameter publicKey)
+        {
+            try
+            {
+                ISigner signer = SignerUtilities.GetSigner("SM3withSM2");
+                signer.Init(false, publicKey);
+                signer.BlockUpdate(msg, 0, msg.Length);
+                return signer.VerifySignature(sign);
+            }
+            catch (Exception e)
+            {
+                //log.Error("VerifySm3WithSm2Asn1Rs error: " + e.Message, e);
+                return false;
+            }
+        }
+
+
+        /**
+        * bc加解密使用旧标c1||c2||c3,此方法在加密后调用,将结果转化为c1||c3||c2
+        * @param c1c2c3
+        * @return
+        */
+        private static byte[] changeC1C2C3ToC1C3C2(byte[] c1c2c3)
+        {
+            int c1Len = (x9ECParameters.Curve.FieldSize + 7) / 8 * 2 + 1; //sm2p256v1的这个固定65。可看GMNamedCurves、ECCurve代码。
+            const int c3Len = 32; //new SM3Digest().getDigestSize();
+            byte[] result = new byte[c1c2c3.Length];
+            Buffer.BlockCopy(c1c2c3, 0, result, 0, c1Len); //c1
+            Buffer.BlockCopy(c1c2c3, c1c2c3.Length - c3Len, result, c1Len, c3Len); //c3
+            Buffer.BlockCopy(c1c2c3, c1Len, result, c1Len + c3Len, c1c2c3.Length - c1Len - c3Len); //c2
+            return result;
+        }
+
+
+        /**
+         * bc加解密使用旧标c1||c3||c2,此方法在解密前调用,将密文转化为c1||c2||c3再去解密
+         * @param c1c3c2
+         * @return
+         */
+        private static byte[] changeC1C3C2ToC1C2C3(byte[] c1c3c2)
+        {
+            int c1Len = (x9ECParameters.Curve.FieldSize + 7) / 8 * 2 + 1; //sm2p256v1的这个固定65。可看GMNamedCurves、ECCurve代码。
+            const int c3Len = 32; //new SM3Digest().GetDigestSize();
+            byte[] result = new byte[c1c3c2.Length];
+            Buffer.BlockCopy(c1c3c2, 0, result, 0, c1Len); //c1: 0->65
+            Buffer.BlockCopy(c1c3c2, c1Len + c3Len, result, c1Len, c1c3c2.Length - c1Len - c3Len); //c2
+            Buffer.BlockCopy(c1c3c2, c1Len, result, c1c3c2.Length - c3Len, c3Len); //c3
+            return result;
+        }
+
+        /**
+         * c1||c3||c2
+         * @param data
+         * @param key
+         * @return
+         */
+        public static byte[] sm2Decrypt(byte[] data, AsymmetricKeyParameter key)
+        {
+            return sm2DecryptOld(changeC1C3C2ToC1C2C3(data), key);
+        }
+
+        /**
+         * c1||c3||c2
+         * @param data
+         * @param key
+         * @return
+         */
+
+        public static byte[] sm2Encrypt(byte[] data, AsymmetricKeyParameter key)
+        {
+            return changeC1C2C3ToC1C3C2(sm2EncryptOld(data, key));
+        }
+
+        /**
+         * c1||c2||c3
+         * @param data
+         * @param key
+         * @return
+         */
+        public static byte[] sm2EncryptOld(byte[] data, AsymmetricKeyParameter pubkey)
+        {
+            try
+            {
+                SM2Engine sm2Engine = new SM2Engine();
+                sm2Engine.Init(true, new ParametersWithRandom(pubkey, new SecureRandom()));
+                return sm2Engine.ProcessBlock(data, 0, data.Length);
+            }
+            catch (Exception e)
+            {
+                //log.Error("Sm2EncryptOld error: " + e.Message, e);
+                return null;
+            }
+        }
+
+        /**
+         * c1||c2||c3
+         * @param data
+         * @param key
+         * @return
+         */
+        public static byte[] sm2DecryptOld(byte[] data, AsymmetricKeyParameter key)
+        {
+            try
+            {
+                SM2Engine sm2Engine = new SM2Engine();
+                sm2Engine.Init(false, key);
+                return sm2Engine.ProcessBlock(data, 0, data.Length);
+            }
+            catch (Exception e)
+            {
+                //log.Error("Sm2DecryptOld error: " + e.Message, e);
+                return null;
+            }
+        }
+
+        /**
+        * @param bytes
+        * @return
+        */
+        public static byte[] sm3(byte[] bytes)
+        {
+            try
+            {
+                SM3Digest digest = new SM3Digest();
+                digest.BlockUpdate(bytes, 0, bytes.Length);
+                byte[] result = DigestUtilities.DoFinal(digest);
+                return result;
+            }
+            catch (Exception e)
+            {
+                //log.Error("Sm3 error: " + e.Message, e);
+                return null;
+            }
+        }
+
+        private const int RS_LEN = 32;
+
+        private static byte[] bigIntToFixexLengthBytes(BigInteger rOrS)
+        {
+            // for sm2p256v1, n is 00fffffffeffffffffffffffffffffffff7203df6b21c6052b53bbf40939d54123,
+            // r and s are the result of mod n, so they should be less than n and have length<=32
+            byte[] rs = rOrS.ToByteArray();
+            if (rs.Length == RS_LEN) return rs;
+            else if (rs.Length == RS_LEN + 1 && rs[0] == 0) return Arrays.CopyOfRange(rs, 1, RS_LEN + 1);
+            else if (rs.Length < RS_LEN)
+            {
+                byte[] result = new byte[RS_LEN];
+                Arrays.Fill(result, (byte)0);
+                Buffer.BlockCopy(rs, 0, result, RS_LEN - rs.Length, rs.Length);
+                return result;
+            }
+            else
+            {
+                throw new ArgumentException("err rs: " + Hex.ToHexString(rs));
+            }
+        }
+
+        /**
+         * BC的SM3withSM2签名得到的结果的rs是asn1格式的,这个方法转化成直接拼接r||s
+         * @param rsDer rs in asn1 format
+         * @return sign result in plain byte array
+         */
+        private static byte[] rsAsn1ToPlainByteArray(byte[] rsDer)
+        {
+            Asn1Sequence seq = Asn1Sequence.GetInstance(rsDer);
+            byte[] r = bigIntToFixexLengthBytes(DerInteger.GetInstance(seq[0]).Value);
+            byte[] s = bigIntToFixexLengthBytes(DerInteger.GetInstance(seq[1]).Value);
+            byte[] result = new byte[RS_LEN * 2];
+            Buffer.BlockCopy(r, 0, result, 0, r.Length);
+            Buffer.BlockCopy(s, 0, result, RS_LEN, s.Length);
+            return result;
+        }
+
+        /**
+         * BC的SM3withSM2验签需要的rs是asn1格式的,这个方法将直接拼接r||s的字节数组转化成asn1格式
+         * @param sign in plain byte array
+         * @return rs result in asn1 format
+         */
+        private static byte[] rsPlainByteArrayToAsn1(byte[] sign)
+        {
+            if (sign.Length != RS_LEN * 2) throw new ArgumentException("err rs. ");
+            BigInteger r = new BigInteger(1, Arrays.CopyOfRange(sign, 0, RS_LEN));
+            BigInteger s = new BigInteger(1, Arrays.CopyOfRange(sign, RS_LEN, RS_LEN * 2));
+            Asn1EncodableVector v = new Asn1EncodableVector();
+            v.Add(new DerInteger(r));
+            v.Add(new DerInteger(s));
+            try
+            {
+                return new DerSequence(v).GetEncoded("DER");
+            }
+            catch (IOException e)
+            {
+                //log.Error("RsPlainByteArrayToAsn1 error: " + e.Message, e);
+                return null;
+            }
+        }
+
+        public static byte[] sm4DecryptCBC(byte[] keyBytes, byte[] cipher, byte[] iv, String algo)
+        {
+            if (keyBytes.Length != 16) throw new ArgumentException("err key length");
+            if (cipher.Length % 16 != 0) throw new ArgumentException("err data length");
+
+            try
+            {
+                KeyParameter key = ParameterUtilities.CreateKeyParameter("SM4", keyBytes);
+                IBufferedCipher c = CipherUtilities.GetCipher(algo);
+                if (iv == null) iv = zeroIv(algo);
+                c.Init(false, new ParametersWithIV(key, iv));
+                return c.DoFinal(cipher);
+            }
+            catch (Exception e)
+            {
+                //log.Error("Sm4DecryptCBC error: " + e.Message, e);
+                return null;
+            }
+        }
+
+
+        public static byte[] sm4EncryptCBC(byte[] keyBytes, byte[] plain, byte[] iv, String algo)
+        {
+            if (keyBytes.Length != 16) throw new ArgumentException("err key length");
+            if (plain.Length % 16 != 0) throw new ArgumentException("err data length");
+
+            try
+            {
+                KeyParameter key = ParameterUtilities.CreateKeyParameter("SM4", keyBytes);
+                IBufferedCipher c = CipherUtilities.GetCipher(algo);
+                if (iv == null) iv = zeroIv(algo);
+                c.Init(true, new ParametersWithIV(key, iv));
+                return c.DoFinal(plain);
+            }
+            catch (Exception e)
+            {
+                //log.Error("Sm4EncryptCBC error: " + e.Message, e);
+                return null;
+            }
+        }
+
+
+        public static byte[] sm4EncryptECB(byte[] keyBytes, byte[] plain, string algo)
+        {
+            if (keyBytes.Length != 16) throw new ArgumentException("err key length");
+            if (plain.Length % 16 != 0) throw new ArgumentException("err data length");
+
+            try
+            {
+                KeyParameter key = ParameterUtilities.CreateKeyParameter("SM4", keyBytes);
+                IBufferedCipher c = CipherUtilities.GetCipher(algo);
+                c.Init(true, key);
+                return c.DoFinal(plain);
+            }
+            catch (Exception e)
+            {
+                //log.Error("Sm4EncryptECB error: " + e.Message, e);
+                return null;
+            }
+        }
+
+        public static byte[] sm4DecryptECB(byte[] keyBytes, byte[] cipher, string algo)
+        {
+            if (keyBytes.Length != 16) throw new ArgumentException("err key length");
+            if (cipher.Length % 16 != 0) throw new ArgumentException("err data length");
+
+            try
+            {
+                KeyParameter key = ParameterUtilities.CreateKeyParameter("SM4", keyBytes);
+                IBufferedCipher c = CipherUtilities.GetCipher(algo);
+                c.Init(false, key);
+                return c.DoFinal(cipher);
+            }
+            catch (Exception e)
+            {
+                //log.Error("Sm4DecryptECB error: " + e.Message, e);
+                return null;
+            }
+        }
+
+
+        public static ECPrivateKeyParameters getPrivatekeyFromD(BigInteger d)
+        {
+            return new ECPrivateKeyParameters(d, ecDomainParameters);
+        }
+
+        public static ECPublicKeyParameters getPublickeyFromXY(BigInteger x, BigInteger y)
+        {
+            return new ECPublicKeyParameters(x9ECParameters.Curve.CreatePoint(x, y), ecDomainParameters);
+        }
+
+        public static byte[] sm4Encrypt(byte[] keyBytes, byte[] plain)
+        {
+            if (keyBytes.Length != 16) throw new ArgumentException("err key length");
+            //        if (plain.length % 16 != 0) throw new RuntimeException("err data length");
+
+            try
+            {
+                KeyParameter key = ParameterUtilities.CreateKeyParameter("SM4", keyBytes);
+                IBufferedCipher c = CipherUtilities.GetCipher("SM4/ECB/PKCS7Padding");
+                c.Init(true, key);
+
+                return c.DoFinal(plain);
+            }
+            catch (Exception e)
+            {
+                return null;
+            }
+        }
+
+        public static byte[] sm4Decrypt(byte[] keyBytes, byte[] cipher)
+        {
+            //        if (keyBytes.length != 16) throw new RuntimeException("err key length");
+            if (cipher.Length % 16 != 0) throw new ArgumentException("err data length");
+
+            try
+            {
+                KeyParameter key = ParameterUtilities.CreateKeyParameter("SM4", keyBytes);
+                IBufferedCipher c = CipherUtilities.GetCipher("SM4/ECB/PKCS7Padding");
+                c.Init(false, key);
+                return c.DoFinal(cipher);
+
+            }
+            catch (Exception e)
+            {
+                return null;
+            }
+        }
+
+        public const String SM4_ECB_NOPADDING = "SM4/ECB/NoPadding";
+        public const String SM4_CBC_NOPADDING = "SM4/CBC/NoPadding";
+        public const String SM4_CBC_PKCS7PADDING = "SM4/CBC/PKCS7Padding";
+
+        public static byte[] zeroIv(String algo)
+        {
+
+            try
+            {
+                IBufferedCipher cipher = CipherUtilities.GetCipher(algo);
+                int blockSize = cipher.GetBlockSize();
+                byte[] iv = new byte[blockSize];
+                Arrays.Fill(iv, (byte)0);
+                return iv;
+            }
+            catch (Exception e)
+            {
+                //log.Error("ZeroIv error: " + e.Message, e);
+                return null;
+            }
+        }
+
+
+    }
+}

+ 132 - 0
Common/SMLib/SMUtil.cs

@@ -0,0 +1,132 @@
+/*
+* @Description: 
+* @Author: ylz-lichong
+* @Date: 2022-06-16 17:46:23
+*/
+using Org.BouncyCastle.Utilities.Encoders;
+using System;
+using System.Text;
+using Newtonsoft.Json.Linq;
+using AnHuiMI.Common;
+using Newtonsoft.Json;
+
+namespace GMCrypto.Lib
+{
+    class SMUtil
+    {
+        /**
+        * 加密
+        *
+        * @param data
+        * @param appId
+        * @param appSecret
+        * @return
+        */
+        public static String encrypt(String data, String appId, String appSecret)
+        {
+            //加密流程
+            //用appId加密appSecret获取新秘钥
+            byte[] appSecretEncData = EasyGmUtils.sm4Encrypt(Encoding.UTF8.GetBytes(appId.Substring(0, 16)), Encoding.UTF8.GetBytes(appSecret));
+            //新秘钥串
+            byte[] secKey = Encoding.UTF8.GetBytes(Hex.ToHexString(appSecretEncData).ToUpper().Substring(0, 16));
+            //加密0数据
+            String encryptDataStr = Hex.ToHexString(EasyGmUtils.sm4Encrypt(secKey, Encoding.UTF8.GetBytes(data))).ToUpper();
+            return encryptDataStr;
+        }
+
+        /**
+         * 解密
+         *
+         * @param data
+         * @param appId
+         * @param appSecret
+         * @return
+        */
+        public static String decrypt(String data, String appId, String appSecret)
+        {
+            byte[] appSecretEncDataDecode = EasyGmUtils.sm4Encrypt(Encoding.UTF8.GetBytes(appId.Substring(0, 16)), Encoding.UTF8.GetBytes(appSecret));
+            byte[] secKeyDecode = Encoding.UTF8.GetBytes(Hex.ToHexString(appSecretEncDataDecode).ToUpper().Substring(0, 16));
+            String decryptDataStr = Encoding.UTF8.GetString(EasyGmUtils.sm4Decrypt(secKeyDecode, Hex.Decode(data)));
+            return decryptDataStr;
+        }
+
+        /**
+        * 签名
+        *
+        * @param jsonObject
+        * @param appSecret
+        * @param privateKey
+        * @return
+        */
+        public static String sign(JObject jsonObject, String appSecret, String privateKey)
+        {
+            // 获取签名串
+            byte[] signText = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
+            byte[] userId = Encoding.UTF8.GetBytes(appSecret);
+            byte[] prvkey = Base64.Decode(privateKey);
+            String responseSign = Base64.ToBase64String(EasyGmUtils.signSm3WithSm2(signText, userId, prvkey));
+            return responseSign;
+        }
+
+
+        /**
+         * 验签
+         *
+         * @param jsonObject
+         * @param appSecret
+         * @param publicKey
+         * @param responseSign
+         * @return
+         */
+        public static Boolean verify(JObject jsonObject, String appSecret, String publicKey, String responseSign)
+        {
+            //验签
+            byte[] msg = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
+            byte[] userIdDecode = Encoding.UTF8.GetBytes(appSecret);
+            byte[] pubkey = Base64.Decode(publicKey);
+            byte[] signData = Base64.Decode(responseSign);
+            return EasyGmUtils.verifySm3WithSm2(msg, userIdDecode, signData, pubkey);
+        }
+
+
+
+        /**
+        * 签名
+        *
+        * @param jsonObject
+        * @param appSecret
+        * @param privateKey
+        * @return
+        */
+        public static String sign(String jsonString, String appSecret, String privateKey)
+        {
+            JObject jsonObject = (JObject)JObject.Parse(jsonString);
+            // 获取签名串
+            byte[] signText = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
+            byte[] userId = Encoding.UTF8.GetBytes(appSecret);
+            byte[] prvkey = Base64.Decode(privateKey);
+            String responseSign = Base64.ToBase64String(EasyGmUtils.signSm3WithSm2(signText, userId, prvkey));
+            return responseSign;
+        }
+
+        /**
+         * 验签
+         *
+         * @param jsonObject
+         * @param appSecret
+         * @param publicKey
+         * @param responseSign
+         * @return
+         */
+        public static Boolean verify(String jsonString, String appSecret, String publicKey, String responseSign)
+        {
+            JObject jsonObject = (JObject)JObject.Parse(jsonString);
+            //验签
+            byte[] msg = Encoding.UTF8.GetBytes(SignUtil.getSignText(jsonObject, appSecret));
+            byte[] userIdDecode = Encoding.UTF8.GetBytes(appSecret);
+            byte[] pubkey = Base64.Decode(publicKey);
+            byte[] signData = Base64.Decode(responseSign);
+            return EasyGmUtils.verifySm3WithSm2(msg, userIdDecode, signData, pubkey);
+        }
+    }
+}

+ 294 - 0
Common/SMLib/SignUtil.cs

@@ -0,0 +1,294 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using System.Collections;
+
+namespace GMCrypto.Lib
+{
+    class SignUtil
+    {
+        private static List<String> ignoreSign = new List<String>() { "signData", "encData", "extra" };
+
+        public static String getSignText(JObject jsonObject, String appSecret)
+        {
+            SortedDictionary<String, String> signMap = new SortedDictionary<String, String>(StringComparer.Ordinal);
+
+            foreach (var entry in jsonObject)
+            {
+                if (!String.IsNullOrEmpty(entry.Value.ToString()) && !ignoreSign.Contains(entry.Key))
+                {
+                    signMap.Add(entry.Key, getValue(entry.Value));
+                }
+            }
+
+
+            List<String> list = new List<String>();
+
+            foreach (var entry in signMap)
+            {
+                if (!String.IsNullOrEmpty(getObjString(entry.Value)))
+                {
+                    list.Add((String)entry.Key + "=" + (String)entry.Value + "&");
+                }
+            }
+
+            int size = list.Count();
+            String[] arrayToSort = (String[])list.ToArray();
+            Array.Sort(arrayToSort, new CaseInsensitiveComparer());
+            StringBuilder sb = new StringBuilder();
+
+            for (int i = 0; i < size; ++i)
+            {
+                sb.Append(arrayToSort[i]);
+            }
+
+            String signText = sb.Append("key=").Append(appSecret).ToString();
+            return signText;
+        }
+
+        public static String getObjString(Object obj)
+        {
+            return obj == null ? "" : (String)obj;
+        }
+
+        private static String getValue(Object value)
+        {
+            return value is String ? getObjString(value) : treeJsonParam(value);
+        }
+
+        private static String treeJsonParam(Object value)
+        {
+            String jsonParam = null;
+            if (value is Dictionary<Object, Object>)
+            {
+                SortedDictionary<String, Object> treeNestedMap = new SortedDictionary<String, Object>(StringComparer.Ordinal);
+                Dictionary<Object, Object> nestedMap = (Dictionary<Object, Object>)value;
+
+                foreach (var entry in nestedMap)
+                {
+                    treeNestedMap.Add(entry.Key.ToString(), entry.Value);
+                }
+                jsonParam = JsonConvert.SerializeObject(treeParams(treeNestedMap), Formatting.None);
+            }
+            else if (value is List<Object>)
+            {
+                List<Object> ar = (List<Object>)value;
+                if (ar != null && ar.Count() != 0)
+                    jsonParam = JsonConvert.SerializeObject(treeList(ar), Formatting.None);
+            }
+            else if (value is JObject)
+            {
+                SortedDictionary<String, Object> treeNestedMap = new SortedDictionary<String, Object>(StringComparer.Ordinal);
+                JObject nestedMap = (JObject)value;
+                foreach (var entry in nestedMap)
+                {
+                    treeNestedMap.Add(entry.Key.ToString(), entry.Value);
+                }
+                jsonParam = JsonConvert.SerializeObject(treeParams(treeNestedMap), Formatting.None);
+            }
+            else if (value is JArray)
+            {
+                JArray ar = (JArray)value;
+                if (ar != null && ar.Count() != 0)
+                    jsonParam = JsonConvert.SerializeObject(treeJsonArray(ar), Formatting.None);
+            }
+            else if (value is JValue)
+            {
+                JValue jval = (JValue)value;
+                if (jval != null && !String.IsNullOrEmpty(jval.ToString()))
+                {
+                    if (jval.ToString().ToLower().Trim().Equals("true") || jval.ToString().ToLower().Trim().Equals("false"))
+                        jsonParam = jval.ToString().ToLower().Trim();
+                    else
+                        jsonParam = jval.Value.ToString();
+                }
+
+
+            }
+            else if (value is JProperty)
+            {
+                SortedDictionary<String, Object> treeNestedMap = new SortedDictionary<String, Object>(StringComparer.Ordinal);
+                JProperty nestedMap = (JProperty)value;
+                treeNestedMap.Add(nestedMap.Name, nestedMap.Value);
+                jsonParam = JsonConvert.SerializeObject(treeParams(treeNestedMap), Formatting.None);
+            }
+            else
+            {
+                jsonParam = value.ToString();
+            }
+
+            return jsonParam;
+        }
+
+        private static SortedDictionary<String, Object> treeParams(SortedDictionary<String, Object> param)
+        {
+            if (param == null)
+            {
+                return new SortedDictionary<String, Object>(StringComparer.Ordinal);
+            }
+            else
+            {
+                SortedDictionary<String, Object> treeParam = new SortedDictionary<String, Object>(StringComparer.Ordinal);
+
+                while (true)
+                {
+                    foreach (var entry in param)
+                    {
+
+                        String key = (String)entry.Key;
+                        Object value = entry.Value;
+                        if (value is Dictionary<Object, Object>)
+                        {
+                            SortedDictionary<String, Object> treeNestedMap = new SortedDictionary<String, Object>(StringComparer.Ordinal);
+                            Dictionary<Object, Object> nestedMap = (Dictionary<Object, Object>)value;
+
+                            foreach (var nestedEntry in nestedMap)
+                            {
+                                treeNestedMap.Add(nestedEntry.Key.ToString(), nestedEntry.Value);
+                            }
+
+                            treeParam.Add(key, treeParams(treeNestedMap));
+                        }
+                        else if (value is List<Object>)
+                        {
+                            List<Object> ar = (List<Object>)value;
+                            if (ar != null && ar.Count() != 0)
+                                treeParam.Add(key, treeList(ar));
+                        }
+                        else if (value is JArray)
+                        {
+                            JArray ar = (JArray)value;
+                            if (ar != null && ar.Count() != 0)
+                                treeParam.Add(key, treeJsonArray(ar));
+                        }
+                        else if (value is JObject)
+                        {
+                            SortedDictionary<String, Object> treeNestedMap = new SortedDictionary<String, Object>(StringComparer.Ordinal);
+                            JObject nestedMap = (JObject)value;
+                            foreach (var nestedEntry in nestedMap)
+                            {
+                                treeNestedMap.Add(nestedEntry.Key.ToString(), nestedEntry.Value);
+                            }
+                            treeParam.Add(key, treeParams(treeNestedMap));
+                        }
+                        else if (value is JValue)
+                        {
+                            JValue jval = (JValue)value;
+                            if (jval != null && !String.IsNullOrEmpty(jval.ToString()))
+                            {
+                                if (jval.ToString().ToLower().Trim().Equals("true") || jval.ToString().ToLower().Trim().Equals("false"))
+                                    treeParam.Add(key, jval.ToString().ToLower().Trim());
+                                else
+                                    treeParam.Add(key, jval.ToString());
+                            }
+                        }
+                        else if (value is JProperty)
+                        {
+                            SortedDictionary<String, Object> treeNestedMap = new SortedDictionary<String, Object>(StringComparer.Ordinal);
+                            JProperty nestedMap = (JProperty)value;
+                            treeNestedMap.Add(nestedMap.Name, nestedMap.Value);
+                            treeParam.Add(key, treeParams(treeNestedMap));
+                        }
+                        else if (!"".Equals(value) && value != null)
+                        {
+                            treeParam.Add(key, value.ToString());
+                        }
+                    }
+                    return treeParam;
+                }
+            }
+        }
+
+        private static List<Object> treeList(List<Object> list)
+        {
+            if (list != null && list.Count() != 0)
+            {
+                JArray jsonArray = new JArray();
+                int size = list.Count();
+
+                for (int i = 0; i < size; ++i)
+                {
+                    jsonArray.Add(list[i]);
+                }
+
+                return treeJsonArray(jsonArray);
+            }
+            else
+            {
+                return null;
+            }
+        }
+
+        private static List<Object> treeJsonArray(JArray jarr)
+        {
+            if (jarr != null && jarr.Count() != 0)
+            {
+                List<Object> jsonArray = new List<Object>();
+                int size = jarr.Count();
+
+                for (int i = 0; i < size; ++i)
+                {
+                    Object value = jarr[i];
+                    if (value is List<Object>)
+                    {
+                        List<Object> ar = (List<Object>)value;
+                        if (ar != null && ar.Count() != 0)
+                            jsonArray.Add(treeList(ar));
+                    }
+                    else if (value is JArray)
+                    {
+                        JArray ar = (JArray)value;
+                        if (ar != null && ar.Count() != 0)
+                            jsonArray.Add(treeJsonArray(ar));
+                    }
+                    else if (value is JObject)
+                    {
+                        SortedDictionary<String, Object> treeNestedMap = new SortedDictionary<String, Object>(StringComparer.Ordinal);
+                        JObject nestedMap = (JObject)value;
+                        foreach (var nestedEntry in nestedMap)
+                        {
+                            treeNestedMap.Add(nestedEntry.Key.ToString(), nestedEntry.Value);
+                        }
+                        jsonArray.Add(treeParams(treeNestedMap));
+
+                    }
+                    else if (value is JValue)
+                    {
+                        JValue jval = (JValue)value;
+                        if (jval != null && !String.IsNullOrEmpty(jval.ToString()))
+                        {
+                            if (jval.ToString().ToLower().Trim().Equals("true") || jval.ToString().ToLower().Trim().Equals("false"))
+                                jsonArray.Add(jval.ToString().ToLower().Trim());
+                            else
+                                jsonArray.Add(jval.ToString());
+                        }
+                    }
+                    else if (value is JProperty)
+                    {
+                        SortedDictionary<String, Object> treeNestedMap = new SortedDictionary<String, Object>(StringComparer.Ordinal);
+                        JProperty nestedMap = (JProperty)value;
+                        treeNestedMap.Add(nestedMap.Name, nestedMap.Value);
+                        jsonArray.Add(treeParams(treeNestedMap));
+                    }
+                    else if (!"".Equals(value))
+                    {
+                        jsonArray.Add(value.ToString());
+                    }
+
+                }
+
+                return jsonArray;
+            }
+            else
+            {
+                return null;
+            }
+        }
+
+
+
+    }
+}

+ 19 - 58
Common/SignUtils.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Text;
+using GMCrypto.Lib;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Linq;
 using Org.BouncyCastle.Crypto;
@@ -11,7 +12,7 @@ namespace AnHuiMI.Common
 {
     class SignUtils
     {
-		public static string signSm3WithSm2(string chnlId, string sm4key, string prvkey, string data)
+		public static string signSm3WithSm2(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));
@@ -26,78 +27,38 @@ namespace AnHuiMI.Common
 			return GmUtil.VerifySm3WithSm2(Encoding.UTF8.GetBytes(msg), Encoding.UTF8.GetBytes(sm4key), rs, publickeyFromXY);
 		}
 
-		public static string getSignText(string chnlId, string sm4key, string data,long ts)
+
+		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);
+		}
+
+		public static string encryptMsg(string appId, string appSecret, string prvkey, string data, ref string rtSignPlain)
 		{
+			string ts = Convert.ToString(CurrentTimeStamp());
+
 			JObject jobject = new JObject();
-			jobject.Add("appId", chnlId);
-			jobject.Add("data", data);
+			jobject.Add("appId", appId);
+			jobject.Add("data", JObject.Parse(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());
-
-			return StringUtils.Json2sign(value) + "&key=" + sm4key;
-		}
+			string signData = SMUtil.sign(jobject, appSecret, prvkey);
+			string encData = SMUtil.encrypt(data, appId, appSecret);
 
-		public static string encryptMsg(string chnlId, string sm4key, string prvkey, string data, ref string rtSignPlain)
-		{
-			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);
-			JObject jobject = new JObject();
-			jobject.Add("appId", chnlId);
 			jobject.Add("encData", encData);
-			jobject.Add("encType", "SM4");
 			jobject.Add("signData", signData);
-			jobject.Add("signType", "SM2");
-			jobject.Add("timestamp", ts);
-			jobject.Add("version", "2.0.1");
+			// 删除明文
+			jobject.Remove("data");
+
 			rtSignPlain = signData;
 
 			return jobject.ToString(Formatting.None, null);
 		}
 
-		public static string encryptMsg(string chnlId, string sm4key, string prvkey, string data)
-		{
-			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);
-			return new JObject
-			{
-				{
-					"appId",
-					chnlId
-				},
-				{
-					"encData",
-					value2
-				},
-				{
-					"encType",
-					"SM4"
-				},
-				{
-					"signData",
-					value
-				},
-				{
-					"signType",
-					"SM2"
-				},
-				{
-					"timestamp",
-					ts
-				},
-				{
-					"version",
-					"2.0.1"
-				}
-			}.ToString(Formatting.None, null);
-		}
 
 		public static string sm4Encrypt(string chnlId, string sm4key, string message)
 		{

+ 0 - 101
Common/StringUtils.cs

@@ -1,101 +0,0 @@
-using Newtonsoft.Json;
-using Newtonsoft.Json.Linq;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace AnHuiMI.Common
-{
-	class StringUtils
-	{
-		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);
-		}
-
-		public static SortedDictionary<string, object> KeySort(JObject obj)
-		{
-			var dic = new SortedDictionary<string, object>();
-			foreach (var x in obj)
-			{
-				if (x.Value is JValue) dic.Add(x.Key, x.Value);
-				else if (x.Value is JObject) dic.Add(x.Key, KeySort((JObject)x.Value));
-				else if (x.Value is JArray)
-				{
-					var tmp = new SortedDictionary<string, object>[x.Value.Count()];
-					for (var i = 0; i < x.Value.Count(); i++)
-					{
-						tmp[i] = KeySort((JObject)x.Value[i]);
-					}
-					dic.Add(x.Key, tmp);
-				}
-			}
-			return dic;
-
-		}
-
-		/// <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));
-			keyValues.OrderBy(m => m.Key);
-
-			return JsonConvert.SerializeObject(keyValues);
-		}
-
-		public static string Json2sign(string json)
-		{
-			Dictionary<string, object> dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
-			string text = "";
-			foreach (KeyValuePair<string, object> keyValuePair in dictionary)
-			{
-                // 值为空不参与签名
-                //if (!string.IsNullOrEmpty(keyValuePair.Value + ""))
-                //{
-                    text = string.Concat(new object[]
-					{
-					text,
-					keyValuePair.Key,
-					"=",
-					keyValuePair.Value,
-					"&"
-					});
-                //}
-            }
-			return text.Substring(0, text.Length - 1);
-		}
-	}
-}

+ 3 - 1
HeFeiMI.csproj

@@ -102,7 +102,9 @@
     <Compile Include="Common\ExPortToExcel.cs" />
     <Compile Include="Common\GmUtil.cs" />
     <Compile Include="Common\SignUtils.cs" />
-    <Compile Include="Common\StringUtils.cs" />
+    <Compile Include="Common\SMLib\EasyGmUtils.cs" />
+    <Compile Include="Common\SMLib\SignUtil.cs" />
+    <Compile Include="Common\SMLib\SMUtil.cs" />
     <Compile Include="Common\Utils.cs" />
     <Compile Include="FormSetter\ComboxSetter.cs" />
     <Compile Include="FormSetter\DataTableSetter.cs" />

+ 6 - 0
HeFeiMI.csproj.user

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <ProjectView>ProjectFiles</ProjectView>
+  </PropertyGroup>
+</Project>

+ 23 - 24
Helper/EncryptHelper.cs

@@ -4,44 +4,44 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using AnHuiMI.Common;
+using Newtonsoft.Json;
 using Newtonsoft.Json.Linq;
 using PTMedicalInsurance.Variables;
+using GMCrypto.Lib;
 
 namespace PTMedicalInsurance.Helper
 {
     class EncryptHelper
     {
-        //private string ak = "1G94963I20403F60C80A00005FF7A699";
-        //private string sk = "APyE9G1D+C8g3qV3Z6VshXztOa55YJBgwN+P4WqU5n0x";
-        //private string appid = "1G94963HS03V3F60C80A00000DB330D8";
+        //private string appSecret = "1G566663V0033F60C80A0000B26913B4";
+        //private string privateKey = "DsexH970Mz9RAuu9SZtqdz/JwwZtppfS1lzOZvtO/Q==";
+        //private static string appId = "1G566663I0023F60C80A00000A8F1C36";
 
         //测试
-        private string ak = "1H1INA1L90OH3F60C80A00008119D616";   //appSecret 数字密钥sm4
-        private string sk = "APCIAgJqh3+AcK/IXL1WJD130i2q+6UblRxQzus3+sVw";     //渠道私密
-        public static string appid = "1H1INA1L30OG3F60C80A0000DEE43558";    //渠道ID
-        public string pubKey = "BPwaiORlFqBIiMMTyeATozdSsLCxlGa/ N8ouTosiHKKmVeSnSWRgdIHOEXzyCVQlRzPCsKB24ZA4E3G8t9biN1E=";
+        private string appSecret = "1H1INA1L90OH3F60C80A00008119D616";   //appSecret 数字密钥sm4
+        private string privateKey = "APCIAgJqh3+AcK/IXL1WJD130i2q+6UblRxQzus3+sVw";     //渠道私密
+        public static string appId = "1H1INA1L30OG3F60C80A0000DEE43558";    //渠道ID
+        public string publicKey = "BDMsMM2HPRkaKSl2ynBbCRtJodP8Nh4G5IkEnV+7YHaCplkAZbPMsUlvJpWqQ+Q4sT7611xGSZ1/mPsqgqJ49zs=";            //平台公钥
 
         //正式
         //private string ak = "1H62Q1KH205K76430B0A0000BF149773";
         //private string sk = "YbNObZNMdUgwgLUEyK4ixNSkaCF9OPtCdDth9APWYKU=";
         //private string appid = "1H62Q1KGP05J76430B0A00007144E257";
 
-        public string getNotEmptyJson(JObject joInput)
-        {
-            return StringUtils.removeEmptyProperty(joInput).ToString();
-        }
 
         public string getSignText(string data)
         {
+            return "";
             //string ts = DateTime.Now.ToString("yyyyMMddHHmmss");
-            return SignUtils.getSignText(appid, ak, data,StringUtils.CurrentTimeStamp());
+            //return SignUtils.getSignText(appid, ak, data,StringUtils.CurrentTimeStamp());
         }
         public string encrypt(string data,ref string signText)
         {
             string encryptData = "";
             try
-            { 
-                encryptData = SignUtils.encryptMsg(appid, ak, sk, data,ref signText);
+            {
+                string strData = JsonConvert.SerializeObject(JObject.Parse(data), Newtonsoft.Json.Formatting.None);
+                encryptData = SignUtils.encryptMsg(appId, appSecret, privateKey, strData, ref signText);
                 return encryptData;
             }
             catch (Exception ex)
@@ -51,7 +51,7 @@ namespace PTMedicalInsurance.Helper
             }
             finally
             {
-                Global.writeLog("ak:" + ak + ";sk:" + sk + ";appid:" + appid, data, encryptData);
+                Global.writeLog("ak:" + appSecret + ";sk:" + privateKey + ";appid:" + appId, data, encryptData);
             }
         }
 
@@ -60,7 +60,7 @@ namespace PTMedicalInsurance.Helper
             string encryptData = "";
             try
             {
-                encryptData = SignUtils.signSm3WithSm2(appid, ak, sk, data);
+                encryptData = SignUtils.signSm3WithSm2(appSecret, privateKey, data);
                 return encryptData;
             }
             catch (Exception ex)
@@ -70,26 +70,25 @@ namespace PTMedicalInsurance.Helper
             }
             finally
             {
-                Global.writeLog("ak:" + ak + ";sk:" + sk + ";appid:" + appid, data, encryptData);
+                Global.writeLog("ak:" + appSecret + ";sk:" + privateKey + ";appid:" + appId, data, encryptData);
             }
         }
 
-        public int verify(string data, string encryptData)
+        public bool verify(JObject signDto, string signData)
         {
             string error = "";
             try
             {
-                string signText = getSignText(data);
-                if (SignUtils.verifySm3WithSm2(signText, ak, encryptData, pubKey)) return 0; else return -1;
+                return SMUtil.verify(signDto, appSecret, publicKey, signData);
             }
             catch (Exception ex)
             {
                 error = ex.Message;
-                return -1;
+                return false;
             }
             finally
             {
-                Global.writeLog("ak:" + ak + ";sk:" + sk + ";appid:" + appid, data + ";" + encryptData, error);
+                Global.writeLog("ak:" + appSecret + ";sk:" + privateKey + ";appid:" + appId, signDto.ToString() + ";" + signData, error);
             }
         }
 
@@ -98,7 +97,7 @@ namespace PTMedicalInsurance.Helper
             string data = "";
             try
             {
-                data = SignUtils.sm4Decrypt(ak, sk, encryptData);
+                data = SMUtil.decrypt(encryptData, appId, appSecret);
                 return data;
             }
             catch (Exception ex)
@@ -108,7 +107,7 @@ namespace PTMedicalInsurance.Helper
             }
             finally
             {
-                Global.writeLog("ak:" + ak + ";sk:" + sk + ";appid:" + appid, encryptData, data);
+                Global.writeLog("ak:" + appSecret + ";sk:" + privateKey + ";appid:" + appId, encryptData, data);
             }
         }
     }

+ 17 - 3
Helper/InvokeHelper.cs

@@ -535,11 +535,25 @@ namespace PTMedicalInsurance.Helper
                 Global.curEvt.URL = "http://10.66.159.55:7080" + url;
                 joRtn = invokeCenterService(data);
 
-                string encryptData = JsonHelper.getDestValue(joRtn, "encData");
-                if (!string.IsNullOrEmpty(encryptData))
+                string encData = JsonHelper.getDestValue(joRtn, "encData");
+                string signData = JsonHelper.getDestValue(joRtn, "signData");
+                if (!string.IsNullOrEmpty(encData) && !string.IsNullOrEmpty(signData))
                 {
+                    joRtn.Remove("encData");
+                    joRtn.Remove("signData");
+                    joRtn.Remove("data");
                     //解密
-                    joRtn.Add("output", encrypt.decrypt(encryptData));
+                    string decData = encrypt.decrypt(encData);
+                    // 验签
+                    JsonConvert.DefaultSettings = () => new JsonSerializerSettings
+                    {
+                        FloatParseHandling = FloatParseHandling.Decimal
+                    };
+                    joRtn.Add("data", JToken.FromObject(JsonConvert.DeserializeObject(decData)));
+                    bool rtn = encrypt.verify(joRtn, signData);
+                    Global.writeLog("验签结果:" + rtn);
+
+                    joRtn.Add("output", decData);
                 }
 
                 return joRtn;

+ 7 - 3
Helper/JsonHelper.cs

@@ -239,14 +239,18 @@ namespace PTMedicalInsurance.Helper
         /// <param name="input"></param>
         /// <returns></returns>
         public static string setMPCenterInpar(string infno, JObject joInput)
+        {
+            return setMPCenterInpar(infno, joInput.ToString());
+        }
+
+        public static string setMPCenterInpar(string infno, string txtData)
         {
             EncryptHelper encrypt = new EncryptHelper();
 
-            string txtData = encrypt.getNotEmptyJson(joInput);
             string signData = "";
 
-            string output = encrypt.encrypt(txtData,ref signData);
-            
+            string output = encrypt.encrypt(txtData, ref signData);
+
             return output;
         }