真的有什么方法可以唯一标识任何计算机吗 [英] Is there really any way to uniquely identify any computer at all

查看:44
本文介绍了真的有什么方法可以唯一标识任何计算机吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在 stackoverflow 中有许多类似的问题,例如:

I know there are a number of similar questions in stackoverflow such as the followings:

……还有几十个,我都研究过了.

... and dozens more and I have studied them all.

问题是一些已接受的答案建议将 MAC 地址作为唯一标识符,这是完全不正确的.其他一些答案建议使用看起来更合乎逻辑的各种组件的组合.但是,在使用组合的情况下,应该考虑哪个组件自然不太可能频繁更改.几天前,我们为软件许可问题开发了一个密钥生成器,我们使用 CPUID 和 MAC 的组合来唯一地识别 Windows pc,直到实际测试我们认为我们的方法足够好.具有讽刺意味的是,当我们对其进行测试时,我们发现三台计算机使用我们的密钥生成器返回相同的 ID!

The problem is that some of the accepted answers have suggested MAC address as an unique identifier which is entirely incorrect. Some other answers have suggested to use a combination of various components which seems more logical. However, in case of using a combination it should be considered which component is naturally unlikely to be changed frequently. A few days ago we developed a key generator for a software licensing issue where we used the combination of CPUID and MAC to identify a windows pc uniquely and till practical testing we thought our approach was good enough. Ironically when we went testing it we found three computers returning the same id with our key generator!

那么,真的有任何方法可以唯一地识别任何计算机吗?现在我们只需要让我们的密钥生成器在 windows pc 上工作.由于我们的系统是在 .net 上开发的,因此使用 c# 的某种方式(如果可能的话)会很棒.

So, is there really any way to uniquely identify any computer at all? Right now we just need to make our key generator to work on windows pc. Some way (if possible at all) using c# would be great as our system is developed on .net.

更新:

抱歉造成了一些混乱和明显的误报.我们发现检索硬件信息的方法存在一些错误.主要是我想删除这个问题,因为现在我自己的困惑已经消失了,而且我确实相信两个或多个组件的组合足以识别一台计算机.但是,后来我决定保留它,因为我认为我应该澄清导致问题的原因,因为同样的事情将来可能会伤害其他人.

Sorry for creating some confusions and an apparently false alarm. We found out some incorrectness in our method of retrieving HW info. Primarily I thought of deleting this question as now my own confusion has gone and I do believe that a combination of two or more components is good enough to identify a computer. However, then I decided to keep it because I think I should clarify what was causing the problem as the same thing might hurt some other guy in future.

这是我们正在做的(不包括其他代码):

This is what we were doing (excluding other codes):

我们使用 getManagementInfo 函数来检索 MAC 和处理器 ID

We were using a getManagementInfo function to retrieve MAC and Processor ID

private String getManagementInfo(String StrKey_String, String strIndex)
    {
        String strHwInfo = null;
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + StrKey_String);
            foreach (ManagementObject share in searcher.Get())
            {
                strHwInfo += share[strIndex];
            }
        }
        catch (Exception ex)
        {
            // show some error message
        }
        return strHwInfo;
    } 

然后我们在需要的地方使用该函数来检索 MAC 地址

Then where needed we used that function to retrieve MAC Address

string strMAC = getManagementInfo("Win32_NetworkAdapterConfiguration", "MacAddress");

并检索处理器ID

string strProcessorId = getManagementInfo("Win32_Processor", "ProcessorId");

此时strMAC如果有多个MAC地址,就会包含多个.为了只取一个,我们只取了前 17 个字符(12 个 MAC 数字和中间的 5 个冒号).

At this point, strMAC would contain more than one MAC address if there are more than one. To take only one we just took the first 17 characters (12 MAC digits and 5 colons in between).

strMAC = strMAC.Length > 17 ? strMAC.Remove(17) : strMAC;

这就是我们犯错的地方.因为 getManagementInfo("Win32_NetworkAdapterConfiguration", "MacAddress") 返回了一些真正在使用的额外 MAC 地址.例如,当我们通过 getmac 命令在命令提示符中搜索 MAC 地址时,它会为每台电脑显示一个或两个不同的 MAC 地址.但是 getManagementInfo("Win32_NetworkAdapterConfiguration", "MacAddress") 返回了四到五个 MAC 地址,其中一些对于所有计算机都是相同的.由于我们只是获取了函数返回的第一个 MAC 地址,而不是检查其他任何内容,因此在 strMAC 中偶然获取了相同的 MAC 地址.

This is where we made the mistake. Because getManagementInfo("Win32_NetworkAdapterConfiguration", "MacAddress") was returning a number of extra MAC addresses that were really in use. For example, when we searched for MAC addresses in the command prompt by getmac command then it showed one or two MAC addresses for each pc which were all different. But getManagementInfo("Win32_NetworkAdapterConfiguration", "MacAddress") returned four to five MAC addresses some of which were identical for all computers. As we just took the first MAC address that our function returned instead of checking anything else, the identical MAC addresses were taken in strMAC incidently.

以下代码由 Sowkot Osman 通过仅返回第一个活动/启用的 MAC 地址来解决问题:

The following code by Sowkot Osman does the trick by returning only the first active/ enabled MAC address:

private static string macId()
    {
        return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled");
    }

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

但是,对于相同的处理器 ID 问题,我是完全正确的.当我们将 wmic cpu get ProcessorId 命令放在它们的命令提示符中时,所有三个都返回相同的处理器 ID.

However, I was absolutely right about the identical Processor ID issue. All three returned the same Processor ID when we put wmic cpu get ProcessorId command in their command prompts.

现在我们决定使用主板序列号代替处理器 ID 与 MAC 地址进行组合.我认为我们的目的将通过这种方式实现,如果在某些情况下不能实现,那么在少数情况下我们应该放手.

Now we have decided to use Motherboard serial number instead of Processor ID to make a combination with MAC address. I think our purpose will be served with this way and if it doesn't in some cases then we should let it go in those few cases.

推荐答案

获得全局唯一 ID 的事实是,如果您重新设置系统,则只有 MAC 地址是不会更改的 ID.如果您要为特定产品生成密钥,最好的方法是为产品分配唯一 ID 并将产品 ID 与 MAC 地址组合.希望能帮助到你.

The fact in getting a globally unique ID is, only MAC address is the ID that will not change if you set up your system all over. IF you are generating a key for a specific product, the best way to do it is assigning unique IDs for products and combining the product ID with MAC address. Hope it helps.

这篇关于真的有什么方法可以唯一标识任何计算机吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