欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于MD5 AES的软件加密系统

程序员文章站 2022-05-12 20:32:27
...

暑假导师让给一个之前做的激光光斑检测软件进行加密。 这个任务让我很难受,在CSDN找了好久,还好有大神写的文章。

加密的思路是,获取安装软件电脑的CUP***(每台电脑***不同),然后对***进行加密,这样就能保证软件只能在注册的电脑上使用。

基于MD5 AES的软件加密系统

 

上图为整个系统的思路。 客户将AES加密后的密文发送给我,然后我在推出客户电脑的CUP***然后经过MD5加密,将加密后得到的数字相对比,一样软件可用。

直接上获取CPU***代码(网上找的)

std::string GetCpuByCmd(int len)
{
	const long MAX_COMMAND_SIZE = 10000; // 命令行输出缓冲大小     
	WCHAR szFetCmd[] = L"wmic cpu get processorid"; // 获取CPU***命令行    
	const string strEnSearch = "ProcessorId"; // CPU***的前导信息  

	BOOL   bret = FALSE;
	HANDLE hReadPipe = NULL; //读取管道  
	HANDLE hWritePipe = NULL; //写入管道      
	PROCESS_INFORMATION pi;   //进程信息      
	STARTUPINFO         si;   //控制命令行窗口信息  
	SECURITY_ATTRIBUTES sa;   //安全属性  

	char            szBuffer[MAX_COMMAND_SIZE + 1] = { 0 }; // 放置命令行结果的输出缓冲区  
	string          strBuffer;
	string         strBuffer2;
	unsigned long   count = 0;
	long            ipos = 0;

	memset(&pi, 0, sizeof(pi));
	memset(&si, 0, sizeof(si));
	memset(&sa, 0, sizeof(sa));

	pi.hProcess = NULL;
	pi.hThread = NULL;
	si.cb = sizeof(STARTUPINFO);
	sa.nLength = sizeof(SECURITY_ATTRIBUTES);
	sa.lpSecurityDescriptor = NULL;
	sa.bInheritHandle = TRUE;

	//1.0 创建管道  
	bret = CreatePipe(&hReadPipe, &hWritePipe, &sa, 0);
	if (!bret)
	{
		goto END;
	}

	//2.0 设置命令行窗口的信息为指定的读写管道  
	GetStartupInfo(&si);
	si.hStdError = hWritePipe;
	si.hStdOutput = hWritePipe;
	si.wShowWindow = SW_HIDE; //隐藏命令行窗口  
	si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;

	//3.0 创建获取命令行的进程  
	bret = CreateProcess(NULL, szFetCmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
	if (!bret)
	{
		goto END;
	}

	//4.0 读取返回的数据  
	WaitForSingleObject(pi.hProcess, 500/*INFINITE*/);
	bret = ReadFile(hReadPipe, szBuffer, MAX_COMMAND_SIZE, &count, 0);
	if (!bret)
	{
		goto END;
	}

	//5.0 查找CPU***  
	bret = FALSE;
	strBuffer = szBuffer;
	ipos = strBuffer.find(strEnSearch);

	if (ipos < 0) // 没有找到  
	{
		goto END;
	}
	else
	{
		strBuffer = strBuffer.substr(ipos + strEnSearch.length());
	}

	memset(szBuffer, 0x00, sizeof(szBuffer));
	//strcpy(szBuffer, strBuffer.c_str());


	//去掉中间的空格 \r \n  
	int j = 0;
	for (int i = 0; i < strBuffer.length(); i++)
	{
		if (strBuffer[i] == ' ' || strBuffer[i] == '\n' || strBuffer[i] == '\r')
		{
			continue;
		}
		else
			strBuffer2 += strBuffer[i];
	}

	bret = TRUE;

END:
	//关闭所有的句柄  
	CloseHandle(hWritePipe);
	CloseHandle(hReadPipe);
	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);
	return strBuffer2;


}

将CUP***通过AES算法获得加密密文。

AES加密算法的详细介绍与实现(这篇文章写的很好)

 Crypto++ 加/解密算法库 

我使用Crypto++库不错的,虽然代码看不懂但是根据上面的文章给出的办法还是得出了加密密文。

下面是MD5加密的算法代码

 

#ifndef MD5_H
#define MD5_H
#include <string>
#include <fstream>
/* Type define */

