vb.net 从 PC 获取唯一的硬件 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43408464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get unique Hardware id from PC
提问by ram nainar
I tried getting Hard disk and Motherboard Serial Numberfrom my PC. It works good in Windows 7 and Above
我尝试从我的 PC获取硬盘和主板序列号。它在 Windows 7 及更高版本中运行良好
But same code in CMD does not working for Windows XP. It Shows O.E.M to be filled or Returns Nothing
但是 CMD 中的相同代码不适用于 Windows XP。它显示 OEM 需要填充或不返回任何内容
wmic diskdrive get name,serialnumber,model // This is cmd to get serial num
In Windows XP It Returns Error for serialnumber
在 Windows XP 中,它返回序列号错误
wmic baseboard get product,Manufacturer,version,serialnumber // This is cmd to get MotherBoard serialnumber
In Windows XP and Win 8 it returnsin serialnumber Error like "To be filled by O.E.M"
在 Windows XP 和 Win 8 中,它返回序列号错误,如“由 OEM 填写”
Am looking for Best Pc Unique Id , which can return id or serialnumber for any OS and should be unique..
我正在寻找 Best Pc Unique Id ,它可以返回任何操作系统的 id 或序列号,并且应该是唯一的..
Please help me
请帮我
Thank You.
谢谢你。
回答by Mederic
I found a little project online where they get:
我在网上找到了一个小项目,他们在那里得到:
- Processor ID
- Motherboard ID
- Volume serial ID
- Mac address ID
- 处理器 ID
- 主板编号
- 卷序列号
- Mac 地址 ID
They then hash it through MD5but it is depreciated now so best bet is do the same and hash it through Sha512
然后他们通过MD5对其进行哈希处理,但现在它已贬值,因此最好的办法是执行相同操作并通过Sha512对其进行哈希处理
First you'll need to import and reference if not done automatically:
首先,如果没有自动完成,您需要导入和引用:
Imports System.Management
Imports System.Security.Cryptography
Imports System.Text
Then the function get HWID (HardwareID)
然后函数获取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
Function to generate Hash:
生成哈希函数:
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
And finally Class to get the information:
最后 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
I reviewed the code of this project
我查看了这个项目的代码
Hope this can help you and please remember add the reference to Management
希望这可以帮助您,请记住添加对管理的引用
More Information:
更多信息:
Most common IDs used for HWID are: CPU IDand MAC address based hardware IDand Hard Drive serial number
用于 HWID 的最常见 ID 是:基于CPU ID和MAC 地址的硬件 ID和硬盘驱动器序列号
HWID are not recommended as a licencing system as it isn't accurate and not practical if the user changes computer or formats the drives etc. It is more recommended to use a certificate system or more complex authentification.
不推荐 HWID 作为许可系统,因为如果用户更换计算机或格式化驱动器等,它不准确且不实用。更推荐使用证书系统或更复杂的身份验证。
回答by Muhammad Ali
Modifiedcode of @Mederic:
All digestedin just one function
Not tried yet but will work fine.
Use it like below:
@Mederic 的修改代码:
全部消化在一个函数中
尚未尝试,但可以正常工作。
像下面这样使用它:
msgbox("Your ID is = " & Get_HWID())
And the functionto generate ID:
以及生成ID的函数:
Public Function Get_HWID() As String
Dim strMotherBoardID As String = Nothing
Dim query As New Management.SelectQuery("Win32_BaseBoard")
Dim search As New Management.ManagementObjectSearcher(query)
Dim info As Management.ManagementObject
For Each info In search.Get()
strMotherBoardID = info("SerialNumber").ToString()
Next
Dim disk As Management.ManagementObject = New Management.ManagementObject(String.Format("win32_logicaldisk.deviceid=""{0}:""", "C"))
disk.Get()
Dim mc As Management.ManagementClass = New Management.ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As Management.ManagementObjectCollection = mc.GetInstances()
Dim MACAddress As String = String.Empty
For Each mo As Management.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
Dim strProcessorId As String = String.Empty
Dim GHHKJGK As New Management.SelectQuery("Win32_processor")
Dim YUGYUKJH As New Management.ManagementObjectSearcher(GHHKJGK)
Dim fghfgh As Management.ManagementObject
For Each fghfgh In YUGYUKJH.Get()
strProcessorId = fghfgh("processorId").ToString()
Next
Dim sha512 As Security.Cryptography.SHA512 = Security.Cryptography.SHA512Managed.Create()
Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(strProcessorId & disk("VolumeSerialNumber").ToString() & strMotherBoardID & MACAddress)
Dim hash As Byte() = sha512.ComputeHash(bytes)
Dim stringBuilder As New System.Text.StringBuilder()
For i As Integer = 0 To hash.Length - 1
stringBuilder.Append(hash(i).ToString("X2"))
Next
Return stringBuilder.ToString()
End Function

