C#中的硬件指纹类

时间:2016-10-01 15:19:30

标签: c# hardware fingerprint

我使用以下类为基本软件许可系统生成唯一的硬件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");
}
}

0 个答案:

没有答案