typedef unsigned char byte;
typedef unsigned long ulong;
using std::string;
using std::ifstream;
/* MD5 declaration. */

class MD5 {

public:

	MD5();

	MD5(const void *input, size_t length);

	MD5(const string &str);

	MD5(ifstream &in);

	void update(const void *input, size_t length);

	void update(const string &str);

	void update(ifstream &in);

	const byte* digest();

	string toString();

	void reset();

private:

	void update(const byte *input, size_t length);

	void final();

	void transform(const byte block[64]);

	void encode(const ulong *input, byte *output, size_t length);

	void decode(const byte *input, ulong *output, size_t length);

	string bytesToHexString(const byte *input, size_t length);



	/* class uncopyable */

	MD5(const MD5&);

	

private:

	ulong _state[4];	/* state (ABCD) */

	ulong _count[2];	/* number of bits, modulo 2^64 (low-order word first) */

	byte _buffer[64];	/* input buffer */

	byte _digest[16];	/* message digest */

	bool _finished;		/* calculate finished ? */



	static const byte PADDING[64];	/* padding for calculate */

	static const char HEX[16];

	static const size_t BUFFER_SIZE = 1024;

};



#endif/*MD5_H*/
#include "stdafx.h"
#include "md5.h"



using namespace std;



/* Constants for MD5Transform routine. */

#define S11 7

#define S12 12

#define S13 17

#define S14 22

#define S21 5

#define S22 9

#define S23 14

#define S24 20

#define S31 4

#define S32 11

#define S33 16

#define S34 23

#define S41 6

#define S42 10

#define S43 15

#define S44 21





/* F, G, H and I are basic MD5 functions.

*/

#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))

#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))

#define H(x, y, z) ((x) ^ (y) ^ (z))

#define I(x, y, z) ((y) ^ ((x) | (~z)))



/* ROTATE_LEFT rotates x left n bits.

*/

#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))



/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.

Rotation is separate from addition to prevent recomputation.

*/

#define FF(a, b, c, d, x, s, ac) { \
(a) += F((b), (c), (d)) + (x)+ac; \
(a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \
}

#define GG(a, b, c, d, x, s, ac) { \
(a) += G((b), (c), (d)) + (x)+ac; \
(a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \
}

#define HH(a, b, c, d, x, s, ac) { \
(a) += H((b), (c), (d)) + (x)+ac; \
(a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \
}

#define II(a, b, c, d, x, s, ac) { \
(a) += I((b), (c), (d)) + (x)+ac; \
(a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \
}





const byte MD5::PADDING[64] = { 0x80 };

const char MD5::HEX[16] = {

	'0', '1', '2', '3',

	'4', '5', '6', '7',

	'8', '9', 'a', 'b',

	'c', 'd', 'e', 'f'

};



/* Default construct. */

MD5::MD5() {

	reset();

}



/* Construct a MD5 object with a input buffer. */

MD5::MD5(const void *input, size_t length) {

	reset();

	update(input, length);

}



/* Construct a MD5 object with a string. */

MD5::MD5(const string &str) {

	reset();

	update(str);

}



/* Construct a MD5 object with a file. */

MD5::MD5(ifstream &in) {

	reset();

	update(in);

}



/* Return the message-digest */

const byte* MD5::digest() {

	if (!_finished) {

		_finished = true;

		final();

	}

	return _digest;

}



/* Reset the calculate state */

void MD5::reset() {



	_finished = false;

	/* reset number of bits. */

	_count[0] = _count[1] = 0;

	/* Load magic initialization constants. */

	_state[0] = 0x67452301;

	_state[1] = 0xefcdab89;

	_state[2] = 0x98badcfe;

	_state[3] = 0x10325476;

}



/* Updating the context with a input buffer. */

void MD5::update(const void *input, size_t length) {

	update((const byte*)input, length);

}



/* Updating the context with a string. */

void MD5::update(const string &str) {

	update((const byte*)str.c_str(), str.length());

}



/* Updating the context with a file. */

void MD5::update(ifstream &in) {



	if (!in)

		return;



	std::streamsize length;

	char buffer[BUFFER_SIZE];

	while (!in.eof()) {

		in.read(buffer, BUFFER_SIZE);

		length = in.gcount();

		if (length > 0)

			update(buffer, length);

	}

	in.close();

}



