CNAS取数仪器端升级
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ActivationForm.cs 7.2KB

4ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. if (ValidateActivationCode(activationCode))
  62. {
  63. IsActivated = true;
  64. SaveActivation(); // 保存激活状态
  65. this.DialogResult = DialogResult.OK;
  66. this.Close();
  67. }
  68. else
  69. {
  70. MessageBox.Show("激活码无效!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  71. }
  72. }
  73. catch (Exception ex)
  74. {
  75. MessageBox.Show($"激活失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  76. }
  77. }
  78. private void lnkDownloadCode_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  79. {
  80. try
  81. {
  82. string machineCode = GetMachineCode();
  83. // 创建保存文件对话框
  84. var saveFileDialog = new SaveFileDialog
  85. {
  86. FileName = $"机器码_{DateTime.Now:yyyyMMdd}",
  87. DefaultExt = ".txt",
  88. Filter = "文本文件|*.txt"
  89. };
  90. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  91. {
  92. File.WriteAllText(saveFileDialog.FileName, machineCode);
  93. MessageBox.Show("机器码已成功保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. MessageBox.Show($"获取机器码失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  99. }
  100. }
  101. private string GetMachineCode()
  102. {
  103. StringBuilder machineCode = new StringBuilder();
  104. // 获取CPU ID
  105. string cpuId = GetCPUId();
  106. machineCode.Append(cpuId);
  107. // 获取所有MAC地址
  108. List<string> macAddresses = GetMACAddresses();
  109. foreach (string mac in macAddresses)
  110. {
  111. machineCode.Append("_").Append(mac);
  112. }
  113. return machineCode.ToString();
  114. }
  115. private string GetCPUId()
  116. {
  117. try
  118. {
  119. using (ManagementClass mc = new ManagementClass("Win32_Processor"))
  120. {
  121. ManagementObjectCollection moc = mc.GetInstances();
  122. foreach (ManagementObject mo in moc)
  123. {
  124. return mo.Properties["ProcessorId"].Value.ToString();
  125. }
  126. }
  127. }
  128. catch (Exception ex)
  129. {
  130. throw new Exception("获取CPU ID失败: " + ex.Message);
  131. }
  132. return string.Empty;
  133. }
  134. private List<string> GetMACAddresses()
  135. {
  136. List<string> macAddresses = new List<string>();
  137. try
  138. {
  139. NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  140. foreach (NetworkInterface adapter in nics)
  141. {
  142. // 只获取物理网卡的MAC地址
  143. if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
  144. adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
  145. {
  146. string mac = adapter.GetPhysicalAddress().ToString();
  147. if (!string.IsNullOrEmpty(mac))
  148. {
  149. macAddresses.Add(mac);
  150. }
  151. }
  152. }
  153. }
  154. catch (Exception ex)
  155. {
  156. throw new Exception("获取MAC地址失败: " + ex.Message);
  157. }
  158. return macAddresses;
  159. }
  160. private bool ValidateActivationCode(string activationCode)
  161. {
  162. // 使用实际的机器码来验证
  163. string machineCode = GetMachineCode();
  164. return GenerateActivationCode(machineCode) == activationCode;
  165. }
  166. private string GenerateActivationCode(string machineCode)
  167. {
  168. // 这里实现激活码生成算法
  169. // 示例:简单的加密算法,实际应用中应该使用更安全的加密方式
  170. using (var md5 = System.Security.Cryptography.MD5.Create())
  171. {
  172. // 加入时间戳使得生成的激活码具有时效性
  173. string input = machineCode + DateTime.Now.ToString("yyyyMMdd");
  174. byte[] inputBytes = Encoding.UTF8.GetBytes(input);
  175. byte[] hashBytes = md5.ComputeHash(inputBytes);
  176. // 转换为16进制字符串
  177. StringBuilder sb = new StringBuilder();
  178. for (int i = 0; i < hashBytes.Length; i++)
  179. {
  180. sb.Append(hashBytes[i].ToString("X2"));
  181. // 每4个字符添加一个分隔符,提高可读性
  182. if ((i + 1) % 4 == 0 && i != hashBytes.Length - 1)
  183. {
  184. sb.Append("-");
  185. }
  186. }
  187. return sb.ToString();
  188. }
  189. }
  190. }
  191. }