用C++写一个获取电脑唯一机器码的方法
时间: 2023-10-25 07:08:56 浏览: 187
好的,我们可以使用Windows API来获取计算机的硬件信息,然后通过哈希算法生成机器码。下面是一个简单的示例代码:
```c++
#include <iostream>
#include <Windows.h>
#include <string>
#include <sstream>
#include <iomanip>
// 哈希算法
std::string MD5Hash(const std::string& str)
{
HCRYPTPROV hProv = NULL;
HCRYPTHASH hHash = NULL;
BYTE rgbHash[16];
DWORD cbHash = sizeof(rgbHash);
std::string hash;
if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
{
if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
{
if (CryptHashData(hHash, (BYTE*)str.c_str(), str.length(), 0))
{
if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0))
{
std::stringstream ss;
for (int i = 0; i < cbHash; i++)
{
ss << std::hex << std::setw(2) << std::setfill('0') << (int)rgbHash[i];
}
hash = ss.str();
}
}
}
}
if (hHash)
{
CryptDestroyHash(hHash);
}
if (hProv)
{
CryptReleaseContext(hProv, 0);
}
return hash;
}
// 获取CPU序列号
std::string GetCPUId()
{
std::string cpuId;
__asm
{
mov eax, 0
cpuid
mov cpuId, ebx
mov cpuId, edx
mov cpuId, ecx
}
return cpuId;
}
// 获取硬盘序列号
std::string GetDiskId()
{
char szDrive[256];
DWORD dwSerialNumber = 0;
DWORD dwMaxComponentLen = 0;
DWORD dwFileSystemFlags = 0;
char szFileSystem[256];
GetLogicalDriveStringsA(sizeof(szDrive) - 1, szDrive);
char* p = szDrive;
while (*p)
{
if (GetDriveTypeA(p) == DRIVE_FIXED)
{
if (GetVolumeInformationA(p, NULL, 0, &dwSerialNumber, &dwMaxComponentLen, &dwFileSystemFlags, szFileSystem, sizeof(szFileSystem)))
{
std::stringstream ss;
ss << std::hex << dwSerialNumber;
return ss.str();
}
}
p += strlen(p) + 1;
}
return "";
}
// 获取MAC地址
std::string GetMACAddress()
{
PIP_ADAPTER_INFO pAdapterInfo;
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL)
{
return "";
}
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
{
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen);
if (pAdapterInfo == NULL)
{
return "";
}
}
std::string macAddress;
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == NO_ERROR)
{
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (pAdapter)
{
if (pAdapter->Type == MIB_IF_TYPE_ETHERNET)
{
std::stringstream ss;
for (int i = 0; i < pAdapter->AddressLength; i++)
{
ss << std::hex << std::setw(2) << std::setfill('0') << (int)pAdapter->Address[i];
}
macAddress = ss.str();
break;
}
pAdapter = pAdapter->Next;
}
}
free(pAdapterInfo);
return macAddress;
}
// 获取机器码
std::string GetMachineCode()
{
std::string machineCode;
std::string cpuId = GetCPUId();
if (!cpuId.empty())
{
machineCode += cpuId;
}
std::string diskId = GetDiskId();
if (!diskId.empty())
{
machineCode += diskId;
}
std::string macAddress = GetMACAddress();
if (!macAddress.empty())
{
machineCode += macAddress;
}
if (!machineCode.empty())
{
machineCode = MD5Hash(machineCode);
}
return machineCode;
}
int main()
{
std::string machineCode = GetMachineCode();
if (!machineCode.empty())
{
std::cout << "机器码:" << machineCode << std::endl;
}
else
{
std::cout << "获取机器码失败!" << std::endl;
}
return 0;
}
```
这个示例代码中,我们使用了Windows API中的CryptAcquireContext、CryptCreateHash、CryptHashData、CryptGetHashParam、CryptDestroyHash、CryptReleaseContext、GetLogicalDriveStringsA、GetDriveTypeA、GetVolumeInformationA、GetAdaptersInfo等函数来获取计算机的硬件信息和生成哈希值。在获取CPU序列号时,我们使用了__asm汇编语句来调用CPUID指令获取信息。在获取MAC地址时,我们使用了WinSock库中的GetAdaptersInfo函数。最终,将获取到的CPU序列号、硬盘序列号、MAC地址等信息拼接在一起,并进行哈希计算,生成唯一的机器码。