socket加解密(c++)

116 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

其实加解密也就是用一下方法而已,并不是说一定得理解其中的原理,当然如果你对这个感兴趣,那么可以深入仔细的研究一下整个过程。 下面就简单的展示一下代码:

void ByteArray::EncodeMessage( char* buf,size_t size )
{
	int * bb = (int*) buf;
	size_t s = size/4;
	size_t end = s/2;
	int t = 0;
	for( size_t i = 0;i < end;i ++ )
	{
		t = bb[i];
		bb[i] = bb[s-1-i]^0xA6E839CD;
		bb[s-1-i] = t^0xA6E839CD;
	}
	if( s % 2 == 1 )
	{
		bb[end] = bb[end]^0xA6E839CD;
	}
}
 
//解密
void ByteArray::DecodeMessage( char* buf,size_t size )
{
	int * bb = (int*) buf;
	size_t s = size/4;
	size_t end = s/2;
	int t = 0;
	for( size_t i = 0;i < end;i ++ )
	{
		t = bb[i];
		bb[i] = bb[s-1-i]^0xA6E839CD;
		bb[s-1-i] = t^0xA6E839CD;
	}
	if( s % 2 == 1 )
	{
		bb[end] = bb[end]^0xA6E839CD;
	}
}

下面的是我在网上看到比较详细的解释了:

20200429091942802.png 这些就是我的个人理解了,希望对你有点启发。