从C/C++中的Windows PC获取独特的硬件/软件签名,可以通过以下方法实现:
硬件签名是基于计算机的硬件信息生成的,可以通过以下方法获取:
软件签名是基于软件的版本信息生成的,可以通过以下方法获取:
将获取到的硬件和软件信息组合起来,可以使用哈希算法如SHA-256生成一个唯一的签名。
示例代码:
#include<Windows.h>
#include <Wbemidl.h>
#include <comdef.h>
#include<iostream>
#include <sstream>
#include <iomanip>
#include<openssl/sha.h>
// 获取CPU ID
std::string GetCPUID()
{
std::stringstream ss;
int CPUInfo[4] = { -1 };
unsigned int nExIds, i = 0;
char CPUBrandString[0x40];
__cpuid(CPUInfo, 0x80000000);
nExIds = CPUInfo[0];
for (i = 0x80000000; i <= nExIds; ++i)
{
__cpuid(CPUInfo, i);
if (i == 0x80000002)
{
memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
ss<< CPUBrandString;
}
else if (i == 0x80000003)
{
memcpy(CPUBrandString + 16, CPUInfo, sizeof(CPUInfo));
ss<< CPUBrandString;
}
else if (i == 0x80000004)
{
memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
ss<< CPUBrandString;
}
}
return ss.str();
}
// 获取硬盘序列号
std::string GetDiskSerialNumber()
{
std::stringstream ss;
HRESULT hres;
VARIANT vtProp;
CoInitialize(NULL);
hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc);
if (FAILED(hres))
return "";
hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc);
if (FAILED(hres))
return "";
hres = CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
if (FAILED(hres))
return "";
hres = pSvc->ExecQuery(_bstr_t("WQL"), _bstr_t("SELECT * FROM Win32_DiskDrive"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
if (FAILED(hres))
return "";
while (pEnumerator)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (FAILED(hres))
break;
hres = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
if (FAILED(hres))
continue;
if (vtProp.vt == VT_BSTR)
{
ss << _bstr_t(vtProp.bstrVal);
break;
}
VariantClear(&vtProp);
}
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();
return ss.str();
}
// 生成签名
std::string GenerateSignature()
{
unsigned char hash[SHA256_DIGEST_LENGTH];
std::stringstream ss;
// 获取CPU ID
std::string cpuid = GetCPUID();
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, cpuid.c_str(), cpuid.size());
// 获取硬盘序列号
std::string diskserial = GetDiskSerialNumber
高校公开课
高校公开课
serverless days
Elastic 中国开发者大会
高校公开课
高校开发者
Elastic 中国开发者大会
DBTalk
Techo Day
腾讯技术开放日
领取专属 10元无门槛券
手把手带您无忧上云