C# C语言中的硬件指纹类#
我使用下面的类为基本软件许可系统生成一个唯一的硬件ID。 问题是,在某些情况下,在没有任何硬件更改的情况下,生成的指纹是不同的 我还没有开发一个客户端调试系统来隔离导致问题的组件,所以我想问一下下面的代码是否以及在什么条件下可以为同一硬件生成不同的ID 我已经删除了网络适配器部分(当存在许多适配器时会引起变化)和硬盘部分(连接usb驱动器时会出现问题)。 但是,即使在软件更改和windows更新之后,其余组件的ID也应该保持不变,不是吗C# C语言中的硬件指纹类#,c#,hardware,fingerprint,C#,Hardware,Fingerprint,我使用下面的类为基本软件许可系统生成一个唯一的硬件ID。 问题是,在某些情况下,在没有任何硬件更改的情况下,生成的指纹是不同的 我还没有开发一个客户端调试系统来隔离导致问题的组件,所以我想问一下下面的代码是否以及在什么条件下可以为同一硬件生成不同的ID 我已经删除了网络适配器部分(当存在许多适配器时会引起变化)和硬盘部分(连接usb驱动器时会出现问题)。 但是,即使在软件更改和windows更新之后,其余组件的ID也应该保持不变,不是吗 public static class FingerPri
public static class FingerPrint
{
private static string fingerPrint = string.Empty;
//--------------------------------------
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
//--------------------------------------
public static string Value()
{
string cpu = cpuId();
string bios = biosId();
string bas = baseId();
string video = videoId();
if (string.IsNullOrEmpty(fingerPrint))
{
fingerPrint = GetHash("CPU >> " + cpu + "\nBIOS >> " +
bios + "\nBASE >> " + bas
+ "\nVIDEO >> " + video
+ "\n some_salt");
}
return Base64Encode(fingerPrint);
}
//--------------------------------------
private static string GetHash(string s)
{
MD5 sec = new MD5CryptoServiceProvider();
ASCIIEncoding enc = new ASCIIEncoding();
byte[] bt = enc.GetBytes(s);
return GetHexString(sec.ComputeHash(bt));
}
//--------------------------------------
private static string GetHexString(byte[] bt)
{
string s = string.Empty;
for (int i = 0; i < bt.Length; i++)
{
byte b = bt[i];
int n, n1, n2;
n = (int)b;
n1 = n & 15;
n2 = (n >> 4) & 15;
if (n2 > 9)
s += ((char)(n2 - 10 + (int)'A')).ToString();
else
s += n2.ToString();
if (n1 > 9)
s += ((char)(n1 - 10 + (int)'A')).ToString();
else
s += n1.ToString();
if ((i + 1) != bt.Length && (i + 1) % 2 == 0) s += "-";
}
return s;
}
//--------------------------------------
//Return a hardware identifier
private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
{
string result = "";
System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
System.Management.ManagementObjectCollection moc = mc.GetInstances();
foreach (System.Management.ManagementObject mo in moc)
{
if (mo[wmiMustBeTrue].ToString() == "True")
{
//Only get the first one
if (result == "")
{
result = mo[wmiProperty].ToString();
break;
}
}
}
return result;
}
//--------------------------------------
//Return a hardware identifier
private static string identifier(string wmiClass, string wmiProperty)
{
string result = "";
System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
System.Management.ManagementObjectCollection moc = mc.GetInstances();
foreach (System.Management.ManagementObject mo in moc)
{
//Only get the first one
if (result == "")
{
result = mo[wmiProperty].ToString();
break;
}
}
return result;
}
//--------------------------------------
private static string cpuId()
{
//Uses first CPU identifier available in order of preference
//Don't get all identifiers, as it is very time consuming
string retVal = identifier("Win32_Processor", "UniqueId");
if (retVal == "") //If no UniqueID, use ProcessorID
{
retVal = identifier("Win32_Processor", "ProcessorId");
if (retVal == "") //If no ProcessorId, use Name
{
retVal = identifier("Win32_Processor", "Name");
if (retVal == "") //If no Name, use Manufacturer
{
retVal = identifier("Win32_Processor", "Manufacturer");
}
}
}
return retVal;
}
//--------------------------------------
//BIOS Identifier
private static string biosId()
{
return identifier("Win32_BIOS", "Manufacturer")
+ identifier("Win32_BIOS", "SMBIOSBIOSVersion")
+ identifier("Win32_BIOS", "IdentificationCode")
+ identifier("Win32_BIOS", "SerialNumber")
+ identifier("Win32_BIOS", "ReleaseDate")
+ identifier("Win32_BIOS", "Version");
}
//--------------------------------------
//Motherboard ID
private static string baseId()
{
return identifier("Win32_BaseBoard", "Model")
+ identifier("Win32_BaseBoard", "Manufacturer")
+ identifier("Win32_BaseBoard", "Name")
+ identifier("Win32_BaseBoard", "SerialNumber");
}
//--------------------------------------
//Primary video controller ID
private static string videoId()
{
return identifier("Win32_VideoController", "Name");
}
}
公共静态类指纹
{
私有静态字符串指纹=string.Empty;
//--------------------------------------
公共静态字符串base64编码(字符串明文)
{
var plainTextBytes=System.Text.Encoding.UTF8.GetBytes(明文);
返回System.Convert.ToBase64String(明文字节);
}
//--------------------------------------
公共静态字符串值()
{
字符串cpu=cpuId();
字符串bios=biosId();
字符串bas=baseId();
字符串video=videoId();
if(string.IsNullOrEmpty(指纹))
{
指纹=GetHash(“CPU>>”+CPU+“\nBIOS>>”+
bios+“\n基础>>”+bas
+“\n视频>>”+视频
+“\n一些盐”);
}
返回base64编码(指纹);
}
//--------------------------------------
私有静态字符串GetHash(字符串s)
{
MD5 sec=新的MD5CryptoServiceProvider();
ascienceoding enc=新的ascienceoding();
字节[]bt=enc.GetBytes;
返回GetHexString(第二节ComputeHash(bt));
}
//--------------------------------------
私有静态字符串GetHexString(字节[]bt)
{
string s=string.Empty;
对于(int i=0;i>4)和15;
如果(n2>9)
s+=((char)(n2-10+(int)'A')).ToString();
其他的
s+=n2.ToString();
如果(n1>9)
s+=((char)(n1-10+(int)'A')).ToString();
其他的
s+=n1.ToString();
如果((i+1)!=bt.长度和(i+1)%2==0)s+=“-”;
}
返回s;
}
//--------------------------------------
//返回硬件标识符
私有静态字符串标识符(字符串wmiClass、字符串wmiProperty、字符串wmiMustBeTrue)
{
字符串结果=”;
System.Management.ManagementClass mc=新的System.Management.ManagementClass(wmiClass);
System.Management.ManagementObjectCollection moc=mc.GetInstances();
foreach(主运行中心中的System.Management.ManagementObject mo)
{
if(mo[wmiMustBeTrue].ToString()=“True”)
{
//只有第一个
如果(结果==“”)
{
结果=mo[wmiProperty].ToString();
打破
}
}
}
返回结果;
}
//--------------------------------------
//返回硬件标识符
专用静态字符串标识符(字符串wmiClass、字符串wmiProperty)
{
字符串结果=”;
System.Management.ManagementClass mc=新的System.Management.ManagementClass(wmiClass);
System.Management.ManagementObjectCollection moc=mc.GetInstances();
foreach(主运行中心中的System.Management.ManagementObject mo)
{
//只有第一个
如果(结果==“”)
{
结果=mo[wmiProperty].ToString();
打破
}
}
返回结果;
}
//--------------------------------------
私有静态字符串cpuId()
{
//按优先顺序使用第一个可用的CPU标识符
//不要获取所有标识符,因为这非常耗时
string retVal=标识符(“Win32_处理器”、“唯一标识”);
if(retVal==“”)//如果没有唯一ID,请使用ProcessorID
{
retVal=标识符(“Win32_处理器”、“处理器ID”);
if(retVal==“”)//如果没有ProcessorId,请使用Name
{
retVal=标识符(“Win32_处理器”、“名称”);
if(retVal==“”)//如果没有名称,请使用制造商
{
retVal=标识符(“Win32_处理器”、“制造商”);
}
}
}
返回返回;
}
//--------------------------------------
//BIOS标识符
私有静态字符串biosId()
{
返回标识符(“Win32_BIOS”、“制造商”)
+标识符(“Win32_BIOS”、“SMBIOS版本”)
+标识符(“Win32_BIOS”、“IdentificationCode”)
+标识符(“Win32_BIOS”、“序列号”)
+标识符(“Win32_BIOS”、“发布日期”)
+标识符(“Win32_BIOS”、“版本”);
}
//--------------------------------------
//主板ID
私有静态字符串baseId()
{
返回标识符(“Win32_基板”、“模型”)
+标识符(“Win32_基板”、“制造商”)
+标识符(“Win32_基板”、“名称”)
+标识符(“Win32_基板”、“序列号”);
}
//--------------------------------------
//主视频控制器ID
私有静态字符串videoId()
{
返回标识符(“Win32_视频控制器”、“名称”);
}
}
您似乎在询问软件更新是否会更改BIOS版本号,这是一个“我的计算机如何工作?”问题,而不是编程问题。无论如何,BIOS是软件(固件),即使硬件相同,BIOS(固件)更新也肯定会更改报告的BIOS版本。其他事项,如CPU的“型号”或视频卡的“名称”,可能取决于驱动程序版本。。。旧驱动程序无法识别较新的硬件,将返回通用字符串,而新驱动程序将返回准确的字符串。通用/安全模式驱动程序可能返回PCI即插即用描述符中的字符串,而不是供应商的命名方案。@BenVoi