以下函數源於網絡上的開源算法,因爲我要在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;
}
以上函數可以根據實際情況做相應改動,變化爲自己所需的版本。