Преглед изворни кода

增加离线使用授权以及有效期判断。

dd пре 5 месеци
родитељ
комит
e7ec84223d

BIN
.vs/Welling_Motor_Debug_Tool/v17/.suo


+ 2 - 0
Welling_Motor_Debug_Tool/LocalInfo.cs

@@ -27,5 +27,7 @@ namespace Welling_Motor_Debug_Tool
         public string ServerCfgFileName = "server1.ttcfg"; //服务器配置文件
         public string ExcelTempleFileName = "FOQC成机参数检验标准_Temple1.xls"; //配置文件模板
         public string HelpFilename = "Welling_Motor_Debug_Tool使用说明书_V1.0.html"; //帮助文件名
+        public string OfflineUseLogFileName = "offlg";
+        public string OffLineUseKeyFileName = "offkey";
     }
 }

+ 2 - 1
Welling_Motor_Debug_Tool/Version.cs

@@ -18,7 +18,8 @@ namespace Welling_Motor_Debug_Tool
         //修改记录
         string ChangeLog = "修改记录:\r\n" +
             "V" + mainForm.Version + "\r\n" +
-            "1,解决参数检验时力矩传感器参数判断错误的问题。\r\n" +
+            "1,解决参数检验时力矩传感器参数判断错误的问题;\r\n" +
+            "2,增加离线使用授权以及有效期判断。\r\n" +
             "\r\n" +
             "V2.5.0\r\n" +
             "1,增加力矩传感器手动标定实时检验,超范围时弹出提示;\r\n" +

+ 9 - 0
Welling_Motor_Debug_Tool/Welling_Motor_Debug_Tool.csproj

@@ -116,6 +116,12 @@
       <DependentUpon>GenerateParams.cs</DependentUpon>
     </Compile>
     <Compile Include="Info.cs" />
+    <Compile Include="Licences.cs">
+      <SubType>Form</SubType>
+    </Compile>
+    <Compile Include="Licences.Designer.cs">
+      <DependentUpon>Licences.cs</DependentUpon>
+    </Compile>
     <Compile Include="MessageBoxTimeOut.cs" />
     <Compile Include="PC_Information.cs" />
     <Compile Include="ServerSelect.cs">
@@ -166,6 +172,9 @@
     <EmbeddedResource Include="GenerateParams.resx">
       <DependentUpon>GenerateParams.cs</DependentUpon>
     </EmbeddedResource>
+    <EmbeddedResource Include="Licences.resx">
+      <DependentUpon>Licences.cs</DependentUpon>
+    </EmbeddedResource>
     <EmbeddedResource Include="Login.resx">
       <DependentUpon>Login.cs</DependentUpon>
     </EmbeddedResource>

+ 55 - 2
Welling_Motor_Debug_Tool/aes.cs

@@ -10,7 +10,7 @@ namespace Welling_Motor_Debug_Tool
 {
     internal class aes
     {
-        // 加密函数
+        // 加密字符串并保存到文件
         public static void EncryptToFile(string plainText, string filePath, string key, string iv)
         {
             using (Aes aesAlg = Aes.Create())
@@ -27,7 +27,7 @@ namespace Welling_Motor_Debug_Tool
             }
         }
 
-        // 解密函数
+        // 从加密文件解密刺符传
         public static string DecryptFromFile(string filePath, string key, string iv)
         {
             using (Aes aesAlg = Aes.Create())
@@ -43,5 +43,58 @@ namespace Welling_Motor_Debug_Tool
                 }
             }
         }
+
+        // 加密字符串
+        public static string EncryptString(string plainText, string key, string iv)
+        {
+            byte[] array;
+
+            using (Aes aesAlg = Aes.Create())
+            {
+                aesAlg.Key = Encoding.UTF8.GetBytes(key);  // 密钥(必须是 16、24 或 32 字节)
+                aesAlg.IV = Encoding.UTF8.GetBytes(iv);    // 初始化向量(IV,必须是 16 字节)
+
+                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
+
+                using (MemoryStream memoryStream = new MemoryStream())
+                {
+                    using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write))
+                    {
+                        using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream))
+                        {
+                            streamWriter.Write(plainText);
+                        }
+
+                        array = memoryStream.ToArray();
+                    }
+                }
+            }
+
+            return Convert.ToBase64String(array);
+        }
+
+        public static string DecryptString(string cipherText, string key, string iv)
+        {
+            byte[] buffer = Convert.FromBase64String(cipherText);
+
+            using (Aes aesAlg = Aes.Create())
+            {
+                aesAlg.Key = Encoding.UTF8.GetBytes(key);  // 密钥(必须是 16、24 或 32 字节)
+                aesAlg.IV = Encoding.UTF8.GetBytes(iv);    // 初始化向量(IV,必须是 16 字节)
+
+                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
+
+                using (MemoryStream memoryStream = new MemoryStream(buffer))
+                {
+                    using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Read))
+                    {
+                        using (StreamReader streamReader = new StreamReader((Stream)cryptoStream))
+                        {
+                            return streamReader.ReadToEnd();
+                        }
+                    }
+                }
+            }
+        }
     }
 }

