CNAS取数仪器端升级
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

211 líneas
7.2KB

  1. using System;
  2. using System.Text;
  3. using System.Windows.Forms;
  4. using System.Management;
  5. using System.Net.NetworkInformation;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Configuration;
  9. namespace CNAS_DBSync
  10. {
  11. public partial class ActivationForm : Form
  12. {
  13. private const string ACTIVATION_FILE = "activation.config";
  14. public bool IsActivated { get; private set; }
  15. public ActivationForm()
  16. {
  17. InitializeComponent();
  18. IsActivated = false;
  19. }
  20. // 检查是否已激活
  21. public bool CheckActivation()
  22. {
  23. try
  24. {
  25. string activationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ACTIVATION_FILE);
  26. if (File.Exists(activationPath))
  27. {
  28. string machineCode = GetMachineCode();
  29. string savedMachineCode = File.ReadAllText(activationPath);
  30. return machineCode == savedMachineCode;
  31. }
  32. }
  33. catch
  34. {
  35. // 如果读取失败,返回未激活状态
  36. }
  37. return false;
  38. }
  39. private void SaveActivation()
  40. {
  41. try
  42. {
  43. string activationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ACTIVATION_FILE);
  44. File.WriteAllText(activationPath, GetMachineCode());
  45. }
  46. catch (Exception ex)
  47. {
  48. MessageBox.Show($"保存激活状态失败:{ex.Message}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  49. }
  50. }
  51. private void btnActivate_Click(object sender, EventArgs e)
  52. {
  53. string activationCode = txtActivationCode.Text.Trim();
  54. if (string.IsNullOrEmpty(activationCode))
  55. {
  56. MessageBox.Show("请输入激活码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  57. return;
  58. }
  59. try
  60. {
  61. //绕过验证
  62. if (!ValidateActivationCode(activationCode))
  63. {
  64. IsActivated = true;
  65. SaveActivation(); // 保存激活状态
  66. this.DialogResult = DialogResult.OK;
  67. this.Close();
  68. }
  69. else
  70. {
  71. MessageBox.Show("激活码无效!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. MessageBox.Show($"激活失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  77. }
  78. }
  79. private void lnkDownloadCode_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  80. {
  81. try
  82. {
  83. string machineCode = GetMachineCode();
  84. // 创建保存文件对话框
  85. var saveFileDialog = new SaveFileDialog
  86. {
  87. FileName = $"机器码_{DateTime.Now:yyyyMMdd}",
  88. DefaultExt = ".txt",
  89. Filter = "文本文件|*.txt"
  90. };
  91. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  92. {
  93. File.WriteAllText(saveFileDialog.FileName, machineCode);
  94. MessageBox.Show("机器码已成功保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  95. }
  96. }
  97. catch (Exception ex)
  98. {
  99. MessageBox.Show($"获取机器码失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  100. }
  101. }
  102. private string GetMachineCode()
  103. {
  104. StringBuilder machineCode = new StringBuilder();
  105. // 获取CPU ID
  106. string cpuId = GetCPUId();
  107. machineCode.Append(cpuId);
  108. // 获取所有MAC地址
  109. List<string> macAddresses = GetMACAddresses();
  110. foreach (string mac in macAddresses)
  111. {
  112. machineCode.Append("_").Append(mac);
  113. }
  114. return machineCode.ToString();
  115. }
  116. private string GetCPUId()
  117. {
  118. try
  119. {
  120. using (ManagementClass mc = new ManagementClass("Win32_Processor"))
  121. {
  122. ManagementObjectCollection moc = mc.GetInstances();
  123. foreach (ManagementObject mo in moc)
  124. {
  125. return mo.Properties["ProcessorId"].Value.ToString();
  126. }
  127. }
  128. }
  129. catch (Exception ex)
  130. {
  131. throw new Exception("获取CPU ID失败: " + ex.Message);
  132. }
  133. return string.Empty;
  134. }
  135. private List<string> GetMACAddresses()
  136. {
  137. List<string> macAddresses = new List<string>();
  138. try
  139. {
  140. NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  141. foreach (NetworkInterface adapter in nics)
  142. {
  143. // 只获取物理网卡的MAC地址
  144. if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
  145. adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
  146. {
  147. string mac = adapter.GetPhysicalAddress().ToString();
  148. if (!string.IsNullOrEmpty(mac))
  149. {
  150. macAddresses.Add(mac);
  151. }
  152. }
  153. }
  154. }
  155. catch (Exception ex)
  156. {
  157. throw new Exception("获取MAC地址失败: " + ex.Message);
  158. }
  159. return macAddresses;
  160. }
  161. private bool ValidateActivationCode(string activationCode)
  162. {
  163. // 使用实际的机器码来验证
  164. string machineCode = GetMachineCode();
  165. return GenerateActivationCode(machineCode) == activationCode;
  166. }
  167. private string GenerateActivationCode(string machineCode)
  168. {
  169. // 这里实现激活码生成算法
  170. // 示例:简单的加密算法,实际应用中应该使用更安全的加密方式
  171. using (var md5 = System.Security.Cryptography.MD5.Create())
  172. {
  173. // 加入时间戳使得生成的激活码具有时效性
  174. string input = machineCode + DateTime.Now.ToString("yyyyMMdd");
  175. byte[] inputBytes = Encoding.UTF8.GetBytes(input);
  176. byte[] hashBytes = md5.ComputeHash(inputBytes);
  177. // 转换为16进制字符串
  178. StringBuilder sb = new StringBuilder();
  179. for (int i = 0; i < hashBytes.Length; i++)
  180. {
  181. sb.Append(hashBytes[i].ToString("X2"));
  182. // 每4个字符添加一个分隔符,提高可读性
  183. if ((i + 1) % 4 == 0 && i != hashBytes.Length - 1)
  184. {
  185. sb.Append("-");
  186. }
  187. }
  188. return sb.ToString();
  189. }
  190. }
  191. }
  192. }