UE4下的某些保密字段的加密处理

以下函数源于网络上的开源算法,因为我要在Unreal4引擎下使用,所以就将其简单处理了下,使得该函数能在UE4下正常使用:以下是函数示例:


#define C1 52845
#define C2 22719
/* 加解密复杂度乱数(数字越大,加密后的字符越乱,越复杂*/
#define COMPLEXITY_NUM 65

/*秘钥*/

WORD Key = 1314;

加密函数:

FString Encrypt(FString Src, WORD Key)
{
FString Result, str;
int32 i, j;
Result = Src;
for (i = 0; i < Src.Len(); i++)
{
Result[i] = Src[i] ^ (Key >> 8);

Key = ((BYTE)Result[i] + Key)*C1 + C2;

}


Src = Result;

Result.Empty();


for (i = 0; i < Src.Len(); i++)

{

j = (BYTE)Src[i];

str = "12";

str[0] = COMPLEXITY_NUM + j / 26;

str[1] = COMPLEXITY_NUM + j % 26;

Result += str;

}

return Result;

}

解密函数:

FString Decrypt(FString Des, WORD Key)
{

FString Result, str;

int32 i, j;


Result.Empty();

for (i = 0; i < Des.Len() / 2; i++)

{

j = ((BYTE)Des[2 * i] - COMPLEXITY_NUM) * 26;

j += (BYTE)Des[2 * i + 1] - COMPLEXITY_NUM;

str = "1";

str[0] = j;

Result += str;

}


Des = Result;

for (i = 0; i < Des.Len(); i++)

{

Result[i] = (BYTE)Des[i] ^ (Key >> 8);

Key = ((BYTE)Des[i] + Key)*C1 + C2;

}

return Result;

}


以上函数可以根据实际情况做相应改动,变化为自己所需的版本。


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