为什么我的ID会随着互联网连接而变化?

时间:2018-02-25 13:04:57

标签: c# windows winapi uniqueidentifier

我正在关注this tutorial以为系统生成唯一的硬件ID。问题是互联网连接时生成的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");
    }

GetUniqueID()是散列各种ID的函数。为什么使用和不使用互联网时生成的ID会有所不同?

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

代替这1000多行复杂逻辑,你不能简单地使用设备的MAC id。它将始终保持相同或设备的串行密钥是Bios序列。即使没有网络,也可以使用Bios密钥。

答案 2 :(得分:1)

我的团队曾尝试构建类似的代码,以便在不使用持久存储的情况下根据计算机设置生成唯一ID。 E.g Hash(hostname, username, domain name, mac address, etc...)。当时看起来像一个好主意,但结果证明本身不可靠,因为在返回这些值时应该保持一致的一些API并非总是如此。

更简单,更可靠的方法是生成GUID并将其持久保存到注册表(或磁盘上的文件,甚至Windows Credential Store)。

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

它也比制作WMI呼叫更快。

答案 3 :(得分:0)

我认为你选错了教程。

由于两个原因,当您连接/断开连接时MAC会发生变化。首先,大多数PC具有多于1个网卡,并且它们在WMI中出现的顺序是未定义的。其次,默认情况下,每次连接到WiFi网络时,Windows 10有时会randomizes WiFi MAC address

您正在使用的其他ID并不是更好。

videoId不稳定,因为更新驱动程序时驱动程序版本会更新,BTW某些Windows更新包含新版本的GPU驱动程序

diskId不稳定,因为用户插入USB闪存驱动器或外部硬盘驱动器。

cpuId是不可靠的,因为它只包含特定于CPU模型的信息,即对于主流CPU,世界上有数百万人具有相同的cpuId。

相关问题