+ 82 - 17
Welling_Motor_Debug_Tool/mainForm.cs

@@ -6,6 +6,7 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics.Eventing.Reader;
 using System.Drawing;
+using System.Globalization;
 using System.IO;
 using System.IO.Ports;
 using System.Linq;
@@ -270,23 +271,7 @@ namespace Welling_Motor_Debug_Tool
             //端口初始化
             mySerialProcess.Init();
             toolStripComboBox_ComNum.Items.AddRange(mySerialProcess.refreshPort());
-
-            //检查备忘录文件
-            if (!System.IO.File.Exists(localInfo.LocalPath + localInfo.NoteFileName))
-            {
-                System.IO.File.WriteAllText(localInfo.LocalPath + localInfo.NoteFileName, "");
-            }
-
-            //检验流水号记录文件
-            if (!System.IO.File.Exists(localInfo.LocalPath + localInfo.SerialNumFileName1))
-            {
-                System.IO.File.WriteAllText(localInfo.LocalPath + localInfo.SerialNumFileName1, "0");
-            }
-            if (!System.IO.File.Exists(localInfo.LocalPath + localInfo.SerialNumFileName2))
-            {
-                System.IO.File.WriteAllText(localInfo.LocalPath + localInfo.SerialNumFileName2, "0");
-            }
-
+            
             //检查文件夹                        
             if (!Directory.Exists(localInfo.LocalPath))
             {
@@ -305,6 +290,22 @@ namespace Welling_Motor_Debug_Tool
                 Directory.CreateDirectory(localInfo.LocalPath + localInfo.CfgPathName);
             }
 
