소스 검색

修改了三个接口数据,增加了是否需要解密抄key和上报加密的功能

chenjiangqun 2 년 전
부모
커밋
6283350391

+ 32 - 1
FactoryTool_CShare/Business/V2Method.cs

@@ -19,6 +19,8 @@ namespace MOKA_Factory_Tools
 {
     internal class V2Method
     {
+        public static MidList midList { get; set; }
+
         private static string url_getMessage = "https://cn.uc.qhmoka.com/scbc-server/clientType/getMessage.do";
 
         public static void SetMexicanConfig(bool bMexican = false)
@@ -148,6 +150,7 @@ namespace MOKA_Factory_Tools
                 mid.clienttype = jObject["clientType"].Value<string>();
                 mid.host = jObject["host"].Value<string>();
                 mid.whiteType = jObject["whiteType"].Value<string>();
+                mid.aesEncrypt = jObject["aesEncrypt"].Value<string>();
                 object a = jObject["rokuCustomer"];
                 if (jObject["rokuCustomer"].ToString().Length > 0)
                 {
@@ -161,7 +164,11 @@ namespace MOKA_Factory_Tools
                 }
 
                 if (mid.code == "1000")
+                {
+                    midList = mid;
                     return true;
+                }
+                   
             }
             catch (Exception ex)
             {
@@ -193,7 +200,8 @@ namespace MOKA_Factory_Tools
             watch.Start();
 
             item.URL = url + "/bind/order";
-            item.Postdata = string.Format("{{\"orderNum\":\"{0}\",\"sn\":\"{1}\",\"psn\":\"{2}\",\"orderCode\":\"{3}\",\"skipKey\":{4}}}", order, sn, psn, firetv_device_code, key2Write == null ? "[]" : JsonConvert.SerializeObject(key2Write).ToString());
+            
+            item.Postdata = string.Format("{{\"orderNum\":\"{0}\",\"sn\":\"{1}\",\"psn\":\"{2}\",\"orderCode\":\"{3}\",\"client_type\":\"{4}\",\"skipKey\":{5}}}", order, sn, psn, firetv_device_code, midList.clienttype, key2Write == null ? "[]" : JsonConvert.SerializeObject(key2Write).ToString());
             HttpResult httpResult = http.GetHtml(item);
             if (httpResult.StatusCode == System.Net.HttpStatusCode.OK)
             {
@@ -204,6 +212,17 @@ namespace MOKA_Factory_Tools
                     code = CommonMethod.JSON_SeleteNode(jObject, "code");
                     if (code == "0")
                     {
+                        //需要解密
+                        if (midList != null && midList.aesEncrypt.Equals("yes", StringComparison.InvariantCultureIgnoreCase))
+                        {
+                            AES_DES Aes = new AES_DES();
+                            //data解密
+                            string plainText = CommonMethod.JSON_SeleteNode(jObject, "data");
+                            string key = AES_DES.AESKey;
+                            byte[] IV = AES_DES.AESIV;
+                            string str = AES_DES.DecryptStringFromBytes_Aes(plainText, key, IV);
+                            jObject["data"] = str;
+                        }
                         Dictionary<string, string> data = JsonConvert.DeserializeObject<Dictionary<string, string>>(jObject["data"].ToString());
                         if (data != null)
                         {
@@ -614,6 +633,18 @@ namespace MOKA_Factory_Tools
             if (timeout < 5000)
                 timeout = 5000;
             HttpHelper http = new HttpHelper();
+            //需要加密上报的话 
+            if (midList != null && midList.aesEncrypt.Equals("yes", StringComparison.InvariantCultureIgnoreCase))
+            {
+              AES_DES AES_DES = new AES_DES();
+              string key = AES_DES.AESKey;
+              byte[] IV = AES_DES.AESIV;
+              postData = AES_DES.EncryptStringToBytes_Aes(postData, key, IV);
+              //json
+              string Data = string.Format("{{\"reportData\":\"{0}\"}}", postData);
+              JObject jo = JObject.Parse(Data);
+              postData = jo.ToString();
+             }
             HttpItem item = new HttpItem()
             {
                 Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)可选项 默认类会自动识别//Encoding = Encoding.Default,

+ 123 - 0
FactoryTool_CShare/Http/AES_DES.cs

@@ -0,0 +1,123 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Security.Cryptography;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MOKA_Factory_Tools
+{
+    //处理加密解密
+    public class AES_DES
+    {
+        //变量            
+        public static byte[] AESIV { get; set; } = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+        public static string AESKey { get; set; } ="32Dip3h9VIR4IYac";
+        
+        /// <summary>
+        /// 加密
+        /// </summary>
+        /// <param name="plainText"></param> 需要加密的字符串
+        /// <param name="key"></param>  密钥
+        /// <param name="IV"></param>   偏移向量
+        /// <returns> 
+        /// 加密结果字符串       
+        /// </returns>
+        public static string EncryptStringToBytes_Aes(string plainText, string key, byte[] IV)
+        {
+            try
+            {
+                byte[] Key = Encoding.UTF8.GetBytes(key);
+                // Check arguments.
+                if (plainText == null || plainText.Length <= 0)
+                    throw new ArgumentNullException("plainText");
+                if (Key == null || Key.Length <= 0)
+                    throw new ArgumentNullException("Key");
+                if (IV == null || IV.Length <= 0)
+                    throw new ArgumentNullException("IV");
+                byte[] encrypted;
+
+                using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
+                {
+                    aesAlg.Key = Key;
+                    aesAlg.IV = IV;
+                    aesAlg.Mode = CipherMode.ECB;
+                    aesAlg.Padding = PaddingMode.PKCS7;
+
+                    ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
+                    using (MemoryStream msEncrypt = new MemoryStream())
+                    {
+                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
+                        {
+                            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
+                            {
+                                swEncrypt.Write(plainText);
+                            }
+                            encrypted = msEncrypt.ToArray();
+                        }
+                    }
+                }
+                return Convert.ToBase64String(encrypted);
+            }
+            catch (Exception ex)
+            {
+                return ex.Message;
+            }
+
+        }
+
+        /// <summary>
+        /// 解密
+        /// </summary>
+        /// <param name="CipherText"></param> 需解密的字符串
+        /// <param name="Key"></param>  密钥
+        /// <param name="IV"></param>   偏移向量
+        /// <returns> 
+        /// 解密结果字符串       
+        /// </returns>
+        public static string DecryptStringFromBytes_Aes(string CipherText, string Key, byte[] IV)
+        {
+            try
+            {
+                byte[] cipherText = Convert.FromBase64String(CipherText);
+                byte[] key = Encoding.UTF8.GetBytes(Key);
+
+                if (cipherText == null || cipherText.Length <= 0)
+                    throw new ArgumentNullException("cipherText");
+                if (key == null || key.Length <= 0)
+                    throw new ArgumentNullException("Key");
+                if (IV == null || IV.Length <= 0)
+                    throw new ArgumentNullException("IV");
+
+                string plaintext = null;
+                using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
+                {
+                    aesAlg.Key = key;
+                    aesAlg.IV = IV;
+                    aesAlg.Mode = CipherMode.ECB;
+                    aesAlg.Padding = PaddingMode.PKCS7;
+                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
+
+                    using (MemoryStream msDecrypt = new MemoryStream(cipherText))
+                    {
+                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
+                        {
+                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
+                            {
+                                plaintext = srDecrypt.ReadToEnd();
+                            }
+                        }
+                    }
+                }
+                return plaintext;
+            }
+            catch (Exception ex)
+            {
+                return ex.Message;
+            }
+        }
+    }
+
+}
+

+ 1 - 0
FactoryTool_CShare/MOKA Factory Tools.csproj

@@ -122,6 +122,7 @@
     <Compile Include="Database\CommandInfo.cs" />
     <Compile Include="Database\ConnectionParameters.cs" />
     <Compile Include="Database\DbHelper.cs" />
+    <Compile Include="Http\AES_DES.cs" />
     <Compile Include="LResource.zh-CN.Designer.cs">
       <AutoGen>True</AutoGen>
       <DesignTime>True</DesignTime>

+ 1 - 0
FactoryTool_CShare/Models/StructList.cs

@@ -30,6 +30,7 @@ namespace MOKA_Factory_Tools
         public string whiteType { get; set; }
         public RokuCustomer rokuCustomer { get; set; }
         public Dictionary <string,string> keytype { get; set; }
+        public string aesEncrypt { get; set; }
     }
 
     public class RokuData