CRegistration.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Management;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using System.Xml;
  10. namespace OTSMeasureApp
  11. {
  12. class CRegistration
  13. {
  14. public bool RegistrationVerification()
  15. {
  16. if (!checkRegistration())//文件授权法
  17. {
  18. //return false;
  19. try
  20. {
  21. uint[] code;
  22. code = new uint[2];
  23. code[0] = 101;
  24. code[1] = 102;
  25. //狗加密法
  26. SenseShield.DogDecrypting.decrypting(code);//参数为许可号
  27. }
  28. catch (Exception ex)
  29. {
  30. //MessageBox.Show("Error: missing dog authorization");
  31. return false;
  32. }
  33. }
  34. return true;
  35. }
  36. private bool checkRegistration()
  37. {
  38. string CPUID, DiskID;
  39. CPUID = getCpu();
  40. DiskID = GetDiskVolumeSerialNumber();
  41. var ID = ReadXML("./Config/SysData/RegistrationProofreading.txt");
  42. if (ID.Count == 0)
  43. {
  44. return false;
  45. }
  46. List<string> list_str = new List<string>();
  47. List<string> list_time = new List<string>();
  48. ID.TryGetValue("ID", out list_str);
  49. ID.TryGetValue("Time", out list_time);
  50. string setCPU = ConvertString(list_str[0]);
  51. string setDisk = ConvertString(list_str[1]);
  52. string setYear = ConvertString(list_time[0]);
  53. string setMonth = ConvertString(list_time[1]);
  54. string setDay = ConvertString(list_time[2]);
  55. if (CPUID != setCPU || DiskID != setDisk)
  56. {
  57. return false;
  58. }
  59. DateTime dt = DateTime.ParseExact(setYear + setMonth + setDay + "235959", "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  60. if (DateTime.Now > dt)
  61. {
  62. return false;
  63. }
  64. return true;
  65. }
  66. // 获得CPU的序列号
  67. private static string getCpu()
  68. {
  69. string strCpu = null;
  70. ManagementClass myCpu = new ManagementClass("win32_Processor");
  71. ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
  72. foreach (ManagementObject myObject in myCpuConnection)
  73. {
  74. strCpu = myObject.Properties["Processorid"].Value.ToString();
  75. break;
  76. }
  77. return strCpu;
  78. }
  79. // 取得设备硬盘的卷标号
  80. private static string GetDiskVolumeSerialNumber()
  81. {
  82. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  83. ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
  84. disk.Get();
  85. return disk.GetPropertyValue("VolumeSerialNumber").ToString();
  86. }
  87. private static IDictionary<String, List<String>> ReadXML(string address)
  88. {
  89. IDictionary<String, List<String>> infos = new Dictionary<String, List<String>>();
  90. if (File.Exists(address))
  91. {
  92. XmlDocument xmlDoc = new XmlDocument();
  93. xmlDoc.Load(address);
  94. XmlNode xn = xmlDoc.SelectSingleNode("infos");
  95. XmlNodeList xnl = xn.ChildNodes;
  96. foreach (XmlNode xnf in xnl)
  97. {
  98. XmlElement xe = (XmlElement)xnf;
  99. XmlNode nameNode = xe.SelectSingleNode("ID");
  100. string name = nameNode.InnerText;
  101. Console.WriteLine(name);
  102. XmlNode filelist = xe.SelectSingleNode("filelist");
  103. List<String> list = new List<string>();
  104. foreach (XmlNode item in filelist.ChildNodes)
  105. {
  106. list.Add(item.InnerText);
  107. }
  108. infos.Add(name, list);
  109. }
  110. }
  111. return infos;
  112. }
  113. private string ConvertString(string str)
  114. {
  115. string str_out;
  116. string[] arr = str.Split(',');
  117. byte[] be = new byte[arr.Count()];
  118. for (int i = 0; i < arr.Count(); i++)
  119. {
  120. be[i] = Convert.ToByte(arr[i]);
  121. }
  122. str_out = Encoding.Unicode.GetString(be);
  123. return str_out;
  124. }
  125. }
  126. }