123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Management;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- namespace MeterVision.Util
- {
- public class RegistrationHelper
- {
- // 生成机器码
- public static string GenerateMachineCode()
- {
- // 获取硬件信息
- //string hddSerial = GetHDDSerial();
- string cpuId = GetCPUId();
- //string macAddress = GetMACAddress();
- // 组合硬件信息并生成哈希
- using (SHA256 sha256 = SHA256.Create())
- {
- //byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(hddSerial + cpuId + macAddress));
- byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(cpuId));
- return Convert.ToBase64String(hashBytes);
- }
- }
- // 生成注册码(使用私钥签名)
- //public static string GenerateRegistrationCode(string machineCode, DateTime expirationDate, RSA privateKey)
- //{
- // // 将机器码和有效期组合成明文
- // string plainText = $"{machineCode}|{expirationDate:yyyy-MM-dd}";
- // // 使用私钥对明文进行签名
- // byte[] dataToSign = Encoding.UTF8.GetBytes(plainText);
- // byte[] signature = privateKey.SignData(dataToSign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
- // // 将签名和明文组合成注册码
- // return Convert.ToBase64String(signature) + "|" + plainText;
- //}
- public static bool ValidateRegistrationCode(string registrationCode, RSA publicKey, out DateTime expirationDate)
- {
- try
- {
- string[] parts = registrationCode.Split('|');
- byte[] signature = Convert.FromBase64String(parts[0]);
- string plainText = parts[1];
- // 验证签名
- byte[] dataToVerify = Encoding.UTF8.GetBytes(plainText);
- bool isValid = publicKey.VerifyData(dataToVerify, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
- if (isValid)
- {
- string[] dataParts = plainText.Split('|');
- string machineCode = dataParts[0];
- expirationDate = DateTime.Parse(dataParts[1]);
- // 验证机器码是否匹配
- if (machineCode == GenerateMachineCode())
- {
- // 验证有效期
- if (DateTime.Now <= expirationDate)
- {
- return true;
- }
- }
- }
- }
- catch
- {
- // 捕获任何异常,表示验证失败
- }
- expirationDate = DateTime.MinValue;
- return false;
- }
- // 获取硬盘序列号
- private static string GetHDDSerial()
- {
- // 实现获取硬盘序列号的逻辑
- return "HDD_SERIAL_12345";
- }
- // 获取MAC地址
- private static string GetMACAddress()
- {
- // 实现获取MAC地址的逻辑
- return "00-1A-2B-3C-4D-5E";
- }
- public static string GetCPUId()
- {
- try
- {
- // 查询CPU的ProcessorId
- using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"))
- {
- foreach (ManagementObject cpu in searcher.Get())
- {
- if (cpu["ProcessorId"] != null)
- {
- return cpu["ProcessorId"].ToString().Trim();
- }
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("获取CPU ID时出错: " + ex.Message);
- }
- //return "无法获取CPU ID";
- return "CPU_ID_ABCDEF";
- }
- //private string GetMachineCode()
- //{
- // string cpuId = string.Empty;
- // using (var searcher = new ManagementObjectSearcher("SELECT ProcessorId FROM Win32_Processor"))
- // {
- // foreach (ManagementObject obj in searcher.Get())
- // {
- // if (!string.IsNullOrEmpty(cpuId)) break; // 只取第一个CPU ID
- // cpuId = obj["ProcessorId"]?.ToString();
- // }
- // }
- // string diskSerial = string.Empty;
- // using (var searcher = new ManagementObjectSearcher("SELECT VolumeSerialNumber FROM Win32_LogicalDisk WHERE DeviceID='C:'"))
- // {
- // foreach (ManagementObject obj in searcher.Get())
- // {
- // diskSerial = obj["VolumeSerialNumber"]?.ToString();
- // break; // C盘的序列号
- // }
- // }
- // return $"{cpuId}{diskSerial}";
- //}
- //------------------------------------------------------
- }
- }
|