/* MD5 block update operation. Continues an MD5 message-digest

operation, processing another message block, and updating the

context.

*/

void MD5::update(const byte *input, size_t length) {



	ulong i, index, partLen;



	_finished = false;



	/* Compute number of bytes mod 64 */

	index = (ulong)((_count[0] >> 3) & 0x3f);



	/* update number of bits */

	if ((_count[0] += ((ulong)length << 3)) < ((ulong)length << 3))

		_count[1]++;

	_count[1] += ((ulong)length >> 29);



	partLen = 64 - index;



	/* transform as many times as possible. */

	if (length >= partLen) {



		memcpy(&_buffer[index], input, partLen);

		transform(_buffer);



		for (i = partLen; i + 63 < length; i += 64)

			transform(&input[i]);

		index = 0;



	}
	else {

		i = 0;

	}



	/* Buffer remaining input */

	memcpy(&_buffer[index], &input[i], length - i);

}



/* MD5 finalization. Ends an MD5 message-_digest operation, writing the

the message _digest and zeroizing the context.

*/

void MD5::final() {



	byte bits[8];

	ulong oldState[4];

	ulong oldCount[2];

	ulong index, padLen;



	/* Save current state and count. */

	memcpy(oldState, _state, 16);

	memcpy(oldCount, _count, 8);



	/* Save number of bits */

	encode(_count, bits, 8);



	/* Pad out to 56 mod 64. */

	index = (ulong)((_count[0] >> 3) & 0x3f);

	padLen = (index < 56) ? (56 - index) : (120 - index);

	update(PADDING, padLen);



	/* Append length (before padding) */

	update(bits, 8);



	/* Store state in digest */

	encode(_state, _digest, 16);



	/* Restore current state and count. */

	memcpy(_state, oldState, 16);

	memcpy(_count, oldCount, 8);

}



/* MD5 basic transformation. Transforms _state based on block. */

void MD5::transform(const byte block[64]) {



	ulong a = _state[0], b = _state[1], c = _state[2], d = _state[3], x[16];



	decode(block, x, 64);



	/* Round 1 */

	FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */

	FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */

	FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */

	FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */

	FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */

	FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */

	FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */

	FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */

	FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */

	FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */

	FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */

	FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */

	FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */

	FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */

	FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */

	FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */



											/* Round 2 */

	GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */

	GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */

	GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */

	GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */

	GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */

	GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */

	GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */

	GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */

	GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */

	GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */

	GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */

	GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */

	GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */

	GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */

	GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */

	GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */



											/* Round 3 */

	HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */

	HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */

	HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */

	HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */

	HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */

	HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */

	HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */

	HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */

	HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */

	HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */

	HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */

	HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */

	HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */

	HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */

	HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */

	HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */



										   /* Round 4 */

	II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */

	II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */

	II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */

	II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */

	II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */

	II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */

	II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */

	II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */

	II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */

	II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */

	II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */

	II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */

	II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */

	II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */

	II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */

	II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */



	_state[0] += a;

	_state[1] += b;

	_state[2] += c;

	_state[3] += d;

}



/* Encodes input (ulong) into output (byte). Assumes length is

a multiple of 4.

*/

void MD5::encode(const ulong *input, byte *output, size_t length) {



	for (size_t i = 0, j = 0; j < length; i++, j += 4) {

		output[j] = (byte)(input[i] & 0xff);

		output[j + 1] = (byte)((input[i] >> 8) & 0xff);

		output[j + 2] = (byte)((input[i] >> 16) & 0xff);

		output[j + 3] = (byte)((input[i] >> 24) & 0xff);

	}

}



/* Decodes input (byte) into output (ulong). Assumes length is

a multiple of 4.

*/

void MD5::decode(const byte *input, ulong *output, size_t length) {



	for (size_t i = 0, j = 0; j < length; i++, j += 4) {

		output[i] = ((ulong)input[j]) | (((ulong)input[j + 1]) << 8) |

			(((ulong)input[j + 2]) << 16) | (((ulong)input[j + 3]) << 24);

	}

}



/* Convert byte array to hex string. */