+            //检查备忘录文件
+            if (!System.IO.File.Exists(localInfo.LocalPath + localInfo.NoteFileName))
+            {
+                System.IO.File.WriteAllText(localInfo.LocalPath + localInfo.NoteFileName, "");
+            }
+
+            //检验流水号记录文件
+            if (!System.IO.File.Exists(localInfo.LocalPath + localInfo.SerialNumFileName1))
+            {
+                System.IO.File.WriteAllText(localInfo.LocalPath + localInfo.SerialNumFileName1, "0");
+            }
+            if (!System.IO.File.Exists(localInfo.LocalPath + localInfo.SerialNumFileName2))
+            {
+                System.IO.File.WriteAllText(localInfo.LocalPath + localInfo.SerialNumFileName2, "0");
+            }
+
             //服务器初始化
             do
             {
@@ -365,6 +366,70 @@ namespace Welling_Motor_Debug_Tool
                                 scan_form.ShowDialog();
                                 if (scan_form.textBox_Scan.Text == "ttium.123")
                                 {
+                                    //检查许可
+                                    if (!System.IO.File.Exists(localInfo.LocalPath + localInfo.OffLineUseKeyFileName)) //未许可
+                                    {
+                                        if (MessageBox.Show("无离线授权,请导入许可文件!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
+                                        {
+                                            Licences myLicences = new Licences();
+                                            myLicences.ShowDialog();
+                                            //从许可文件读取内容
+                                            string str_lic = aes.DecryptFromFile(myLicences.textBox_LicencePath.Text, "2D820F88F60A39D5", "3687C5216B19D16B");
+                                            string PC_Code = PC_Information.GetCPUSerialNumber();
+                                            if (PC_Code == str_lic) //授权码正确
+                                            {
+                                                aes.EncryptToFile(PC_Code, localInfo.LocalPath + localInfo.OffLineUseKeyFileName, "2D820F88F60A39D5", "3687C5216B19D16B");
+                                                string strInfo = "";
+                                                strInfo = "0;" + DateTime.Now.ToString("yyyyMMdd");
+                                                aes.EncryptToFile(strInfo, localInfo.LocalPath + localInfo.OfflineUseLogFileName, "2D820F88F60A39D5", "3687C5216B19D16B");
+                                            }
+                                            else //授权码错误
+                                            {
+                                                MessageBox.Show("许可文件无效,请联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
+                                                System.Environment.Exit(0);
+                                            }
+                                        }
+                                        else
+                                        {
+                                            System.Environment.Exit(0);
+                                        }                                        
+                                    }
+                                    else //已授权
+                                    { 
+                                        string str_Key = aes.DecryptFromFile(localInfo.LocalPath + localInfo.OffLineUseKeyFileName, "2D820F88F60A39D5", "3687C5216B19D16B");
+                                        //判断授权是否正确
+                                        if (PC_Information.GetCPUSerialNumber() == str_Key) //授权正确
+                                        {
+                                            //检查离线使用记录文件
+                                            if (!System.IO.File.Exists(localInfo.LocalPath + localInfo.OfflineUseLogFileName))
+                                            {
+                                                MessageBox.Show("许可文件内容缺失,请联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
+                                                System.Environment.Exit(0);
+                                            }
+                                            //获取离线使用记录并更新
+                                            string UseLog = aes.DecryptFromFile(localInfo.LocalPath + localInfo.OfflineUseLogFileName, "2D820F88F60A39D5", "3687C5216B19D16B");
+                                            UInt32 UseTimes = Convert.ToUInt32(UseLog.Split(';')[0]);
+                                            UseTimes++;
+                                            string str = UseTimes.ToString() + ";" + UseLog.Split(';')[1];
+                                            aes.EncryptToFile(str, localInfo.LocalPath + localInfo.OfflineUseLogFileName, "2D820F88F60A39D5", "3687C5216B19D16B");
+                                            //判断授权期限
+                                            DateTime DataOrigin = new DateTime();
+                                            DateTime.TryParseExact(UseLog.Split(';')[1], "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DataOrigin);
+                                            DateTime DataEnd = DataOrigin.AddDays(90);
+                                            DateTime NowDate = DateTime.Now.Date;
+                                            if ((NowDate > DataEnd) || (UseTimes > 1000)) //第一次打开软件90天内,或1000次内
+                                            {
+                                                MessageBox.Show("离线授权过期,请联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
+                                                System.Environment.Exit(0);
+                                            }
+                                        }
+                                        else
+                                        {
+                                            MessageBox.Show("离线授权错误,请联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
+                                            System.Environment.Exit(0);
+                                        }
+                                    }
+
                                     OfflineFlag = true;
                                     checkBox_OffLineFacMode.Checked = true;
                                     checkBox_OffLineCheckMode.Checked = true;

BIN
Welling_Motor_Debug_Tool/obj/Debug/Welling_Motor_Debug_Tool.csproj.AssemblyReference.cache


+ 1 - 1
Welling_Motor_Debug_Tool/obj/Debug/Welling_Motor_Debug_Tool.csproj.CoreCompileInputs.cache

@@ -1 +1 @@
-441adacf67438a11f7c49778bb4c20fa25df26fd58c5cada2f9c92d0e618d81a
+421b1811b0f6e3171eaafbec06f63e8227142957a15ad55679b281c198b129c4

+ 1 - 0
Welling_Motor_Debug_Tool/obj/Debug/Welling_Motor_Debug_Tool.csproj.FileListAbsolute.txt

@@ -127,3 +127,4 @@ D:\笔记本20221223\zhouxiong9\Documents\Tools\motor-debug\Welling_Motor_Debug_
 D:\笔记本20221223\zhouxiong9\Documents\Tools\motor-debug\Welling_Motor_Debug_Tool\obj\Debug\Welling_.6AF7A538.Up2Date
 D:\笔记本20221223\zhouxiong9\Documents\Tools\motor-debug\Welling_Motor_Debug_Tool\obj\Debug\Welling_Motor_Debug_Tool.exe
 D:\笔记本20221223\zhouxiong9\Documents\Tools\motor-debug\Welling_Motor_Debug_Tool\obj\Debug\Welling_Motor_Debug_Tool.pdb
+D:\笔记本20221223\zhouxiong9\Documents\Tools\motor-debug\Welling_Motor_Debug_Tool\obj\Debug\Welling_Motor_Debug_Tool.Licences.resources

BIN
Welling_Motor_Debug_Tool/obj/Debug/Welling_Motor_Debug_Tool.csproj.GenerateResource.cache


BIN
Welling_Motor_Debug_Tool/obj/Debug/Welling_Motor_Debug_Tool.exe


BIN
Welling_Motor_Debug_Tool/obj/Debug/Welling_Motor_Debug_Tool.pdb