98 lines
3.2 KiB
C#
98 lines
3.2 KiB
C#
|
|
using System;
|
|||
|
|
using System.Management;
|
|||
|
|
using System.Security.Cryptography;
|
|||
|
|
using System.Text;
|
|||
|
|
|
|||
|
|
namespace WpfApp.Utils
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 硬件信息工具类,用于获取主板和CPU序列号
|
|||
|
|
/// </summary>
|
|||
|
|
public static class HardwareInfo
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取主板序列号
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>主板序列号</returns>
|
|||
|
|
public static string GetMotherboardSerialNumber()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (var searcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_BaseBoard"))
|
|||
|
|
{
|
|||
|
|
foreach (var obj in searcher.Get())
|
|||
|
|
{
|
|||
|
|
return obj["SerialNumber"].ToString().Trim();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine($"获取主板序列号时出错: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
return string.Empty;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取CPU序列号
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>CPU序列号</returns>
|
|||
|
|
public static string GetCpuSerialNumber()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (var searcher = new ManagementObjectSearcher("SELECT ProcessorId FROM Win32_Processor"))
|
|||
|
|
{
|
|||
|
|
foreach (var obj in searcher.Get())
|
|||
|
|
{
|
|||
|
|
return obj["ProcessorId"].ToString().Trim();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine($"获取CPU序列号时出错: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
return string.Empty;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取硬件标识(主板序列号和CPU序列号的组合哈希值)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>硬件标识哈希值</returns>
|
|||
|
|
public static string GetHardwareId()
|
|||
|
|
{
|
|||
|
|
string motherboardSn = GetMotherboardSerialNumber();
|
|||
|
|
string cpuSn = GetCpuSerialNumber();
|
|||
|
|
string combined = $"{motherboardSn}|{cpuSn}";
|
|||
|
|
|
|||
|
|
// 计算组合值的SHA256哈希
|
|||
|
|
using (SHA256 sha256 = SHA256.Create())
|
|||
|
|
{
|
|||
|
|
byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(combined));
|
|||
|
|
StringBuilder builder = new StringBuilder();
|
|||
|
|
for (int i = 0; i < hashBytes.Length; i++)
|
|||
|
|
{
|
|||
|
|
builder.Append(hashBytes[i].ToString("x2"));
|
|||
|
|
}
|
|||
|
|
return builder.ToString();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 验证硬件标识是否匹配
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="storedHardwareId">存储的硬件标识</param>
|
|||
|
|
/// <returns>如果匹配返回true,否则返回false</returns>
|
|||
|
|
public static bool ValidateHardwareId(string storedHardwareId)
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(storedHardwareId))
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string currentHardwareId = GetHardwareId();
|
|||
|
|
return string.Equals(currentHardwareId, storedHardwareId, StringComparison.OrdinalIgnoreCase);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|