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

148 行
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 CNASBalanceDBManage
  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. //绕过验证
  63. if (ValidateActivationCode(activationCode))
  64. {
  65. IsActivated = true;
  66. SaveActivation(); // 保存激活状态
  67. this.DialogResult = DialogResult.OK;
  68. this.Close();
  69. }
  70. else
  71. {
  72. MessageBox.Show("激活码无效!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. MessageBox.Show($"激活失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  78. }
  79. }
  80. private void lnkDownloadCode_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  81. {
  82. try
  83. {
  84. string machineCode =MachieCodeHelper.GetMachineCode();
  85. // 创建保存文件对话框
  86. var saveFileDialog = new SaveFileDialog
  87. {
  88. FileName = $"机器码_{DateTime.Now:yyyyMMdd}",
  89. DefaultExt = ".txt",
  90. Filter = "文本文件|*.txt"
  91. };
  92. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  93. {
  94. File.WriteAllText(saveFileDialog.FileName, machineCode);
  95. MessageBox.Show("机器码已成功保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. MessageBox.Show($"获取机器码失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  101. }
  102. }
  103. private bool ValidateActivationCode(string activationCode)
  104. {
  105. // 使用实际的机器码来验证
  106. string machineCode =MachieCodeHelper.GetMachineCode();
  107. return GenerateActivationCode(machineCode) == activationCode;
  108. }
  109. private string GenerateActivationCode(string machineCode)
  110. {
  111. // 这里实现激活码生成算法
  112. // 示例:简单的加密算法,实际应用中应该使用更安全的加密方式
  113. using (var md5 = System.Security.Cryptography.MD5.Create())
  114. {
  115. // 加入时间戳使得生成的激活码具有时效性
  116. string input = machineCode + DateTime.Now.ToString("yyyyMMdd");
  117. byte[] inputBytes = Encoding.UTF8.GetBytes(input);
  118. byte[] hashBytes = md5.ComputeHash(inputBytes);
  119. // 转换为16进制字符串
  120. StringBuilder sb = new StringBuilder();
  121. for (int i = 0; i < hashBytes.Length; i++)
  122. {
  123. sb.Append(hashBytes[i].ToString("X2"));
  124. // 每4个字符添加一个分隔符,提高可读性
  125. if ((i + 1) % 4 == 0 && i != hashBytes.Length - 1)
  126. {
  127. sb.Append("-");
  128. }
  129. }
  130. return sb.ToString();
  131. }
  132. }
  133. }
  134. }