首页 > 代码库 > C/C++ 字符串加密 可打印文本加密

C/C++ 字符串加密 可打印文本加密

今天做了一个文本加密算法, 由于是这个算法是基于算法保密的,没有秘钥,所有只能应付安全要求不高的需求,


//strOut必须为可打印字符0x20 -- 0x7F
//strOut必须以'\0'结尾,
//加密效果较好 By Sols
// 加密结果为可打印字符0x20 -- 0x7F
void EnCodeStr(char *strOut)
{
	int randOffset = rand()%96;
	int ResChar ;
	int i=0;
	for (;strOut[i] != '\0';i++)
	{
		ResChar = randOffset + strOut[i] + i*17;//17可以为其他合适的数字
		while ( ResChar > 127)
		{
			ResChar -= 96;
		}
		strOut[i] = (char)ResChar;
	}
	strOut[i++] =(char)( randOffset + 32 );
	strOut[i] = '\0';
}
//strOut必须为可打印字符 0x20 -- 0x7F
//strOut必须以'\0'结尾,
void DeCodeStr(char *strCode)
{
	int KeyIndex = 0;
	if (strCode[0] == '\0')  return;
	while( strCode[KeyIndex] != '\0')
	{
		KeyIndex++;
	}
	int Key = (int)(strCode[KeyIndex-1]) - 32;
	int ResChar ;
	int i=0;
	for ( ;strCode[i] != '\0';i++)
	{
		ResChar = strCode[i] - Key - i*17;
		while ( ResChar < 32)
		{
			ResChar += 96;
		}
		strCode[i] = (char)ResChar;
	}
	strCode[i-1] = '\0';
}

C/C++ 字符串加密 可打印文本加密