C# 为什么我的ID会随着internet连接而改变?

C# 为什么我的ID会随着internet连接而改变?,c#,windows,winapi,uniqueidentifier,C#,Windows,Winapi,Uniqueidentifier,我正在为系统生成一个唯一的硬件ID。问题是,连接internet时生成的ID不同,断开internet时生成的ID也不同 这是我正在使用的代码: private static string fingerPrint = string.Empty; public static string GetUniqueID() { if (string.IsNullOrEmpty(fingerPrint)) { fingerPri

我正在为系统生成一个唯一的硬件ID。问题是,连接internet时生成的ID不同,断开internet时生成的ID也不同

这是我正在使用的代码:

    private static string fingerPrint = string.Empty;
    public static string GetUniqueID()
    {
        if (string.IsNullOrEmpty(fingerPrint))
        {
            fingerPrint = GetHash("CPU " + cpuId() + "\nBIOS " + 
                biosId() + "\nBASE " + baseId()
                + "\nVIDEO " + videoId() +"\nMAC "+ macId()
            );
        }
        return 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;
    }

    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 == "")
                {
                    try
                    {
                        result = mo[wmiProperty].ToString();
                        break;
                    }
                    catch
                    {
                    }
                }
            }
        }
        return result;
    }

    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)
        {
            if (result == "")
            {
                try
                {
                    result = mo[wmiProperty].ToString();
                    break;
                }
                catch
                {
                }
            }
        }
        return result;
    }
    private static string cpuId()
    {
        string retVal = identifier("Win32_Processor", "UniqueId");
        if (retVal == "")
        {
            retVal = identifier("Win32_Processor", "ProcessorId");
            if (retVal == "")
            {
                retVal = identifier("Win32_Processor", "Name");
                if (retVal == "") 
                {
                    retVal = identifier("Win32_Processor", "Manufacturer");
                }
                retVal += identifier("Win32_Processor", "MaxClockSpeed");
            }
        }
        return retVal;
    }

    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");
    }

    private static string diskId()
    {
        return identifier("Win32_DiskDrive", "Model")
        + identifier("Win32_DiskDrive", "Manufacturer")
        + identifier("Win32_DiskDrive", "Signature")
        + identifier("Win32_DiskDrive", "TotalHeads");
    }

    private static string baseId()
    {
        return identifier("Win32_BaseBoard", "Model")
        + identifier("Win32_BaseBoard", "Manufacturer")
        + identifier("Win32_BaseBoard", "Name")
        + identifier("Win32_BaseBoard", "SerialNumber");
    }

    private static string videoId()
    {
        return identifier("Win32_VideoController", "DriverVersion")
        + identifier("Win32_VideoController", "Name");
    }

    private static string macId()
    {
        return identifier("Win32_NetworkAdapterConfiguration", 
            "MACAddress", "IPEnabled");
    }
private static string fingerPrint=string.Empty;
公共静态字符串GetUniqueID()
{
if(string.IsNullOrEmpty(指纹))
{
指纹=GetHash(“CPU”+cpuId()+“\nBIOS”+
biosId()+“\n基”+baseId()
+“\nVIDEO”+videoId()+“\nMAC”+macId()
);
}
返回指纹;
}
私有静态字符串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=
新系统。管理。管理类(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=
新系统。管理。管理类(wmiClass);
System.Management.ManagementObjectCollection moc=mc.GetInstances();
foreach(主运行中心中的System.Management.ManagementObject mo)
{
如果(结果==“”)
{
尝试
{
结果=mo[wmiProperty].ToString();
打破
}
抓住
{
}
}
}
返回结果;
}
私有静态字符串cpuId()
{
string retVal=标识符(“Win32_处理器”、“唯一标识”);
如果(retVal==“”)
{
retVal=标识符(“Win32_处理器”、“处理器ID”);
如果(retVal==“”)
{
retVal=标识符(“Win32_处理器”、“名称”);
如果(retVal==“”)
{
retVal=标识符(“Win32_处理器”、“制造商”);
}
retVal+=标识符(“Win32_处理器”、“MaxClockSpeed”);
}
}
返回返回;
}
私有静态字符串biosId()
{
返回标识符(“Win32_BIOS”、“制造商”)
+标识符(“Win32_BIOS”、“SMBIOS版本”)
+标识符(“Win32_BIOS”、“IdentificationCode”)
+标识符(“Win32_BIOS”、“序列号”)
+标识符(“Win32_BIOS”、“发布日期”)
+标识符(“Win32_BIOS”、“版本”);
}
专用静态字符串diskId()
{
返回标识符(“Win32\U磁盘驱动器”、“型号”)
+标识符(“Win32\U磁盘驱动器”、“制造商”)
+标识符(“Win32\U磁盘驱动器”、“签名”)
+标识符(“Win32_DiskDrive”、“TotalHeads”);
}
私有静态字符串baseId()
{
返回标识符(“Win32_基板”、“模型”)
+标识符(“Win32_基板”、“制造商”)
+标识符(“Win32_基板”、“名称”)
+标识符(“Win32_基板”、“序列号”);
}
私有静态字符串videoId()
{
返回标识符(“Win32\U视频控制器”、“驱动程序版本”)
+标识符(“Win32_视频控制器”、“名称”);
}
私有静态字符串macId()
{
返回标识符(“Win32\U网络适配器配置”,
“MACAddress”、“IPEnabled”);
}

GetUniqueID()
是散列各种ID的函数。为什么在有和没有互联网的情况下生成的ID不同?

我认为这是因为当您断开连接时,您的网络接口被禁用,因此无法检索MAC地址。

您不能简单地使用设备的MAC ID,而不是这1000行复杂的逻辑。它将始终保持不变,或者是设备的Bios串行的设备串行密钥。即使没有网络,Bios密钥也将可用。

我想你选错了教程

由于以下两个原因,当您连接/断开连接时,MAC会发生变化。首先,大多数电脑都有一个以上的网卡,它们在WMI中的显示顺序尚未定义。其次,默认情况下,Windows 10有时会在每次连接到WiFi网络时出现

您正在使用的其余ID也不是更好

videoId不稳定,因为更新驱动程序时驱动程序版本会更新,顺便说一句,一些windows更新包括新版本的GPU驱动程序

diskId不稳定,因为用户插入USB闪存驱动器或外部设备
    static string GetUniqueID()
    {
        string result;
        string registry_path = "HKEY_CURRENT_USER\\Software\\MyApp";  // substitue your own app name here
        Object obj = Registry.GetValue(registry_path, "UniqueID", null);
        if ((obj == null) || !(obj is string))
        {
            result = Guid.NewGuid().ToString();
            Registry.SetValue(registry_path, "UniqueID", result);
        }
        else
        {
            result = (string)obj;
        }
        return result;
    }