给你的Qt软件加个授权

2024-04-12 11:20
文章标签 qt 软件 授权 加个

本文主要是介绍给你的Qt软件加个授权,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

写在前面

环境:

Win11 64位

VS2019 + Qt5.15.2

核心思路:

将授权相关信息加密保存到License.txt中,软件运行时获取并解密授权信息,判断是否在限制期限内即可。

加解密部分使用第三方openssl库进行,因此需要手动在项目中链接下openssl库,参考步骤如下。

链接openssl库

①官网下载openssl库安装包
官网链接:https://slproweb.com/products/Win32OpenSSL.html

下载自己当前操作系统位数对应的安装包即可,我当前系统为Win11 64位,因此下载以下安装包:
1

②双击安装。注意勾选添加到系统环境变量:
2
③打开安装目录如下:
3

④拷贝include文件夹到项目路径下:
4
⑤拷贝当前项目使用运行库对应的lib到项目路径下,这里使用MD Release版本中的动态库:
5
不会全部用到,可按需选择使用。

例如只用到以下三个库:
6
⑥在项目中链接这三个库,包含相应头文件即可使用:
7

示例

//AuthorizationManager.h
#pragma once
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>struct MyLicense
{int nAuthorizationStatus{ 1 };    //0: 未授权 1: 已授权std::string sFirstRunDate;    //首次运行日期int nAuthorizationDays{ -1 };        //授权天数std::string Serialize(){std::ostringstream os;os << nAuthorizationStatus << "\n" << sFirstRunDate << "\n" << nAuthorizationDays << "\n";return os.str();}void Deserialize(const std::string& str){std::istringstream is(str);is >> nAuthorizationStatus;is.ignore();std::getline(is, sFirstRunDate);sFirstRunDate = sFirstRunDate.substr(0, sFirstRunDate.length());is >> nAuthorizationDays;}
};class AuthorizationManager
{
public:static AuthorizationManager& Instance(){static AuthorizationManager instance;return instance;}AuthorizationManager(const AuthorizationManager&) = delete;AuthorizationManager& operator=(const AuthorizationManager&) = delete;std::string do_encrypt(const std::string& plaintext, const std::string& key, const std::string& iv);std::string do_decrypt(const std::string& ciphertext, const std::string& key, const std::string& iv);bool AuthorizationVerify();private:AuthorizationManager() = default;};
#include "AuthorizationManager.h"
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <vector>
#include <QDate>std::string AuthorizationManager::do_encrypt(const std::string& plaintext, const std::string& key, const std::string& iv)
{EVP_CIPHER_CTX* ctx;std::vector<unsigned char> ciphertext(plaintext.size() + EVP_MAX_BLOCK_LENGTH);int len;int ciphertext_len;if (!(ctx = EVP_CIPHER_CTX_new())){return "";}if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)key.c_str(), (const unsigned char*)iv.c_str())){return "";}if (1 != EVP_EncryptUpdate(ctx, ciphertext.data(), &len, (const unsigned char*)plaintext.c_str(), plaintext.size())){return "";}ciphertext_len = len;if (1 != EVP_EncryptFinal_ex(ctx, ciphertext.data() + len, &len)){return "";}ciphertext_len += len;EVP_CIPHER_CTX_free(ctx);return std::string((char*)ciphertext.data(), ciphertext_len);
}std::string AuthorizationManager::do_decrypt(const std::string& ciphertext, const std::string& key, const std::string& iv)
{EVP_CIPHER_CTX* ctx;std::vector<unsigned char> plaintext(ciphertext.size());int len;int plaintext_len;if (!(ctx = EVP_CIPHER_CTX_new())){return "";}if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)key.c_str(), (const unsigned char*)iv.c_str())){return "";}if (1 != EVP_DecryptUpdate(ctx, plaintext.data(), &len, (const unsigned char*)ciphertext.c_str(), ciphertext.size())){return "";}plaintext_len = len;if (1 != EVP_DecryptFinal_ex(ctx, plaintext.data() + len, &len)){return "";}plaintext_len += len;EVP_CIPHER_CTX_free(ctx);return std::string((char*)plaintext.data(), plaintext_len);
}bool AuthorizationManager::AuthorizationVerify()
{//密钥对,妥善保管std::string key = "98765432100123456789987654321001";std::string iv = "9876543210012345";//构造授权//MyLicense original;//original.nAuthorizationStatus = 1;//original.sFirstRunDate = "2024-04-08";//original.nAuthorizationDays = 90;//std::string serialized_str = original.Serialize();//std::string ciphertext = AuthorizationManager::Instance().do_encrypt(serialized_str, key, iv);//std::ofstream outfile("License.txt", std::ios::binary);//outfile << ciphertext;//outfile.close();//获取授权std::ifstream infile("License.txt", std::ios::binary);std::stringstream ss;ss << infile.rdbuf();std::string content = ss.str();std::string decryptedtext = AuthorizationManager::Instance().do_decrypt(content, key, iv);MyLicense restored;restored.Deserialize(decryptedtext);//授权校验bool bRet = false;if (restored.nAuthorizationStatus == 0){//首次运行授权MyLicense original;original.nAuthorizationStatus = 1;QDate curDate = QDate::currentDate();std::string sCurDate = curDate.toString("yyyy-MM-dd").toStdString();original.sFirstRunDate = sCurDate;original.nAuthorizationDays = restored.nAuthorizationDays;std::string serialized_str = original.Serialize();std::string ciphertext = AuthorizationManager::Instance().do_encrypt(serialized_str, key, iv);std::ofstream outfile("License.txt", std::ios::binary);outfile << ciphertext;outfile.close();bRet = true;}else{QDate curDate = QDate::currentDate();QDate firstRunDate = QDate::fromString(QString::fromStdString(restored.sFirstRunDate), "yyyy-MM-dd");QDate endDate = firstRunDate.addDays(restored.nAuthorizationDays);if (curDate < firstRunDate){bRet = false;}else if (curDate > endDate){bRet = false;}else{bRet = true;}}return bRet;
}
//main.cpp
#include "AuthorizationWidget.h"
#include <QtWidgets/QApplication>
#include "AuthorizationManager.h"int main(int argc, char* argv[])
{QApplication a(argc, argv);//授权检查if (!AuthorizationManager::Instance().AuthorizationVerify()){//未授权,退出软件return false;}AuthorizationWidget w;w.show();return a.exec();
}

