|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- 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;
-
- 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 = 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, 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 = 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 string GetMachineCode()
- {
- StringBuilder machineCode = new StringBuilder();
-
- // 获取CPU ID
- string cpuId = GetCPUId();
- machineCode.Append(cpuId);
-
- // 获取所有MAC地址
- List<string> macAddresses = GetMACAddresses();
- foreach (string mac in macAddresses)
- {
- machineCode.Append("_").Append(mac);
- }
-
- return machineCode.ToString();
- }
-
- private string GetCPUId()
- {
- try
- {
- using (ManagementClass mc = new ManagementClass("Win32_Processor"))
- {
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- return mo.Properties["ProcessorId"].Value.ToString();
- }
- }
- }
- catch (Exception ex)
- {
- throw new Exception("获取CPU ID失败: " + ex.Message);
- }
- return string.Empty;
- }
-
- private List<string> GetMACAddresses()
- {
- List<string> macAddresses = new List<string>();
- try
- {
- NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
- foreach (NetworkInterface adapter in nics)
- {
- // 只获取物理网卡的MAC地址
- if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
- adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
- {
- string mac = adapter.GetPhysicalAddress().ToString();
- if (!string.IsNullOrEmpty(mac))
- {
- macAddresses.Add(mac);
- }
- }
- }
- }
- catch (Exception ex)
- {
- throw new Exception("获取MAC地址失败: " + ex.Message);
- }
- return macAddresses;
- }
-
- private bool ValidateActivationCode(string activationCode)
- {
- // 使用实际的机器码来验证
- string machineCode = 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();
- }
- }
- }
- }
|