给你的Qt软件加个授权

时间:2024-04-12 17:45:37

写在前面

环境:

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();
}