这篇关于给你的Qt软件加个授权的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/896988

相关文章

安装centos8设置基础软件仓库时出错的解决方案

《安装centos8设置基础软件仓库时出错的解决方案》:本文主要介绍安装centos8设置基础软件仓库时出错的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录安装Centos8设置基础软件仓库时出错版本 8版本 8.2.200android4版本 javas

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

如何确定哪些软件是Mac系统自带的? Mac系统内置应用查看技巧

《如何确定哪些软件是Mac系统自带的?Mac系统内置应用查看技巧》如何确定哪些软件是Mac系统自带的?mac系统中有很多自带的应用,想要看看哪些是系统自带,该怎么查看呢?下面我们就来看看Mac系统内... 在MAC电脑上,可以使用以下方法来确定哪些软件是系统自带的:1.应用程序文件夹打开应用程序文件夹

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言

Qt中Qfile类的使用

《Qt中Qfile类的使用》很多应用程序都具备操作文件的能力,包括对文件进行写入和读取,创建和删除文件,本文主要介绍了Qt中Qfile类的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.QFile文件操作3.演示示例3.1实验一3.2实验二【演示 QFile 读写二进制文件的过程】4.

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Mysql用户授权(GRANT)语法及示例解读

《Mysql用户授权(GRANT)语法及示例解读》:本文主要介绍Mysql用户授权(GRANT)语法及示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql用户授权(GRANT)语法授予用户权限语法GRANT语句中的<权限类型>的使用WITH GRANT

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言