最簡單的加密---異或加密

 

/*************************************************
*IDE:VS2017
*Author:Rise
*Project:XOR 加密解密
**************************************************/
#include <stdio.h>

/***********************
*cipherttext 密文
*plaintext	明文
*textlen	文本長度
*key		密鑰
********************/

void EN_XOR(unsigned char *ciphertext, unsigned char* Plaintext, unsigned char textlen, unsigned char key)
{
	int i = 0;
	for (i = 0; i < textlen; i++)
	{
		ciphertext[i] = Plaintext[i] ^ key;
	}

}

void DE_XOR(unsigned char *ciphertext, unsigned char* Plaintext, unsigned char textlen, unsigned char key)
{
	int i = 0;
	for (i = 0; i < textlen; i++)
	{
		Plaintext[i] = ciphertext[i] ^ key;
	}
}

void test()
{
	int i = 0;
	unsigned char plaint[10] = { 0xaa,0x55,0x44,0x22,0xff,0xcc,0x88,0x99,0x56,0x65 };
	unsigned char ciphert[10];
	unsigned char key = 0xA5;

	EN_XOR(ciphert, plaint, 10, key);

	for (i = 0; i < 10; i++)
	{
		printf("plaint[%d] 0x%02x\n", i, plaint[i]);
	}
	printf("\n");

	for (i = 0; i < 10; i++)
	{
		printf("ciphert[%d] 0x%02x\n", i, ciphert[i]);
	}
	printf("\n");

	DE_XOR(ciphert, plaint, 10, key);
	for (i = 0; i < 10; i++)
	{
		printf("plaint[%d] 0x%02x\n", i, plaint[i]);
	}
	printf("\n");
}


int main()
{
	test();
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章