百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

Qt设备识别(简单的密钥生成器)

ztj100 2025-02-26 14:46 31 浏览 0 评论

一、介绍

在很多商业软件中,需要提供一些可以试运行的版本,这样就需要配套密钥机制来控制,纵观大部分的试用版软件,基本上采用以下几种机制来控制。

1、远程联网激活,每次启动都联网查看使用时间等,这种方法最完美,缺点是没法联网的设备就歇菜了。

2、通过获取本地的硬盘、CPU等硬件的编号,做一个运算,生成一个激活码,超过半数的软件会采用此方法,缺点是不能自由控制软件的其他参数,比如软件中添加的设备数量的控制。

3、设定一个运行到期时间+数量限制+已运行时间的密钥文件,发给用户配套软件使用,缺点是如果仅仅设置的是运行到期时间,用户可以更改电脑时间来获取更长的使用时间,在电脑不联网的情况下。

本demo实现第2种方式;

第3中方式请参考:

Qt编写密钥生成器+使用demo(开源)_Qt自定义控件大全+UI定制+输入法+视频监控+物联网-CSDN博客

二、功能描述

1、软件A(KeyDemo)首次运行弹出输入注册码(密钥)对话框,点击“获取机器码”,把机器码(系统的UUID)发送给商家;

2、商家使用密钥生成器(Key)生成注册码(密钥),给软件A;

3、软件A输入注册码,点击“确认”,软件正常启动;

4、第二次运行软件A时,若还是同一台电脑,则直接正常运行;若不是同一台电脑则弹出输入注册码对话框;

三、查看windows系统的UUID

1、win+r,打开命令提示符,输入wmic,点击确定



2、输入csproduct,回车(enter),UUID即为一会要用到的机器码



如何查看windows系统UUID_weixin_33690963的博客-CSDN博客

3、cmd运行wmic提示

‘wmic‘ 不是内部或外部命令,也不是可运行的程序或批处理文件

或出现以下提示



解决办法:

①win+r,打开命令提示符,输入sysdm.cpl,点击确定



②选择“高级”->“环境变量”



③“系统变量”->“Path”->编辑



④“新建”,输入“C:\Windows\System32\wbem”,点击“确定”,之后就可以正常运行wmic了



cmd运行wmic提示‘wmic‘ 不是内部或外部命令,也不是可运行的程序或批处理文件_东京没有下雨天-CSDN博客_wmic不是内部或外部命令

四、Qt使用AES加密解密

https://github.com/bricke/Qt-AES

先下载qaesencryption.cpp和qaesencryption.h,程序中会用到



五、创建工程KeyDemo

1、新建Qt Widgets应用,名称为KeyDemo,基类选择QMainWindow;

2、添加新的Qt设计师界面类,名称为KeyDialog,界面样式如下



3、添加新的C++类,名称为KeyVerify

4、把下载好的qaesencryption.cpp和qaesencryption.h添加到工程中

六、代码KeyDemo演示

1、KeyDialog.h

#ifndef KEYDIALOG_H
#define KEYDIALOG_H

#include 

namespace Ui {
class KeyDialog;
}

class KeyDialog : public QDialog
{
Q_OBJECT

public:
explicit KeyDialog(QWidget *parent = nullptr);
~KeyDialog();

static QString g_key;

private slots:
void on_cancelButton_clicked();

void on_okButton_clicked();

void on_uuidButton_clicked();

private:
Ui::KeyDialog *ui;
};

#endif // KEYDIALOG_H

2、KeyDialog.cpp

#include "KeyDialog.h"
#include "ui_KeyDialog.h"
#include 
#include 
#include 
QString KeyDialog::g_key = "";

KeyDialog::KeyDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::KeyDialog)
{
ui->setupUi(this);
ui->uuidEdit->setVisible(false);
ui->tipsLabel->setVisible(false);
}

KeyDialog::~KeyDialog()
{
delete ui;
}

void KeyDialog::on_cancelButton_clicked()
{
exit(0);
}

void KeyDialog::on_okButton_clicked()
{
g_key = ui->keyEdit->text();
this->close();
}

//获取UUID
void KeyDialog::on_uuidButton_clicked()
{
QString cmd = "wmic csproduct get uuid";
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();

p.kill();
p.close();

ui->tipsLabel->setVisible(true);
ui->uuidEdit->setVisible(true);
ui->uuidEdit->setText(result);
}

3、KeyVerify.h

