在不少商业软件中,须要提供一些能够试运行的版本,这样就须要配套密钥机制来控制,纵观大部分的试用版软件,基本上采用如下几种机制来控制。
1:远程联网激活,每次启动都联网查看使用时间等,这种方法最完美,缺点是无法联网的设备就歇菜了。
2:经过获取本地的硬盘+CPU等硬件的编号,作一个运算,生成一个激活码,超过半数的软件会采用此方法,缺点是不能自由控制软件的其余参数,好比软件中添加的设备数量的控制。
3:设定一个运行到期时间+数量限制+已运行时间的密钥文件,发给用户配套软件使用,缺点是若是仅仅设置的是运行到期时间,用户能够更改电脑时间来获取更长的使用时间,在电脑不联网的状况下。
本demo采用抛砖引玉的形式,用第三种方法来实现,密钥文件采用最简单的异或加密,能够自行改为其余加密方法。算法
完整代码下载地址:https://download.csdn.net/download/feiyangqingyun/10975625app
核心代码:ui
#include "frmmain.h" #include "ui_frmmain.h" #include "qmessagebox.h" #include "qfile.h" frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain) { ui->setupUi(this); this->initForm(); } frmMain::~frmMain() { delete ui; } void frmMain::initForm() { QStringList min; min << "1" << "5" << "10" << "20" << "30"; for (int i = 1; i <= 24; i++) { min << QString::number(i * 60); } ui->cboxMin->addItems(min); ui->cboxMin->setCurrentIndex(1); ui->dateEdit->setDate(QDate::currentDate()); for (int i = 5; i <= 150; i = i + 5) { ui->cboxCount->addItem(QString("%1").arg(i)); } } QString frmMain::getXorEncryptDecrypt(const QString &data, char key) { //采用异或加密,也能够自行更改算法 QByteArray buffer = data.toLatin1(); int size = buffer.size(); for (int i = 0; i < size; i++) { buffer[i] = buffer.at(i) ^ key; } return QLatin1String(buffer); } void frmMain::on_btnOk_clicked() { bool useDate = ui->ckDate->isChecked(); bool useRun = ui->ckRun->isChecked(); bool useCount = ui->ckCount->isChecked(); if (!useDate && !useRun && !useCount) { if (QMessageBox::question(this, "询问", "肯定要生成没有任何限制的密钥吗?") != QMessageBox::Yes) { return; } } QString strDate = ui->dateEdit->date().toString("yyyy-MM-dd"); QString strRun = ui->cboxMin->currentText(); QString strCount = ui->cboxCount->currentText(); QString key = QString("%1|%2|%3|%4|%5|%6").arg(useDate).arg(strDate).arg(useRun).arg(strRun).arg(useCount).arg(strCount); QFile file(QApplication::applicationDirPath() + "/key.db"); file.open(QFile::WriteOnly | QIODevice::Text); file.write(getXorEncryptDecrypt(key, 110).toLatin1()); file.close(); QMessageBox::information(this, "提示", "生成密钥成功,将 key.db 文件拷贝到对应目录便可!"); } void frmMain::on_btnClose_clicked() { this->close(); }
使用demo封装类代码:this
#include "appkey.h" #include "qmutex.h" #include "qfile.h" #include "qtimer.h" #include "qdatetime.h" #include "qapplication.h" #include "qmessagebox.h" AppKey *AppKey::self = NULL; AppKey *AppKey::Instance() { if (!self) { QMutex mutex; QMutexLocker locker(&mutex); if (!self) { self = new AppKey; } } return self; } AppKey::AppKey(QObject *parent) : QObject(parent) { keyData = ""; keyUseDate = false; keyDate = "2017-01-01"; keyUseRun = false; keyRun = 1; keyUseCount = false; keyCount = 10; timer = new QTimer(this); timer->setInterval(1000); connect(timer, SIGNAL(timeout()), this, SLOT(checkTime())); startTime = QDateTime::currentDateTime(); } void AppKey::start() { //判断密钥文件是否存在,不存在则从资源文件复制出来,同时须要设置文件写权限 QString keyName = qApp->applicationDirPath() + "/key.db"; QFile keyFile(keyName); if (!keyFile.exists() || keyFile.size() == 0) { QMessageBox::critical(0, "错误", "密钥文件丢失,请联系供应商!"); exit(0); } //读取密钥文件 keyFile.open(QFile::ReadOnly); keyData = keyFile.readLine(); keyFile.close(); //将从注册码文件中的密文解密,与当前时间比较是否到期 keyData = getXorEncryptDecrypt(keyData, 110); QStringList data = keyData.split("|"); if (data.count() != 6) { QMessageBox::critical(0, "错误", "注册码文件已损坏,程序将自动关闭!"); exit(0); } keyUseDate = (data.at(0) == "1" ? true : false); keyDate = data.at(1); keyUseRun = (data.at(2) == "1" ? true : false); keyRun = data.at(3).toInt(); keyUseCount = (data.at(4) == "1" ? true : false); keyCount = data.at(5).toInt(); //若是启用了时间限制 if (keyUseDate) { QString nowDate = QDate::currentDate().toString("yyyy-MM-dd"); if (nowDate > keyDate) { QMessageBox::critical(0, "错误", "软件已到期,请联系供应商更新注册码!"); exit(0); } } //若是启用了运行时间显示 if (keyUseRun) { timer->start(); } } void AppKey::stop() { timer->stop(); } void AppKey::checkTime() { //找出当前时间与首次启动时间比较 QDateTime now = QDateTime::currentDateTime(); if (startTime.secsTo(now) >= (keyRun * 60)) { QMessageBox::critical(0, "错误", "试运行时间已到,请联系供应商更新注册码!"); exit(0); } } QString AppKey::getXorEncryptDecrypt(const QString &data, char key) { //采用异或加密,也能够自行更改算法 QByteArray buffer = data.toLatin1(); int size = buffer.size(); for (int i = 0; i < size; i++) { buffer[i] = buffer.at(i) ^ key; } return QLatin1String(buffer); } bool AppKey::checkCount(int count) { if (keyUseCount) { if (count >= keyCount) { QMessageBox::critical(0, "错误", "设备数量超过限制,请联系供应商更新注册码!"); return false; } } return true; }