string MD5::bytesToHexString(const byte *input, size_t length) {

	string str;

	str.reserve(length << 1);

	for (size_t i = 0; i < length; i++) {

		int t = input[i];

		int a = t / 16;

		int b = t % 16;

		str.append(1, HEX[a]);

		str.append(1, HEX[b]);

	}

	return str;

}



/* Convert digest to string value */

string MD5::toString() {

	return bytesToHexString(digest(), 16);

}

 现在加密算法都有了,然后加密软件程序是MFC对话框做的,所以在MFC添加一个Dialog然后用domodal,创建非模态对话框,可以在此对话框进行判断,如果密码不对则不能使用软件功能

BOOL CMD5_MFCDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	UpdateData(FALSE);
	AESEncryption aesEncryptor; //加密器 

	unsigned char aesKey[AES::DEFAULT_KEYLENGTH];                   //**
	unsigned char inBlock[AES::BLOCKSIZE];    //要加密的数据块
	string lp;
	lp = GetCpuByCmd();
	for (int i = 0; i < lp.length(); i++)
	{
		inBlock[i] = lp[i];
	}
	
	unsigned char outBlock[AES::BLOCKSIZE];                                 //加密后的密文块
	unsigned char xorBlock[AES::BLOCKSIZE];                                 //必须设定为全零
	memset(xorBlock, 0, AES::BLOCKSIZE);                                 //置零

	aesEncryptor.SetKey(aesKey, AES::DEFAULT_KEYLENGTH);  //设定加***
	aesEncryptor.ProcessAndXorBlock(inBlock, xorBlock, outBlock); //加密
	
	string str;
	for (int i = 0; i < AES::BLOCKSIZE; i++)
	{
		str += to_string(outBlock[i]) + "-";
	}
	
	
	
	
	CCODE code;
	code.m_TZM = str.c_str();
	UpdateData(FALSE);
	code.DoModal();
	

	
	
	
	
	
	
	
	

	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

下面是解密代码 解密客户传来的AES密文

void CRegisterDlg::OnBnClickedRegister()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);

	unsigned char aesKey[AES::DEFAULT_KEYLENGTH];                   //**
	int  inBlock[AES::BLOCKSIZE] ;
	inBlock[0] = m_Num0;
	inBlock[1] = m_Num1;
	inBlock[2] = m_Num2;
	inBlock[3] = m_Num3;
	inBlock[4] = m_Num4;
	inBlock[5] = m_Num5;
	inBlock[6] = m_Num6;
	inBlock[7] = m_Num7;
	inBlock[8] = m_Num8;
	inBlock[9] = m_Num9;
	inBlock[10] = m_Num10;
	inBlock[11] = m_Num11;
	inBlock[12] = m_Num12;
	inBlock[13] = m_Num13;
	inBlock[14] = m_Num14;
	inBlock[15] = m_Num15;
	unsigned char outBlock[AES::BLOCKSIZE];//加密后的密文块
	unsigned char xorBlock[AES::BLOCKSIZE];                                 //必须设定为全零
	memset(xorBlock, 0, AES::BLOCKSIZE);                                 //置零
	for (int i = 0; i < AES::BLOCKSIZE; i++)
	{
		outBlock[i] = inBlock[i];
	}

	


	AESDecryption aesDecryptor;
	unsigned char plainText[AES::BLOCKSIZE];
	aesDecryptor.SetKey(aesKey, AES::DEFAULT_KEYLENGTH);
	aesDecryptor.ProcessAndXorBlock(outBlock, xorBlock, plainText);
	string str;
	for (int i = 0; i < AES::BLOCKSIZE ; i++)
	{
		str += plainText[i];
	}
	
	m_zhuce = str.c_str();

	 	string str2(CW2A(m_zhuce.GetString()));
		MD5 md5(str2);
	 	str2 =md5.toString();
		m_zhuce1 = str2.c_str();
	 	b_judge = false;
	
	

// 	for (int i = 0; i < lp.length(); i++) { cout << plainText[i]; }
// 	cout << endl;
	UpdateData(FALSE);


}

 上面的代码自己写的 有点蠢不过能力有限,慢慢加油。

基于MD5 AES的软件加密系统

基于MD5 AES的软件加密系统

 

这期间遇到的问题就是AES加密后得到的unsigned char outBlock[16]数组中的数据传不到string 中然后加了 to_string才好用

 

 

相关标签: AES MD5