给你的Qt软件加个授权

写在前面

环境:

Win11 64位

VS2019 + Qt5.15.2

核心思路:

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

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

链接openssl库

①官网下载openssl库安装包

官网链接:https://slproweb.com/products/Win32OpenSSL.html

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

②双击安装。注意勾选添加到系统环境变量:

③打开安装目录如下:

④拷贝include文件夹到项目路径下:

⑤拷贝当前项目使用运行库对应的lib到项目路径下,这里使用MD Release版本中的动态库:

不会全部用到,可按需选择使用。

例如只用到以下三个库:

⑥在项目中链接这三个库,包含相应头文件即可使用:

示例

cpp 复制代码
//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;

};
cpp 复制代码
#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;
}
cpp 复制代码
//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();
}
相关推荐
库玛西几秒前
快速幂算法全景总结图:从“愚公移山”到“细胞分裂”
c语言·c++·笔记·算法
小灰灰搞电子15 分钟前
Qt 队列容器 QQueue 的详解与示例
开发语言·qt·qqueue
小鬼头编程26 分钟前
信息学竞赛体系(CSP-J/S、NOIP、NOI、IOI)
c++·人工智能·青少年编程
ACP广源盛1392462567340 分钟前
GSV6155@ACP# Type-C 视频信号转换芯片:技术架构、工程痛点适配与落地场景分析
大数据·c语言·开发语言·人工智能·分布式·嵌入式硬件·架构
我不是懒洋洋43 分钟前
从零实现一个分布式数据管道:dbt的核心设计
c++
程序员-Benothing1 小时前
Java集合:List实现类深度对比
java·开发语言·后端·面试·list
酷在前行1 小时前
【R论文复刻】Nature Communications 冲积图进阶:状态转移、流带排序与多面板排版(保姆级教程)
开发语言·r语言
Darkwanderor1 小时前
使用管道实现进程间通信 (IPC, Inter-Process Communitation)
linux·c语言·c++
减瓦1 小时前
用 Git 为本地文件打造一台时光机
开发语言·git·编辑器
乱七八糟的屋子1 小时前
【C++数值计算】Armadillo超详细入门教程
c++·线性代数·数值计算·科学计算·矩阵运算·armadillo