#ifndef KEYVERIFY_H
#define KEYVERIFY_H
#include 
class KeyVerify
{
public:
KeyVerify();
private slots:
void initSystem(); //初始化
QString getUUID(); //获取唯一标识
bool enterKey(); //输入密钥
QString encodedText(QString, QString key = "zxcvbnm"); //加密
QString decodedText(QString, QString key = "zxcvbnm"); //解密
};
#endif // KEYVERIFY_H

4、KeyVerify.cpp

#include "KeyVerify.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "qaesencryption.h"
#include "KeyDialog.h"

KeyVerify::KeyVerify()
{
initSystem();
}

void KeyVerify::initSystem()
{
QString keyName = qApp->applicationDirPath() + "/key.db";
QFile keyFile(keyName);

//密钥文件不存在
if (!keyFile.exists() || keyFile.size() == 0)
{
//弹出输入密钥界面
if(!enterKey()) //密钥不对
{
exit(0);
}
}
else
{
//读取密钥文件
keyFile.open(QFile::ReadOnly);
QByteArray keyData = keyFile.readAll();
keyFile.close();

QString decodedStr = decodedText(QString::fromLatin1(keyData));

QString uuid = getUUID();

if(uuid != decodedStr) //密钥不对
{
int ret = QMessageBox::critical(nullptr, "错误", "密钥文件不对,请联系供应商!", QMessageBox::Ok, QMessageBox::Cancel);
if( ret == QMessageBox::Ok)
{
if(!enterKey()) //密钥不对
{
keyFile.close();
exit(0);
}
}
else
{
keyFile.close();
exit(0);
}
}
keyFile.close();
}
}

//获取电脑UUID
QString KeyVerify::getUUID()
{
QString cmd = "wmic csproduct get uuid";
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();

p.kill();
p.close();

return result;
}

//输入密钥
bool KeyVerify::enterKey()
{
//弹出输入密钥界面
KeyDialog keyDialog;
keyDialog.exec();

if(KeyDialog::g_key != "")
{
//解密
QString decodedStr = decodedText(KeyDialog::g_key);
//获取本地uuid
QString uuid = getUUID();
//对比
if(uuid != decodedStr)
{
QMessageBox::critical(nullptr, "错误", "密钥文件不对,请联系供应商!");
return false;
}
else
{
QFile keyFile(qApp->applicationDirPath() + "/key.db");
//存储
keyFile.open(QFile::WriteOnly);
keyFile.write(KeyDialog::g_key.toLatin1());
keyFile.close();
}
}
else
{
QMessageBox::critical(nullptr, "错误", "密钥不能为空");
return false;
}
return true;
}

//加密
QString KeyVerify::encodedText(QString data, QString key)
{
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);
QByteArray hashKey = QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Md5);
QByteArray encodedText = encryption.encode(data.toUtf8(), hashKey);
QString encodeTextStr = QString::fromLatin1(encodedText.toBase64());
//qDebug()<< "encodedText:"<< encodeTextStr;

return encodeTextStr;
}

//解密
QString KeyVerify::decodedText(QString data, QString key)
{
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);
QByteArray hashKey = QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Md5);
QByteArray decodedText = encryption.decode(QByteArray::fromBase64(data.toLatin1()), hashKey);
QString decodedTextStr = QString::fromLatin1(decodedText);
//qDebug()<<"decodedText:"<< decodedTextStr;

return decodedTextStr;
}

5、main.cpp

#include "MainWindow.h"
#include 
#include "KeyVerify.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

//密钥验证
KeyVerify k;

MainWindow w;
w.show();

return a.exec();
}

七、创建工程Key

1、新建Qt Widgets应用,名称为Key,基类选择QMainWindow,界面设计如下



2、把下载好的qaesencryption.cpp和qaesencryption.h添加到工程中

八、代码Key演示

1、MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();

private slots:
void on_keyButton_clicked();

QString encodedText(QString data, QString key);

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

2、MainWindow.cpp

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include 
#include 
#include 
#include "qaesencryption.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_keyButton_clicked()
{
if(ui->uuidEdit->text().isEmpty())
{
QMessageBox::critical(this, tr("提示"), tr("机器码不能为空"));
return;
}

if(ui->passwordEdit->text().isEmpty())
{
QMessageBox::critical(this, tr("提示"), tr("密码不能为空"));
return;
}

//加密
QString key = encodedText(ui->uuidEdit->text(), ui->passwordEdit->text());
ui->keyEdit->setText(key);
ui->keyEdit->setFocus();
}

//加密
QString MainWindow::encodedText(QString data, QString key)
{
//使用QCryptographicHash(只能加密不能解密)类对密码进行加密
QByteArray hashKey = QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Md5);

//使用AES加密
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);

