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

147 行
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 CnasSynchronousCommon;
  10. namespace CNAS_DBSync
  11. {
  12. public partial class ActivationForm : Form
  13. {
  14. private const string ACTIVATION_FILE = "activation.config";
  15. public bool IsActivated { get; private set; }
  16. public ActivationForm()
  17. {
  18. InitializeComponent();
  19. IsActivated = false;
  20. }
  21. // 检查是否已激活
  22. public bool CheckActivation()
  23. {
  24. try
  25. {
  26. string activationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ACTIVATION_FILE);
  27. if (File.Exists(activationPath))
  28. {
  29. string machineCode =MachieCodeHelper.GetMachineCode();
  30. string savedMachineCode = File.ReadAllText(activationPath);
  31. return machineCode == savedMachineCode;
  32. }
  33. }
  34. catch
  35. {
  36. // 如果读取失败,返回未激活状态
  37. }
  38. return false;
  39. }
  40. private void SaveActivation()
  41. {
  42. try
  43. {
  44. string activationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ACTIVATION_FILE);
  45. File.WriteAllText(activationPath,MachieCodeHelper.GetMachineCode());
  46. }
  47. catch (Exception ex)
  48. {
  49. MessageBox.Show($"保存激活状态失败:{ex.Message}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  50. }
  51. }
  52. private void btnActivate_Click(object sender, EventArgs e)
  53. {
  54. string activationCode = txtActivationCode.Text.Trim();
  55. if (string.IsNullOrEmpty(activationCode))
  56. {
  57. MessageBox.Show("请输入激活码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  58. return;
  59. }
  60. try
  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 =MachieCodeHelper.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 bool ValidateActivationCode(string activationCode)
  103. {
  104. // 使用实际的机器码来验证
  105. string machineCode =MachieCodeHelper.GetMachineCode();
  106. return GenerateActivationCode(machineCode) == activationCode;
  107. }
  108. private string GenerateActivationCode(string machineCode)
  109. {
  110. // 这里实现激活码生成算法
  111. // 示例:简单的加密算法,实际应用中应该使用更安全的加密方式
  112. using (var md5 = System.Security.Cryptography.MD5.Create())
  113. {
  114. // 加入时间戳使得生成的激活码具有时效性
  115. string input = machineCode + DateTime.Now.ToString("yyyyMMdd");
  116. byte[] inputBytes = Encoding.UTF8.GetBytes(input);
  117. byte[] hashBytes = md5.ComputeHash(inputBytes);
  118. // 转换为16进制字符串
  119. StringBuilder sb = new StringBuilder();
  120. for (int i = 0; i < hashBytes.Length; i++)
  121. {
  122. sb.Append(hashBytes[i].ToString("X2"));
  123. // 每4个字符添加一个分隔符,提高可读性
  124. if ((i + 1) % 4 == 0 && i != hashBytes.Length - 1)
  125. {
  126. sb.Append("-");
  127. }
  128. }
  129. return sb.ToString();
  130. }
  131. }
  132. }
  133. }