从PC获取唯一的硬件ID

时间:2017-04-14 09:03:38

标签: vb.net cmd command-prompt vb.net-2010

我尝试从我的电脑上获取硬盘和主板序列号。它在Windows 7及以上版本中运行良好

但是CMD中的相同代码不适用于Windows XP。它显示要填充的O.E.M或返回任何内容

wmic diskdrive get name,serialnumber,model // This is cmd to get serial num

在Windows XP中,它返回序列号的错误

wmic baseboard get product,Manufacturer,version,serialnumber // This is cmd to get MotherBoard serialnumber

在Windows XP和Win 8中,在serialnumber中返回错误,如" 由O.E.M填写"

我正在寻找Best Pc Unique Id,它可以为任何操作系统返回id或serialnumber,并且应该是唯一的..

请帮帮我

谢谢。

2 个答案:

答案 0 :(得分:4)

我在网上发现了一个小项目:

  • 处理器ID
  • 主板ID
  • 卷序列号
  • Mac地址ID

然后他们通过 MD5 对其进行哈希处理,但现在已经过折旧,所以最好的选择是做同样的事情并通过 Sha512

进行哈希处理

首先,如果没有自动完成,您将需要导入和引用:

Imports System.Management
Imports System.Security.Cryptography
Imports System.Text

然后函数获取HWID(HardwareID)

Public Function Get_HWID() As String
    'Information Handler
    Dim hw As New clsComputerInfo
    'Decalre variables
    Dim hdd, cpu, mb, mac As String
    'Get all the values
    cpu = hw.GetProcessorId()
    hdd = hw.GetVolumeSerial("C")
    mb = hw.GetMotherBoardID()
    mac = hw.GetMACAddress()
    'Generate the hash
    Dim hwid As String = GenerateSHA512String(cpu & hdd & mb & mac)
    Return hwid
End Function

生成哈希的函数:

Public Shared Function GenerateSHA512String(ByVal inputString) As String
    Dim sha512 As SHA512 = SHA512Managed.Create()
    Dim bytes As Byte() = Encoding.UTF8.GetBytes(inputString)
    Dim hash As Byte() = sha512.ComputeHash(bytes)
    Dim stringBuilder As New StringBuilder()
    For i As Integer = 0 To hash.Length - 1
        stringBuilder.Append(hash(i).ToString("X2"))
    Next
    Return stringBuilder.ToString()
End Function

最后是Class来获取信息:

Public Class clsComputerInfo
    Friend Function GetProcessorId() As String
        Dim strProcessorId As String = String.Empty
        Dim query As New SelectQuery("Win32_processor")
        Dim search As New ManagementObjectSearcher(query)
        Dim info As ManagementObject
        For Each info In search.Get()
            strProcessorId = info("processorId").ToString()
        Next
        Return strProcessorId
    End Function
    Friend Function GetMACAddress() As String
        Dim mc As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
        Dim moc As ManagementObjectCollection = mc.GetInstances()
        Dim MACAddress As String = String.Empty
        For Each mo As ManagementObject In moc
            If (MACAddress.Equals(String.Empty)) Then
                If CBool(mo("IPEnabled")) Then MACAddress = mo("MacAddress").ToString()
                mo.Dispose()
            End If
            MACAddress = MACAddress.Replace(":", String.Empty)
        Next
        Return MACAddress
    End Function
    Friend Function GetVolumeSerial(Optional ByVal strDriveLetter As String = "C") As String
        Dim disk As ManagementObject = New ManagementObject(String.Format("win32_logicaldisk.deviceid=""{0}:""", strDriveLetter))
        disk.Get()
        Return disk("VolumeSerialNumber").ToString()
    End Function
    Friend Function GetMotherBoardID() As String
        Dim strMotherBoardID As String = String.Empty
        Dim query As New SelectQuery("Win32_BaseBoard")
        Dim search As New ManagementObjectSearcher(query)
        Dim info As ManagementObject
        For Each info In search.Get()
            strMotherBoardID = info("SerialNumber").ToString()
        Next
        Return strMotherBoardID
    End Function
End Class

我查看了this project

的代码

希望这可以帮助您,请记住添加对管理

的引用

更多信息:

用于HWID的最常见ID包括: CPU ID 基于MAC地址的硬件ID 硬盘驱动器序列号

不建议将HWID用作许可系​​统,因为如果用户更换计算机或格式化驱动器等,则不准确且不实用。建议使用证书系统或更复杂的身份验证。

答案 1 :(得分:1)

记住 Mac地址卷序列可以更改。因此,硬件将发生不可预测的变化。
因此,请使用处理器主板 ID,以使硬件ID 永不更改。
因此,在此基础上,您可以如下修改@Mederic代码:

'Get the MotherBoard ID
    Function GetMotherBoardID() As String
        Dim query As New SelectQuery("Win32_BaseBoard")
        Dim search As New ManagementObjectSearcher(query)
        For Each info As ManagementObject In search.Get()
            Return info("SerialNumber").ToString()
        Next
    End Function

'Get the Processor ID 
    Function GetProcessorId() As String
        Dim query As New SelectQuery("Win32_processor")
        Dim search As New ManagementObjectSearcher(query)
        Dim info As ManagementObject
        For Each info In search.Get()
            Return info("processorId").ToString()
        Next
    End Function

' Generate Hash  
    Function GenerateHash(ByVal input As String) As String
        Dim hash = New SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(input))
        Return String.Concat(hash.[Select](Function(b) b.ToString("x2")))
    End Function


'Now display Hardware ID
MessageBox.Show(GenerateHash(GetMotherBoardID() & GetProcessorId()))

源代码:Code fileBinary executable

处理器ID 在所有计算机上不是唯一的,但保持不变。 refernce
主板ID reference