代码改变世界

异或运算加密的c语言实现

2018-03-07 11:15  eternalwanglu  阅读(1523)  评论(0编辑  收藏  举报
void trans_encrypt(unsigned char * data,int length)  //XOR encryption algorithm
{
	int i,j,temp1,temp2;
	unsigned char key[32] = {0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f};  //XOR encryption algorithm: symmetric encryption, encryption and decryption keys are the same

	temp1 = length / 32;
	temp2 = length % 32;
	for(i = 0;i < temp1;i++)
	{
		for(j = 0;j < 32;j++)
		{
			data[i*32+j] ^= key[j];	// 异或
		}
	}
	for(j = 0;j < temp2;j++)
	{
		data[i*32+j] ^=  key[j];		
	}
}