软件使用权的控制---C/C++代码的实现

本文介绍了一种简单有效的软件使用权控制机制,并提供了Beauty.exe软件的控制代码示例。通过私有加密算法和证书生成,确保只有特定PC可以使用软件功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

         有时候, 自己写了一个软件, 放到网上, 大家都可以使用。但是, 如果要控制别人的使用权, 该怎么办呢?  在本文中, 我们用一个极其简单的例子来说明控制机制。 当然, 如果你的软件写得够好, 有很大的客户需求, 搞一个使用权的控制机制, 说不定可以小小地赚一笔钱呢大笑

 

         我们把这个软件的名称定为Beauty.exe吧, 其源码是(下面的程序只是为了示意, 为了简便起见, 魔鬼数字我就不替换成宏了, 也没有过多考虑空指针等健壮性问题):

 

#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// 获取Beauty.exe所在路径
void getExePath(char *path, int size)
{
	char szBuf[1025] = {0};   
	GetModuleFileName(NULL, szBuf, sizeof(szBuf));
	char *p = strrchr(szBuf, '\\');
	*p = '\0'; 

	strncpy(path, szBuf, size - 1);
	path[size - 1] = '\0';
}

// 私有转化算法(不能公开)
void privateConvert(char *pStr)
{
	int len = strlen(pStr);
	int i = 0;
	for(i = 0; i < len; i++)
	{
		pStr[i] = pStr[i] * pStr[i] % 128; // 爱咋写咋写
		if('\0' == pStr[i])
		{
			pStr[i] = 1;
		}
	}
}


// Beauty.exe的开始部分
int main()
{
	/* Beauty.exe软件的认证部分, 如果不通过, 则不可以用Beauty.exe */


	// 获取Beauty.exe所在目录下的license.txt文件的第一行
	char szLicenseFile[2049] = {0};
	getExePath(szLicenseFile, sizeof(szLicenseFile));
	strcat(szLicenseFile, "\\license.txt"); // strcat不安全哈

	// 校验license.txt文件是否存在
	ifstream in(szLicenseFile);
	if(!in)
	{
		cout << "无license.txt文件, 认证失败" << endl;
		while(1);
		return -1;
	}

	// 获取并校验license.txt文件第一行
	string line;
	getline(in, line);
	if(line == string(""))
	{
		cout << "license.txt文件错误" << endl;
		while(1);
		return -1;
	}

	// 获取license.txt文件指纹
	char szLine[1025] = {0};
	strncpy(szLine, line.c_str(), sizeof(szLine) - 1);


	// 获取运行Beauty.exe的PC的指纹
	char szPC_ID[1025] = "004201EAC230"; // 获取运行Beauty.exe的PC所特有的一个值,假设为004201EAC230(每台PC都不同)
    char szFingerPrint[1025] = "DFEE6B250591BAD1AA7EFA7D0ACB235F"; // szPC_ID的md5值
	cout << szFingerPrint << endl;    // 显示szFingerPrint,让Beauty.exe用户可见, 便于提供给权限控制者(Beauty.exe的开发者) 
	
	// 私有变换
	privateConvert(szFingerPrint);

	
	
	// 判断license.txe文件指纹和PC指纹是否一致
	if(0 != strcmp(szLine, szFingerPrint))
	{
		cout << "license.txt文件认证失败" << endl;
		while(1);
		return -1;
	}


	// 软件的功能部分, 此处略去10000行功能性语句
	cout << "欢迎使用本软件" << endl;

	while(1);

	return 0;
}

       当用户用上面的Beauty.exe软件后, 会在自己的机器上自动生成该机器特有的DFEE6B250591BAD1AA7EFA7D0ACB235F串。 如果用户要真正使用这个软件的功能, 则需要把这个特有的串提供给软件的开发者, 比如你,我。

 



       软件开发者收到这个串之后, 根据下面的代码来制作证书, LicenseGenerator.exe的代码如下:

 

#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// 获取LicenseGenerator.exe所在路径
void getExePath(char *path, int size)
{
	char   szBuf[1025] = {0};   
	GetModuleFileName(NULL, szBuf, sizeof(szBuf));
	char *p = strrchr(szBuf, '\\'); 
	*p = '\0'; 

	strncpy(path, szBuf, size - 1);
	path[size - 1] = '\0';
}

// 私有转化算法(不能公开, 且与Beauty.exe中的函数必须一致)
void privateConvert(char *pStr)
{
	int len = strlen(pStr);
	int i = 0;
	for(i = 0; i < len; i++)
	{
		pStr[i] = pStr[i] * pStr[i] % 128; // 爱咋写咋写
		if('\0' == pStr[i])
		{
			pStr[i] = 1;
		}
	}
}

// LicenseGenerator.exe的开始部分
int main()
{
    char szFingerPrint[1025] = "DFEE6B250591BAD1AA7EFA7D0ACB235F"; // 用户提供的串
	privateConvert(szFingerPrint);

	char szLicenseFile[2049] = {0};
	getExePath(szLicenseFile, sizeof(szLicenseFile));
	strcat(szLicenseFile, "//license.txt"); // strcat不安全哈
	ofstream out(szLicenseFile);
	out << szFingerPrint << endl;

	cout << "制作证书成功" << endl;
	while(1);

	return 0;
}

         当软件的开发者利用LicenseGenerator.exe制作好证书后, 就可以把这个license.txt发给Beauty.exe软件的使用者, 使用者把这个证书放到Beauty.exe所在目录下, 下次运行Beauty.exe的时候, 就通过了校验。

 

 

         上述只是控制机制的一个非常简单的模型, 我们可以看到, 没有证书不行, 证书错误也不行。 这样, 对于每个证书, 只有一台PC可用, 其他人不可用。 实际上还可以控制用户使用的时间, 我就不赘述了。 有兴趣的朋友们, 赶快试一下吧。

 

         最后强调一点, 私有加密算法和LicenseGenerator.exe绝对不能泄露, 否则, 使用权控制的机制形同虚设。 当然啦, 要想更安全, 那就需要在私有加密算法上大作文章。

 

 

       

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值