using System; using System.Text; using System.Windows.Forms; using System.Management; using System.Net.NetworkInformation; using System.Collections.Generic; using System.IO; using System.Configuration; using System.Linq; using CnasSynchronousCommon; namespace CNAS_DBSync { public partial class ActivationForm : Form { private const string ACTIVATION_FILE = "activation.config"; public bool IsActivated { get; private set; } public ActivationForm() { InitializeComponent(); IsActivated = false; } // 检查是否已激活 public bool CheckActivation() { try { string activationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ACTIVATION_FILE); if (File.Exists(activationPath)) { string machineCode = MachieCodeHelper.GetMachineCode(); string savedMachineCode = File.ReadAllText(activationPath); return machineCode == savedMachineCode; } } catch { // 如果读取失败,返回未激活状态 } return false; } private void SaveActivation() { try { string activationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ACTIVATION_FILE); File.WriteAllText(activationPath, MachieCodeHelper.GetMachineCode()); } catch (Exception ex) { MessageBox.Show($"保存激活状态失败:{ex.Message}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void btnActivate_Click(object sender, EventArgs e) { string activationCode = txtActivationCode.Text.Trim(); if (string.IsNullOrEmpty(activationCode)) { MessageBox.Show("请输入激活码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } try { //绕过验证 if (ValidateActivationCode(activationCode)) { IsActivated = true; SaveActivation(); // 保存激活状态 this.DialogResult = DialogResult.OK; this.Close(); } else { MessageBox.Show("激活码无效!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { MessageBox.Show($"激活失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void lnkDownloadCode_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { try { string machineCode = MachieCodeHelper.GetMachineCode(); // 创建保存文件对话框 var saveFileDialog = new SaveFileDialog { FileName = $"机器码_{DateTime.Now:yyyyMMdd}", DefaultExt = ".txt", Filter = "文本文件|*.txt" }; if (saveFileDialog.ShowDialog() == DialogResult.OK) { File.WriteAllText(saveFileDialog.FileName, machineCode); MessageBox.Show("机器码已成功保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show($"获取机器码失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private bool ValidateActivationCode(string activationCode) { // 使用实际的机器码来验证 string machineCode = MachieCodeHelper.GetMachineCode(); return GenerateActivationCode(machineCode) == activationCode; } private string GenerateActivationCode(string machineCode) { // 这里实现激活码生成算法 // 示例:简单的加密算法,实际应用中应该使用更安全的加密方式 using (var md5 = System.Security.Cryptography.MD5.Create()) { // 加入时间戳使得生成的激活码具有时效性 string input = machineCode + DateTime.Now.ToString("yyyyMMdd"); byte[] inputBytes = Encoding.UTF8.GetBytes(input); byte[] hashBytes = md5.ComputeHash(inputBytes); // 转换为16进制字符串 StringBuilder sb = new StringBuilder(); for (int i = 0; i < hashBytes.Length; i++) { sb.Append(hashBytes[i].ToString("X2")); // 每4个字符添加一个分隔符,提高可读性 if ((i + 1) % 4 == 0 && i != hashBytes.Length - 1) { sb.Append("-"); } } return sb.ToString(); } } } }