CNAS取数仪器端升级
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

150 wiersze
5.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. using System.Linq;
  10. using CnasSynchronousCommon;
  11. namespace CNAS_DBSync
  12. {
  13. public partial class ActivationForm : Form
  14. {
  15. private const string ACTIVATION_FILE = "activation.config";
  16. public bool IsActivated { get; private set; }
  17. public ActivationForm()
  18. {
  19. InitializeComponent();
  20. IsActivated = false;
  21. }
  22. // 检查是否已激活
  23. public bool CheckActivation()
  24. {
  25. try
  26. {
  27. string activationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ACTIVATION_FILE);
  28. if (File.Exists(activationPath))
  29. {
  30. string machineCode = MachieCodeHelper.GetMachineCode();
  31. string savedMachineCode = File.ReadAllText(activationPath);
  32. return machineCode == savedMachineCode;
  33. }
  34. }
  35. catch
  36. {
  37. // 如果读取失败,返回未激活状态
  38. }
  39. return false;
  40. }
  41. private void SaveActivation()
  42. {
  43. try
  44. {
  45. string activationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ACTIVATION_FILE);
  46. File.WriteAllText(activationPath, MachieCodeHelper.GetMachineCode());
  47. }
  48. catch (Exception ex)
  49. {
  50. MessageBox.Show($"保存激活状态失败:{ex.Message}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  51. }
  52. }
  53. private void btnActivate_Click(object sender, EventArgs e)
  54. {
  55. string activationCode = txtActivationCode.Text.Trim();
  56. if (string.IsNullOrEmpty(activationCode))
  57. {
  58. MessageBox.Show("请输入激活码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  59. return;
  60. }
  61. try
  62. {
  63. //绕过验证
  64. if (!ValidateActivationCode(activationCode))
  65. {
  66. IsActivated = true;
  67. SaveActivation(); // 保存激活状态
  68. this.DialogResult = DialogResult.OK;
  69. this.Close();
  70. }
  71. else
  72. {
  73. MessageBox.Show("激活码无效!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  74. }
  75. }
  76. catch (Exception ex)
  77. {
  78. MessageBox.Show($"激活失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  79. }
  80. }
  81. private void lnkDownloadCode_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  82. {
  83. try
  84. {
  85. string machineCode = MachieCodeHelper.GetMachineCode();
  86. // 创建保存文件对话框
  87. var saveFileDialog = new SaveFileDialog
  88. {
  89. FileName = $"机器码_{DateTime.Now:yyyyMMdd}",
  90. DefaultExt = ".txt",
  91. Filter = "文本文件|*.txt"
  92. };
  93. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  94. {
  95. File.WriteAllText(saveFileDialog.FileName, machineCode);
  96. MessageBox.Show("机器码已成功保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. MessageBox.Show($"获取机器码失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  102. }
  103. }
  104. private bool ValidateActivationCode(string activationCode)
  105. {
  106. // 使用实际的机器码来验证
  107. string machineCode = MachieCodeHelper.GetMachineCode();
  108. return GenerateActivationCode(machineCode) == activationCode;
  109. }
  110. private string GenerateActivationCode(string machineCode)
  111. {
  112. // 这里实现激活码生成算法
  113. // 示例:简单的加密算法,实际应用中应该使用更安全的加密方式
  114. using (var md5 = System.Security.Cryptography.MD5.Create())
  115. {
  116. // 加入时间戳使得生成的激活码具有时效性
  117. string input = machineCode + DateTime.Now.ToString("yyyyMMdd");
  118. byte[] inputBytes = Encoding.UTF8.GetBytes(input);
  119. byte[] hashBytes = md5.ComputeHash(inputBytes);
  120. // 转换为16进制字符串
  121. StringBuilder sb = new StringBuilder();
  122. for (int i = 0; i < hashBytes.Length; i++)
  123. {
  124. sb.Append(hashBytes[i].ToString("X2"));
  125. // 每4个字符添加一个分隔符,提高可读性
  126. if ((i + 1) % 4 == 0 && i != hashBytes.Length - 1)
  127. {
  128. sb.Append("-");
  129. }
  130. }
  131. return sb.ToString();
  132. }
  133. }
  134. }
  135. }