目录
Qt开发记录18——授权校验
QT软件开发: 获取CPU序列号、硬盘序列号、主板序列号 (采用wmic命令)
------------------------------------------------------------------
参考:
QT软件开发: 获取CPU序列号、硬盘序列号、主板序列号 (采用wmic命令)
QT软件开发: 获取CPU序列号、硬盘序列号、主板序列号 (采用wmic命令)_qt获取cpu序列号_我不是萧海哇~~~~的博客-CSDN博客
Qt获取CPU序列号(最简单的方式)_qt getcpuid_luoyayun361的博客-CSDN博客
以上方式可能在某些机子上无法正常获取,接下来通过一种复杂的方式来获取 CPU 序列号。
Qt获取CPU编号和硬盘序列号 Qt获取CPU编号和硬盘序列号_weixin_34032827的博客-CSDN博客
-----------------------------------------------------
cpu的获取,可能有些麻烦:有些代码,并不适用于所有的 通用 CPU。
因此,如果不是特别需求,可以不用考虑 这个吧?
======================================
Qt获取CPU编号和硬盘序列号
Qt获取CPU编号和硬盘序列号_weixin_34032827的博客-CSDN博客
windows下执行命令除了用cmd之外,还有个东西叫 WMIC,非常强大,可以通过他获取很多信息,包括硬件信息。
QString frmMain::getWMIC(const QString &cmd)
{
//获取cpu名称:wmic cpu get Name
//获取cpu核心数:wmic cpu get NumberOfCores
//获取cpu线程数:wmic cpu get NumberOfLogicalProcessors
//查询cpu序列号:wmic cpu get processorid
//查询主板序列号:wmic baseboard get serialnumber
//查询BIOS序列号:wmic bios get serialnumber
//查看硬盘:wmic diskdrive get serialnumber
QProcess p;
p.start(cmd);
p.waitForFinished();
QString result = QString::fromLocal8Bit(p.readAllStandardOutput());
QStringList list = cmd.split(" ");
result = result.remove(list.last(), Qt::CaseInsensitive);
result = result.replace("\r", "");
result = result.replace("\n", "");
result = result.simplified();
return result;
}
Qt开发记录18——授权校验
授权校验
Windows系统,获取cpu序列号、BIOS序列号、网卡mac地址
代码
Linux系统,获取硬盘序列号、网卡MAC地址
代码
生成的授权码
代码
读取授权文件中的授权码
代码
校验
代码
Windows系统,获取cpu序列号、BIOS序列号、网卡mac地址
代码
#ifdef CXBD_WINDOWS
QString WinGetHwID()
{
// cpu序列号
QString str1 = getWMIHWInfo(0);
printLog("cpu序列号: " + str1);
QString md5Str1 = QCryptographicHash::hash(str1.toLatin1(), QCryptographicHash::Md5).toHex();
// BIOS序列号
QString str2 = getWMIHWInfo(1);
printLog("BIOS序列号: " + str2);
QString md5Str2 = QCryptographicHash::hash(str2.toLatin1(), QCryptographicHash::Md5).toHex();
// 网卡mac地址
QString str3 = getWMIHWInfo(2);
printLog("网卡: " + str3);
QString md5Str3 = QCryptographicHash::hash(str3.toLatin1(), QCryptographicHash::Md5).toHex();
QString authStr = md5Str1 + md5Str2 + md5Str3;
return authStr;
}
QString getWMIHWInfo(int type)
{
/*
* 注意:qt调用wmi时,对查询语句要求很严格,所以like之类的句子务必精确才能有结果出来
*
* 0. 处理器ID
* SELECT ProcessorId ...
*
* 1. BIOS序列号
* SELECT SerialNumber ...
*
* 2. 当前原生网卡地址
* SELECT MACAddress ...
*
*/
QString hwInfo;
QStringList sqlCmd;
sqlCmd.clear();
sqlCmd << "SELECT ProcessorId FROM Win32_Processor WHERE (ProcessorId IS NOT NULL)";
sqlCmd << "SELECT SerialNumber FROM Win32_BIOS WHERE (SerialNumber IS NOT NULL)";
sqlCmd << "SELECT MACAddress FROM Win32_NetworkAdapter WHERE (MACAddress IS NOT NULL) AND (PNPDeviceID LIKE 'PCI%')";
QStringList columnName;
columnName.clear();
columnName << "ProcessorId";
columnName << "SerialNumber";
columnName << "MACAddress";
QAxObject *objIWbemLocator = new QAxObject("WbemScripting.SWbemLocator");
QAxObject *objWMIService = objIWbemLocator->querySubObject("ConnectServer(QString&,QString&)",QString("."), QString("root\\cimv2"));
QString query;
if (type < sqlCmd.size()) {
query = sqlCmd.at(type);
}
QAxObject *objInterList = objWMIService->querySubObject("ExecQuery(QString&))", query);
QAxObject *enum1 = objInterList->querySubObject("_NewEnum");
IEnumVARIANT *enumInterface = 0;
enum1->queryInterface(QUuid(IID_IEnumVARIANT), (void**)&enumInterface);
enumInterface->Reset();
for ( int i = 0; i < objInterList->dynamicCall("Count").toInt(); ++i )
{
VARIANT *theItem = (VARIANT*)malloc(sizeof(VARIANT));
if (enumInterface->Next(1, theItem, NULL) != S_FALSE)
{
QAxObject *item = new QAxObject((IUnknown *)theItem->punkVal);
if (item) {
if (type<columnName.size()) {
QByteArray datagram(columnName.at(type).toLatin1());
const char* tempConstChar = datagram.data();
hwInfo+=item->dynamicCall(tempConstChar).toString().trimmed();
}
}
}
}
return hwInfo;
}
#endif
Linux系统,获取硬盘序列号、网卡MAC地址
代码
#ifdef CXBD_GUOCHAN
QString LinuxGetHwID()
{
// 硬盘序列号
QString cmd1 = "lsblk -d -no serial /dev/sda 2>/dev/null || lsblk -d -no serial /dev/nvme0n1 2>/dev/null || lsblk -d -no serial /dev/hda 2>/dev/null";
QString str1 = getWMIC1(cmd1);
printLog("硬盘序列号: " + str1);
QString md5Str1 = QCryptographicHash::hash(str1.toLatin1(), QCryptographicHash::Md5).toHex();
// 网卡MAC地址
QString cmd2 = "ip addr";
QString str2 = getWMIC2(cmd2);
printLog("网卡MAC地址: " + str2);
QString md5Str2 = QCryptographicHash::hash(str2.toLatin1(), QCryptographicHash::Md5).toHex();
QString authStr = md5Str1 + md5Str2;
return authStr;
}
// 根据命令获取信息
QString getWMIC(const QString &cmd)
{
QProcess p;
p.start(cmd);
p.waitForFinished();
return QString::fromLocal8Bit(p.readAllStandardOutput());
}
// 硬盘序列号
QString getWMIC1(const QString &cmd)
{
QString result = getWMIC(cmd);
QStringList list = cmd.split(" ");
result = result.remove(list.last(), Qt::CaseInsensitive);
result = result.replace("\r", "");
result = result.replace("\n", "");
result = result.replace(" ", "");
result = result.simplified();
return result;
}
// 网卡MAC地址
QString getWMIC2(const QString &cmd)
{
QString result = getWMIC(cmd);
QStringList list;
foreach (QString lineStr,result.split("\n"))
{
if (lineStr.contains("ether")) {
list.append(lineStr.left(lineStr.indexOf("ether") + 5 + 18).right(18).trimmed());
}
}
result = list.join("");
return result;
}
#endif
生成的授权码
代码
QString sysHid;
#ifdef CXBD_WINDOWS
sysHid = WinGetHwID();
#endif
#ifdef CXBD_GUOCHAN
sysHid = LinuxGetHwID();
#endif
sysHid += "xy@123456";
QString md5Str1 = QCryptographicHash::hash(sysHid.toLatin1(), QCryptographicHash::Md5).toHex();
printLog("生成的授权码: " + md5Str1);
读取授权文件中的授权码
代码
QString readHwID()
{
QString authFilePath = QCoreApplication::applicationDirPath() + QDir::separator() + "authFileInfo";
printLog("授权校验文件路径: " + authFilePath);
QString authStrRead;
//构造一个QFile对象
QFile file(authFilePath);
if(file.exists() && file.open(QIODevice::ReadOnly | QIODevice::Text)) //以只读方式打开
{
authStrRead.append(file.readAll());
//关闭文件
file.close();
}
printLog("读取的授权码: " + authStrRead);
return authStrRead;
}
校验
代码
bool authorizationVerification()
{
QString sysHid;
#ifdef CXBD_WINDOWS
sysHid = WinGetHwID();
#endif
#ifdef CXBD_GUOCHAN
sysHid = LinuxGetHwID();
#endif
sysHid += "xy@123456";
QString md5Str1 = QCryptographicHash::hash(sysHid.toLatin1(), QCryptographicHash::Md5).toHex();
// printLog("生成的授权码: " + md5Str1);
bool boo = false;
QString sysHidRead = readHwID();
sysHidRead = sysHidRead.replace("\n", "");
printLog("读取的授权码: " + sysHidRead);
if (sysHidRead == md5Str1) {
printLog("------>授权码一致,通过授权校验!");
boo = true;
}
return boo;
}
加密授权验证学习之一 ——获取计算机CPUID序列号