CNAS取数仪器端升级
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

173 行
5.9KB

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