C#中的硬件指纹类

问题描述 投票:0回答:1

我正在使用以下类为基本软件许可系统生成唯一的硬件 ID。 问题是在某些情况下产生的指纹是不同的,无需任何硬件更改。

我还没有开发客户端调试系统来隔离导致问题的组件,所以我想问一下以下代码是否以及在什么条件下可以为同一硬件生成不同的ID。

我已经删除了网络适配器部分(当存在许多适配器时导致变化)和硬盘部分(连接 USB 驱动器时出现问题)。 但即使在软件更改和 Windows 更新之后,其余组件的 ID 也应该保持不变,不是吗?

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");
}
}
c# hardware fingerprint
1个回答
0
投票

您的计算机上可能有多张网卡或显卡,识别时请注意。 当您使用远程桌面时,Windows 会向您的硬件添加显卡!!! several network cards - several HDD

Remote Desktop Adaptor

        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
                {
                    if (wmiClass.EQ("Win32_DiskDrive") )
                    {
                        if (mo.Path.ToString().Contains("PHYSICALDRIVE0"))
                        {
                            result = mo[wmiProperty] != null ? mo[wmiProperty].ToString() : "";
                            break;
                        }
                    }
                    else if (wmiClass.EQ("Win32_VideoController"))
                    {
                        if (mo.Path.ToString().Contains("VideoController1"))
                        {
                            result = mo[wmiProperty] != null ? mo[wmiProperty].ToString() : "";
                            break;
                        }
                    }
                    else if (wmiClass.EQ("Win32_NetworkAdapterConfiguration"))
                    {
                            result = mo[wmiProperty] != null ? mo[wmiProperty].ToString() : "";
                        if(result!="")
                            break;
                    }
                    else
                    {
                        result = mo[wmiProperty] != null ? mo[wmiProperty].ToString() : "";
                        break;
                    }
                    
                }
                catch
                {
                }
            }
        }
        return result;
    }
© www.soinside.com 2019 - 2024. All rights reserved.