加密与解密算法

简单的异或加解密

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>

static void encrypt(unsigned char* pBuff, int iCnt)
{
	int i = 0;
	const char* XorKeyChar = "72f80deb855afd01a8ef0d5e6455ecb8ed8eda";
	const char* AddKeyChar = "d3b52f85fd2fe9b86cb21150621ee25c113508";

	if (iCnt > 8)
	{
		for (i = 0; i < (iCnt / 8); i++)
		{
			encrypt(pBuff + i * 8, 8);
		}
		encrypt(pBuff + i * 8, (iCnt % 8));
	}
	else
	{
		for (i = 0; i < iCnt; i++)
		{
			pBuff[i] = pBuff[i] ^ XorKeyChar[i];
			pBuff[i] = ~pBuff[i];
			pBuff[i] = pBuff[i] - AddKeyChar[i];
		}
	}
}

void decrypt(unsigned char* pBuff, int iCnt)
{
	int i = 0;
	const char* XorKeyChar = "72f80deb855afd01a8ef0d5e6455ecb8ed8eda";
	const char* AddKeyChar = "d3b52f85fd2fe9b86cb21150621ee25c113508";

	if (iCnt > 8)
	{
		for (i = 0; i < (iCnt / 8); i++)
		{
			decrypt(pBuff + i * 8, 8);
		}
		decrypt(pBuff + i * 8, (iCnt % 8));
	}
	else
	{
		for (i = 0; i < iCnt; i++)
		{
			pBuff[i] = pBuff[i] + AddKeyChar[i];
			pBuff[i] = ~pBuff[i];
			pBuff[i] = pBuff[i] ^ XorKeyChar[i];
		}
	}
}

int main() {
	unsigned char data[] = "hello world";

	printf("data : %s\n", data);

	encrypt(data, strlen((const char *)data));
	printf("encrypt data : %s\n", data);

	decrypt(data, strlen((const char*)data));
	printf("decrypt data : %s\n", data);

	return 0;
}
0%