用Qt制作License流程:
我们通过CPU的ID号加密生成密钥(不一定要CPU的ID号,但是我认为CPU的ID号作为上位机软件的数据验证基本上可以交付给客户,如果觉得强度不够可以自己添加一些别的密钥)
License模块分为两个部分:
1.制作密钥生成工具
2.产品软件中加入密钥模块
制作密钥生成工具:
制作密钥生成工具是为了能够通过CPU的ID号再进行算法加密改为自己的密钥,在运行过程中,由客户发送给开发人员CPU的ID号,开发人员再进行密钥转换
插入密钥模块:
在源码中插入密钥校验的功能,看密钥是否正确,不正确则退出软件
制作密钥生成工具源码:
这里的密钥生成工具其实也就是个小Demo,能用就行
查看电脑CPU的ID方法:控制台下敲入命令:wmic CPU get ProcessorID
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_ChangBtn_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QCryptographicHash>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_ChangBtn_clicked()
{
//这里的**可以自己编辑一下
QString text = ui->InputCPUIdLed->text() + "**";
QByteArray array;
array.append(text);
QCryptographicHash hash(QCryptographicHash::Md5); //Md5加密
hash.addData(array); //添加数据
QByteArray retArray = hash.result(); //加密后的数据
ui->LisenceLed->setText(retArray.toHex());
}
当转换密钥完成后,发送给客户然后粘贴到对应的txt文档(在下面会说明txt文档哪里来的)里面
产品软件中加入密钥模块的源码:
在进入的时候先进行校验,用txt文档去存储密钥数值,这里只要在main函数下做一些校验工作就行了:
#include "GeometryParameterForm.h"
#include <QApplication>
#include "common.h"
#include "BsMainTask.h"
#include "LoginDialog.h"
#if _MSC_VER >= 1600 //判断编译器版本(MSVC)
#pragma execution_character_set("utf-8") //强制转换为UTF-8
#endif
//获取当前主机CPU的ID
//加密方式 主机ID+hysc
//加密算法:Md5
QString GetCurrentCpuId()
{
QString cpuId = "";
QProcess p(0);
p.start("wmic CPU get ProcessorID"); p.waitForStarted();
p.waitForFinished();
cpuId = QString::fromLocal8Bit(p.readAllStandardOutput());
cpuId = cpuId.remove("ProcessorId").trimmed();
//这里的**可以自己编辑
cpuId += "**";
QByteArray array;
array.append(cpuId);
QCryptographicHash hash(QCryptographicHash::Md5); //Md5加密
hash.addData(array); //添加数据
QByteArray retArray = hash.result(); //加密后的数据
return QString(retArray.toHex());;
}
//在txt文件里读取License的密钥
QString ReadLicense()
{
//当前程序文件路径
QString path = QDir::cleanPath(QCoreApplication::applicationDirPath() + QDir::separator() + QString("/License.txt"));
QFile file(path);
file.open(QIODevice::ReadWrite | QIODevice::Text);//文件不存在会自动创建
QString data;
//判断文件是否打开成功
if(file.isOpen())
{
if(file.size() != 0)//文件有数据
{
QTextStream in(&file);
data=in.readLine();//读取一行存到data里
file.close();
}
else
{
file.close();//关闭文件
}
}
else//文件打开失败
{
}
return data;
}
//License判断
bool JudgeCpuIdLegal(QString cpuId , QString allowId)
{
if(cpuId == allowId)
{
return true;
}
return false;
}
//日志模块(不用管)
void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
switch (type)
{
case QtDebugMsg:
gl_log.LogMessage(LOG_DEBUG, msg.toLocal8Bit().data());
break;
case QtWarningMsg:
gl_log.LogMessage(LOG_WARN, msg.toLocal8Bit().data());
break;
case QtCriticalMsg:
gl_log.LogMessage(LOG_CRIT, msg.toLocal8Bit().data());
break;
case QtFatalMsg:
gl_log.LogMessage(LOG_ERROR, msg.toLocal8Bit().data());
}
}
int main(int argc, char *argv[])
{
//初始化时钟
StartHighResolutionTimer();
//保存系统运行时间
g_iSystemBeginRunTime = EndHighResolutionTimer()/1000;
QApplication a(argc, argv);
//Liscense验证模块,实际上要看的就只有这里
QString allowLicense = ReadLicense();
QString currrentCpuId = GetCurrentCpuId();
if(!JudgeCpuIdLegal(currrentCpuId,allowLicense))//当前主机CPU序列号未授权则程序退出
{
QMessageBox::warning(nullptr,"程序错误","非法硬件!");
return -1;
}
//下面就是业务了
CMainTask* pMainTask = new CMainTask;
LoginDialog logindialog;
int ret = -1;
if (logindialog.exec() == QDialog::Accepted)
{
gl_log.LogMessage(LOG_INFO, "已进入主界面");
GeometryParameterForm w;
w.show();
pMainTask->TaskStart();
ret = a.exec();
}
//任务停止
pMainTask->TaskStop();
delete pMainTask;
gl_log.LogMessage(LOG_INFO, "系统已退出!");
SLEEP(1000);
g_terminal = true;
//删除队列
gl_log.ReleaseAllRes();
return ret;
}
这种密钥做起来很简单,也基本够用,如果觉得强度不够自己多加几个校验机制嘛