背景
因为朋友想做一个软件认证相关的东西,所以考虑到关于电脑唯一标识机器码的问题。关于机器码之前并没有真正实现过,也只是了解大概的原理。今天就做一下简单记录,以便以后使用。
机器码
机器码
指的是软件根据计算机的硬件信息,例如:CPU
、内存、主板序列号等,按照一定的算法生成的一串无规律的字符串,并且在不同计算机上生成的机器码是不一样的,因为每台计算机的硬件信息不一样。机器码
,也叫序列号
、认证码
、注册申请码
等。
代码实现
using System.Management;
using System.Runtime.InteropServices;
namespace HonourWorld.Core.Common
{
public sealed class MachineCode
{
private readonly static MachineCode machineCode=new MachineCode();
public static string GetMachineCodeString()
{
string machineCodeString = $"PC.{machineCode.GetCpuInfo()}.{machineCode.GetHDid()}.{machineCode.GetDiskSerialNumber()}.{machineCode.GetMacAddress()}.{machineCode.GetBiosSerialNumber()}";
return machineCodeString;
}
private MachineCode()
{
}
/// <summary>
/// 获取cpu序列号
/// </summary>
/// <returns> string </returns>
public string GetCpuInfo()
{
string? cpuInfo = "";
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using (ManagementClass cimobject = new ManagementClass("Win32_Processor"))
{
ManagementObjectCollection moc = cimobject.GetInstances();
foreach (ManagementObject mo in moc)
{
if (mo == null || mo.Properties == null)
{
continue;
}
var info = mo.Properties["ProcessorId"];
if (info!=null)
{
cpuInfo = info.Value.ToString();
}
mo?.Dispose();
}
}
}
}
catch (Exception)
{
throw;
}
return (cpuInfo??"").Trim();
}
/// <summary>
/// 获取硬盘ID
/// </summary>
/// <returns> string </returns>
public string GetHDid()
{
string HDid = "";
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using (ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive"))
{
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach (ManagementObject mo in moc1)
{
HDid = (string)mo.Properties["Model"].Value;
mo.Dispose();
}
}
}
}
catch (Exception)
{
throw;
}
return HDid.Trim();
}
/// <summary>
/// 获得硬盘序列号
/// </summary>
/// <returns></returns>
public string GetDiskSerialNumber()
{
string serialNumber = "";
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using (ManagementClass bios = new ManagementClass("Win32_PhysicalMedia"))
{
ManagementObjectCollection moc1 = bios.GetInstances();
foreach (ManagementObject mo in moc1)
{
serialNumber = (string)mo.Properties["SerialNumber"].Value;
mo.Dispose();
}
}
}
}
catch (Exception)
{
throw;
}
return serialNumber.Trim();
}
/// <summary>
/// 获取网卡硬件地址
/// </summary>
/// <returns> string </returns>
public string GetMacAddress()
{
string mocAddress = "";
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
{
ManagementObjectCollection moc2 = mc.GetInstances();
foreach (ManagementObject mo in moc2)
{
if ((bool)mo["IPEnabled"]&& mo["MacAddress"]!=null)
{
mocAddress = (string)mo["MacAddress"];
}
mo.Dispose();
}
}
}
}
catch (Exception)
{
throw;
}
return mocAddress.Trim();
}
/// <summary>
/// 获得主板序列号
/// </summary>
/// <returns></returns>
public string GetBiosSerialNumber()
{
string serialNumber = "";
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using (ManagementClass bios = new ManagementClass("Win32_BaseBoard"))
{
ManagementObjectCollection moc1 = bios.GetInstances();
foreach (ManagementObject mo in moc1)
{
serialNumber = (string)mo.Properties["SerialNumber"].Value;
mo.Dispose();
}
}
}
}
catch (Exception)
{
throw;
}
return serialNumber.Trim();
}
}
}
调用方式
private void btnIntroduction_Click(object sender, EventArgs e)
{
string machineCode = Honour.MachineCode.GetMachineCodeString();
machineCode = EncryptionSha.GetSha1HashString(machineCode);
machineCode = Regex.Replace(machineCode, @"\w{8}", "$0-", RegexOptions.IgnoreCase).TrimEnd('-');
var result = MessageBox.Show($"机器码:{machineCode}", "提示", MessageBoxButtons.OK);
}
机器码是使用CPU序列号
、硬盘ID
、硬盘序列号
、MAC地址
、主板序列号
进行拼接的字符串。由于电脑硬件信息拼接而成的无规则字符串,所以显得杂乱。为了让其看上去比较规范就对拼接的取散列信息,散列算法有很多种,例如:MD5
、SHA1
等。这里使用SHA1
来实现需求。
注:本方法只适用于windows系统。