175 lines
6.7 KiB
C#
175 lines
6.7 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Globalization;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Management;
|
|||
|
|
using System.Net;
|
|||
|
|
using System.Reflection;
|
|||
|
|
using System.Security.Cryptography;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Microsoft.Win32;
|
|||
|
|
|
|||
|
|
namespace ShrlAlgoToolkit.Core.Assists
|
|||
|
|
{
|
|||
|
|
public class LicenseEngine
|
|||
|
|
{
|
|||
|
|
// 【重要】替换为你生成的公钥
|
|||
|
|
private const string PublicKey = @"<RSAKeyValue><Modulus>rd5+EjU6QxaTY/AalU9g6ugAquN0ahJSgeahnf2CrfvUAWFNJ+SH7Qr0RcvOTyAbWfvLWoBDACKaPsg+8nRQcO3EZdFyjJ9oycLNrw38+gSB2+/79Axys/8MHtSXVUw9WN2e9LxeHOQGtcaoSsp+bPGaXswthovQ2CkvBTmCokcAOX6UaR7Av4npaXyEoGCUsZLEiSydMsNbQ+wLsumVg1H2o/cpkO0s4DiJHoF66zUuxA+pYSjSCn5KTUsOemf+gBsob6Sw+7WOToyiOkMdO9Op6esf8yL7DJ1Yd40XaKd5IeLQmEX/b+n1EV2JEkGO0p9q9MQRp6NrEc9LvfMJWQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
|
|||
|
|
|
|||
|
|
private static string RegPath = @$"Software\{Assembly.GetExecutingAssembly().GetName()}\License";
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 完整验证逻辑
|
|||
|
|
/// </summary>
|
|||
|
|
public static (bool isValid, string message) Validate()
|
|||
|
|
{
|
|||
|
|
string licenseKey = LoadFromRegistry("Key");
|
|||
|
|
if (string.IsNullOrEmpty(licenseKey)) return (false, "未找到授权码");
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
// 1. RSA 解密/验证逻辑
|
|||
|
|
// 激活码格式:签名(Base64)|机器码|到期日期
|
|||
|
|
var parts = licenseKey.Split('|');
|
|||
|
|
if (parts.Length != 3) return (false, "授权格式非法");
|
|||
|
|
|
|||
|
|
string signature = parts[0];
|
|||
|
|
string mCode = parts[1];
|
|||
|
|
string expiryStr = parts[2];
|
|||
|
|
|
|||
|
|
// 2. 验证是否是本机的机器码
|
|||
|
|
if (mCode != GetMachineCode()) return (false, "授权码与本机硬件不匹配");
|
|||
|
|
|
|||
|
|
// 3. 验证 RSA 签名(确保数据没被篡改)
|
|||
|
|
if (!VerifySignature($"{mCode}|{expiryStr}", signature)) return (false, "授权签名校验失败");
|
|||
|
|
|
|||
|
|
// 4. 时间校验
|
|||
|
|
DateTime expiryDate = DateTime.ParseExact(expiryStr, "yyyyMMdd", CultureInfo.InvariantCulture);
|
|||
|
|
DateTime now = GetRobustDateTime();
|
|||
|
|
|
|||
|
|
// 防回滚检查
|
|||
|
|
DateTime lastRun = GetLastRunTime();
|
|||
|
|
if (now < lastRun) return (false, "系统时间异常,请检查时钟");
|
|||
|
|
UpdateLastRunTime(now);
|
|||
|
|
|
|||
|
|
if (now > expiryDate) return (false, $"授权已于 {expiryDate:yyyy-MM-dd} 到期");
|
|||
|
|
|
|||
|
|
return (true, "授权有效");
|
|||
|
|
}
|
|||
|
|
catch { return (false, "解析授权出错"); }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static bool VerifySignature(string data, string signature)
|
|||
|
|
{
|
|||
|
|
using (var rsa = new RSACryptoServiceProvider())
|
|||
|
|
{
|
|||
|
|
rsa.FromXmlString(PublicKey);
|
|||
|
|
var formatter = new RSAPKCS1SignatureDeformatter(rsa);
|
|||
|
|
formatter.SetHashAlgorithm("SHA256");
|
|||
|
|
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
|
|||
|
|
using (var sha = SHA256.Create())
|
|||
|
|
{
|
|||
|
|
byte[] hash = sha.ComputeHash(dataBytes);
|
|||
|
|
return formatter.VerifySignature(hash, Convert.FromBase64String(signature));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static string GetMachineCode()
|
|||
|
|
{
|
|||
|
|
// 组合 CPU ID + 硬盘 ID
|
|||
|
|
string raw = GetHardwareId("Win32_Processor", "ProcessorId") +
|
|||
|
|
GetHardwareId("Win32_PhysicalMedia", "SerialNumber");
|
|||
|
|
using (var sha = SHA256.Create())
|
|||
|
|
{
|
|||
|
|
byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(raw));
|
|||
|
|
return BitConverter.ToString(hash).Replace("-", "").Substring(0, 16);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static string GetHardwareId(string wmiClass, string property)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (var mc = new ManagementClass(wmiClass))
|
|||
|
|
foreach (var mo in mc.GetInstances()) return mo[property]?.ToString().Trim();
|
|||
|
|
}
|
|||
|
|
catch { }
|
|||
|
|
return "UNKNOWN";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 获取可靠的时间(网络优先,本地其次)
|
|||
|
|
private static DateTime GetRobustDateTime()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var request = WebRequest.Create("http://www.baidu.com");
|
|||
|
|
request.Timeout = 2000;
|
|||
|
|
using (var response = request.GetResponse())
|
|||
|
|
{
|
|||
|
|
string dateStr = response.Headers["Date"];
|
|||
|
|
return DateTime.ParseExact(dateStr, "ddd, dd MMM yyyy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch { return DateTime.Now; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#region 注册表操作
|
|||
|
|
private static DateTime GetLastRunTime()
|
|||
|
|
{
|
|||
|
|
string val = LoadFromRegistry("T");
|
|||
|
|
return string.IsNullOrEmpty(val) ? DateTime.MinValue : new DateTime(long.Parse(val));
|
|||
|
|
}
|
|||
|
|
private static void UpdateLastRunTime(DateTime now) => SaveToRegistry("T", now.Ticks.ToString());
|
|||
|
|
|
|||
|
|
public static void SaveToRegistry(string key, string val)
|
|||
|
|
{
|
|||
|
|
using var r = Registry.CurrentUser.CreateSubKey(RegPath);
|
|||
|
|
r.SetValue(key, val);
|
|||
|
|
}
|
|||
|
|
private static string LoadFromRegistry(string key)
|
|||
|
|
{
|
|||
|
|
using var r = Registry.CurrentUser.OpenSubKey(RegPath);
|
|||
|
|
return r?.GetValue(key)?.ToString();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 仅移除当前的激活码(重置授权状态,但保留时间戳防止白嫖)
|
|||
|
|
/// </summary>
|
|||
|
|
public static void ClearLicenseKey()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (var r = Registry.CurrentUser.OpenSubKey(RegPath, true))
|
|||
|
|
{
|
|||
|
|
if (r != null && r.GetValue("Key") != null)
|
|||
|
|
{
|
|||
|
|
r.DeleteValue("Key");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("移除授权码失败: " + ex.Message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 彻底销毁所有授权痕迹(包括时间戳记录,通常用于卸载)
|
|||
|
|
/// </summary>
|
|||
|
|
public static void DestroyAllLicenseData()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
// 第二个参数为 false 表示:如果路径不存在,不抛出异常
|
|||
|
|
Registry.CurrentUser.DeleteSubKeyTree(RegPath, false);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("彻底清理注册表失败: " + ex.Message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
}
|