Skip to content

Commit

Permalink
新增秘钥生成器+程序守护进程
Browse files Browse the repository at this point in the history
  • Loading branch information
feiyangqingyun committed Jun 18, 2020
1 parent b7979fa commit 3ea100d
Show file tree
Hide file tree
Showing 42 changed files with 1,582 additions and 2 deletions.
Binary file added 0snap/key.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 0snap/live.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
| 33 | echartgauge | echart仪表盘含交互支持webkit及webengine |
| 34 | ffmpegdemo | 视频流播放ffmpeg内核 |
| 35 | vlcdemo | 视频流播放vlc内核 |
| 36 | designer | QtDesigner4源码 |
| 37 | netserver | 网络中转服务器 |
| 36 | key | 秘钥生成器 |
| 37 | live | 程序守护进程 |
| 38 | designer | QtDesigner4源码 |
| 39 | netserver | 网络中转服务器 |

### 二、学习群
1. **Qt交流大会群 853086607(雨田哥)**
Expand Down Expand Up @@ -86,5 +88,7 @@
![avatar](https://github.com/feiyangqingyun/QWidgetDemo/raw/master/0snap/imageswitch.gif)
![avatar](https://github.com/feiyangqingyun/QWidgetDemo/raw/master/0snap/ffmpegdemo.png)
![avatar](https://github.com/feiyangqingyun/QWidgetDemo/raw/master/0snap/vlcdemo.png)
![avatar](https://github.com/feiyangqingyun/QWidgetDemo/raw/master/0snap/key.png)
![avatar](https://github.com/feiyangqingyun/QWidgetDemo/raw/master/0snap/live.png)
![avatar](https://github.com/feiyangqingyun/QWidgetDemo/raw/master/0snap/netserver.jpg)
![avatar](https://github.com/feiyangqingyun/QWidgetDemo/raw/master/0snap/designer.png)
4 changes: 4 additions & 0 deletions key/key.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += keytool
SUBDIRS += keydemo
122 changes: 122 additions & 0 deletions key/keydemo/appkey.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#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");
keyDate = data.at(1);
keyUseRun = (data.at(2) == "1");
keyRun = data.at(3).toInt();
keyUseCount = (data.at(4) == "1");
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;
}
40 changes: 40 additions & 0 deletions key/keydemo/appkey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef APPKEY_H
#define APPKEY_H

#include <QObject>
#include <QDateTime>

class QTimer;

class AppKey : public QObject
{
Q_OBJECT
public:
static AppKey *Instance();
explicit AppKey(QObject *parent = 0);

private:
static AppKey *self;

QString keyData; //注册码密文
bool keyUseDate; //是否启用运行日期时间限制
QString keyDate; //到期时间字符串
bool keyUseRun; //是否启用可运行时间限制
int keyRun; //可运行时间
bool keyUseCount; //是否启用设备数量限制
int keyCount; //设备限制数量

QTimer *timer; //定时器判断是否运行超时
QDateTime startTime; //程序启动时间

private slots:
void checkTime();
QString getXorEncryptDecrypt(const QString &data, char key);

public slots:
void start();
void stop();
bool checkCount(int count);
};

#endif // APPKEY_H
22 changes: 22 additions & 0 deletions key/keydemo/frmmain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "frmmain.h"
#include "ui_frmmain.h"
#include "appkey.h"

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

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

void frmMain::on_btnAdd_clicked()
{
QString name = ui->lineEdit->text().trimmed();
ui->listWidget->addItem(name);

//计算当前设备数量多少
AppKey::Instance()->checkCount(ui->listWidget->count());
}
25 changes: 25 additions & 0 deletions key/keydemo/frmmain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef FRMMAIN_H
#define FRMMAIN_H

#include <QWidget>

namespace Ui {
class frmMain;
}

class frmMain : public QWidget
{
Q_OBJECT

public:
explicit frmMain(QWidget *parent = 0);
~frmMain();

private:
Ui::frmMain *ui;

private slots:
void on_btnAdd_clicked();
};

#endif // FRMMAIN_H
39 changes: 39 additions & 0 deletions key/keydemo/frmmain.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmMain</class>
<widget class="QWidget" name="frmMain">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>frmMain</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<widget class="QListWidget" name="listWidget"/>
</item>
<item row="1" column="0">
<widget class="QLineEdit" name="lineEdit">
<property name="text">
<string>测试设备</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="btnAdd">
<property name="text">
<string>添加</string>
</property>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
24 changes: 24 additions & 0 deletions key/keydemo/keydemo.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#-------------------------------------------------
#
# Project created by QtCreator 2019-02-17T20:23:58
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = keydemo
TEMPLATE = app
MOC_DIR = temp/moc
RCC_DIR = temp/rcc
UI_DIR = temp/ui
OBJECTS_DIR = temp/obj
DESTDIR = $$PWD/../bin
CONFIG += warn_off

SOURCES += main.cpp
SOURCES += frmmain.cpp appkey.cpp
HEADERS += frmmain.h appkey.h
FORMS += frmmain.ui

34 changes: 34 additions & 0 deletions key/keydemo/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma execution_character_set("utf-8")
#include "frmmain.h"
#include "appkey.h"
#include <QApplication>
#include <QTextCodec>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9));

#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
#endif

//启动密钥服务类
AppKey::Instance()->start();

frmMain w;
w.setWindowTitle("密钥使用示例");
w.show();

return a.exec();
}
Loading

0 comments on commit 3ea100d

Please sign in to comment.