Skip to main content
 首页 » 编程设计

加密授权验证学习之一 ——获取计算机CPUID序列号

2022年07月19日263lidabo

最近项目中需要对CPUID、硬盘序列号、网卡物理地址进行加密授权验证,网上这方面的资料代码很多,但很多都有错或者不能正确获取,通过整理与学习,将自己已经正确获得的代码分享出来吧。

code:

#include <iostream> 
#include <string> 
#include <windows.h> 
 
using namespace std; 
 
string GetCPUID() 
{ 
	std::string strCPUId; 
	unsigned long s1, s2; 
	char buf[32] = { 0 }; 
	__asm{ 
		mov eax, 01h   //eax=1:取CPU序列号 
			xor edx, edx 
			cpuid 
			mov s1, edx 
			mov s2, eax 
	} 
	if (s1) { 
		memset(buf, 0, 32); 
		sprintf_s(buf, 32, "%08X", s1); 
		strCPUId += buf; 
	} 
	if (s2) { 
		memset(buf, 0, 32); 
		sprintf_s(buf, 32, "%08X", s2); 
		strCPUId += buf; 
	} 
	__asm{ 
		mov eax, 03h 
			xor ecx, ecx 
			xor edx, edx 
			cpuid 
			mov s1, edx 
			mov s2, ecx 
	} 
	if (s1) { 
		memset(buf, 0, 32); 
		sprintf_s(buf, 32, "%08X", s1); 
		strCPUId += buf; 
	} 
	if (s2) { 
		memset(buf, 0, 32); 
		sprintf_s(buf, 32, "%08X", s2); 
		strCPUId += buf; 
	} 
	return strCPUId; 
} 
 
int main() 
{ 
	cout << "CPUID:" << GetCPUID() << endl; 
	getchar(); 
	return 0; 
} 
 

为了验证获取结果和计算机的是不是一致,可以通过计算机的命令提示符进行查看。

在搜索框内输入cmd,然后输入命令 wmic cpu get ProcessorID 来查看ID信息。

https://blog.csdn.net/zkz10086/article/details/81505254?utm_medium=distribute.pc_relevant.none-task-blog-title-3&spm=1001.2101.3001.4242


本文参考链接:https://www.cnblogs.com/xihong2014/p/13693673.html
阅读延展