QByteArray encodedText = encryption.encode(data.toUtf8(), hashKey);
QString encodeTextStr = QString::fromLatin1(encodedText.toBase64());
//qDebug()<< "encodedText:"<< encodeTextStr << encodedText;

return encodeTextStr;
}

九、运行测试

1、先运行KeyDemo,弹出输入密钥对话框



2、点击“获取机器码”,把机器码给商家(软件Key)


3、运行软件Key,输入机器码和密码,点击生成密钥,Ctrl+a、Ctrl+v复制密钥,把密钥发送给KeyDemo软件;



注:密码需要和KeyDemo工程中KeyVerify.h的“zxcvbnm”一样

QString encodedText(QString, QString key = "zxcvbnm"); //加密

QString decodedText(QString, QString key = "zxcvbnm"); //解密

4、KeyDemo软件输入密钥后,点击“确定”,软件正常运行,弹出主界面



5、KeyDemo软件若还在刚才的电脑上第二次运行,会直接弹出主界面;若不是在同一台电脑上则弹出输入密钥对话框;

6、KeyDemo程序可执行文件exe的同级目录下用key.db文件,里面保存着输入的密钥


【领QT开发教程学习资料,点击下方链接莬费领取↓↓,先码住不迷路~】

点击这里:「链接」

相关推荐

Vue3非兼容变更——函数式组件(vue 兼容)

在Vue2.X中,函数式组件有两个主要应用场景:作为性能优化,因为它们的初始化速度比有状态组件快得多;返回多个根节点。然而在Vue3.X中,有状态组件的性能已经提高到可以忽略不计的程度。此外,有状态组...

利用vue.js进行组件化开发,一学就会(一)

组件原理/组成组件(Component)扩展HTML元素,封装可重用的代码,核心目标是为了可重用性高,减少重复性的开发。组件预先定义好行为的ViewModel类。代码按照template\styl...

Vue3 新趋势:10 个最强 X 操作!(vue.3)

Vue3为前端开发带来了诸多革新,它不仅提升了性能,还提供了...

总结 Vue3 组件管理 12 种高级写法,灵活使用才能提高效率

SFC单文件组件顾名思义,就是一个.vue文件只写一个组件...

前端流行框架Vue3教程:17. _组件数据传递

_组件数据传递我们之前讲解过了组件之间的数据传递,...

前端流行框架Vue3教程:14. 组件传递Props效验

组件传递Props效验Vue组件可以更细致地声明对传入的props的校验要求...

前端流行框架Vue3教程:25. 组件保持存活

25.组件保持存活当使用...

5 个被低估的 Vue3 实战技巧,让你的项目性能提升 300%?

前端圈最近都在卷性能优化和工程化,你还在用老一套的Vue3开发方法?作为摸爬滚打多年的老前端,今天就把私藏的几个Vue3实战技巧分享出来,帮你在开发效率、代码质量和项目性能上实现弯道超车!一、...

绝望!Vue3 组件频繁崩溃?7 个硬核技巧让性能暴涨 400%!

前端的兄弟姐妹们五一假期快乐,谁还没在Vue3项目上栽过跟头?满心欢喜写好的组件,一到实际场景就频频崩溃,页面加载慢得像蜗牛,操作卡顿到让人想砸电脑。用户疯狂吐槽,领导脸色难看,自己改代码改到怀疑...

前端流行框架Vue3教程:15. 组件事件

组件事件在组件的模板表达式中,可以直接使用...

Vue3,看这篇就够了(vue3 从入门到实战)

一、前言最近很多技术网站,讨论的最多的无非就是Vue3了,大多数都是CompositionAPI和基于Proxy的原理分析。但是今天想着跟大家聊聊,Vue3对于一个低代码平台的前端更深层次意味着什么...

前端流行框架Vue3教程:24.动态组件

24.动态组件有些场景会需要在两个组件间来回切换,比如Tab界面...

前端流行框架Vue3教程:12. 组件的注册方式

组件的注册方式一个Vue组件在使用前需要先被“注册”,这样Vue才能在渲染模板时找到其对应的实现。组件注册有两种方式:全局注册和局部注册...

焦虑!Vue3 组件频繁假死?6 个奇招让页面流畅度狂飙 500%!

前端圈的朋友们,谁还没在Vue3项目上踩过性能的坑?满心期待开发出的组件,一到高并发场景就频繁假死,用户反馈页面点不动,产品经理追着问进度,自己调试到心态炸裂!别以为这是个例,不少人在电商大促、数...

前端流行框架Vue3教程:26. 异步组件

根据上节课的代码,我们在切换到B组件的时候,发现并没有网络请求:异步组件:...

取消回复欢迎 发表评论: