您的位置:首页 > 编程语言 > C#

c#中对int等基础数据类型进行封装加密

2018-01-31 19:04 495 查看
为了防止游戏过程中被第三方应用搜索到真实数据信息,我们需要对基础数据类型进行二次封装并加密。

基本思想就是设置一个密码,然后与真实数据进行异或操作。

为了使用方便,可以对运算符进行重载,这样就可以像使用基本数据类型一样使用了。

直接上代码:

public struct AHInt
{
private static int cryptoKey = 123456;

private int currentCryptoKey;
private int hiddenValue;

public int v
{
get { return InternalEncryptDecrypt(); }
set { currentCryptoKey = cryptoKey; hiddenValue = EncryptDecrypt(value); }
}

public AHInt(int value)
{
currentCryptoKey = cryptoKey;
hiddenValue = EncryptDecrypt(value);
}

public static void SetNewCryptoKey(int newKey)
{
cryptoKey = newKey;
}

public int GetEncrypted()
{
if (currentCryptoKey != cryptoKey)
{
hiddenValue = InternalEncryptDecrypt();
hiddenValue = EncryptDecrypt(hiddenValue, cryptoKey);
currentCryptoKey = cryptoKey;
}
return hiddenValue;
}

public void SetEncrypted(int encrypted)
{
hiddenValue = encrypted;
}

public static int EncryptDecrypt(int value)
{
return EncryptDecrypt(value, 0);
}

public static int EncryptDecrypt(int value, int key)
{
if (key == 0)
{
return value ^ cryptoKey;
}
else
{
return value ^ key;
}
}

private int InternalEncryptDecrypt()
{
int key = cryptoKey;

if (currentCryptoKey != cryptoKey)
{
key = currentCryptoKey;
}

return EncryptDecrypt(hiddenValue, key);
}

// Operators

public static implicit operator AHInt(int value)
{
return new AHInt(value);
}

public static implicit operator int(AHInt value)
{
return value.v;
}

public static AHInt operator ++ (AHInt input)
{
input.hiddenValue = EncryptDecrypt(input.InternalEncryptDecrypt() + 1);
return input;
}

public static AHInt operator -- (AHInt input)
{
input.hiddenValue = EncryptDecrypt(input.InternalEncryptDecrypt() - 1);
return input;
}

public static AHInt operator + (AHInt a, AHInt b) { return new AHInt(a.v + b.v); }
public static AHInt operator - (AHInt a, AHInt b) { return new AHInt(a.v - b.v); }
public static AHInt operator * (AHInt a, AHInt b) { return new AHInt(a.v * b.v); }
public static AHInt operator / (AHInt a, AHInt b) { return new AHInt(a.v / b.v); }

public static AHInt operator + (AHInt a, int b) { return new AHInt(a.v + b); }
public static AHInt operator - (AHInt a, int b) { return new AHInt(a.v - b); }
public static AHInt operator * (AHInt a, int b) { return new AHInt(a.v * b); }
public static AHInt operator / (AHInt a, int b) { return new AHInt(a.v / b); }

public static AHInt operator + (int a, AHInt b) { return new AHInt(a + b.v); }
public static AHInt operator - (int a, AHInt b) { return new AHInt(a - b.v); }
public static AHInt operator * (int a, AHInt b) { return new AHInt(a * b.v); }
public static AHInt operator / (int a, AHInt b) { return new AHInt(a / b.v); }

public static int operator * (AHInt a, float b) { return (int)(a.v * b); } //@

public static bool operator < (AHInt a, AHInt b) { return a.v < b.v; }
public static bool operator <= (AHInt a, AHInt b) { return a.v <= b.v; }
public static bool operator > (AHInt a, AHInt b) { return a.v > b.v; }
public static bool operator >= (AHInt a, AHInt b) { return a.v >= b.v; }

public static bool operator < (AHInt a, int b) { return a.v < b; }
public static bool operator <= (AHInt a, int b) { return a.v <= b; }
public static bool operator > (AHInt a, int b) { return a.v > b; }
public static bool operator >= (AHInt a, int b) { return a.v >= b; }

public override bool Equals(object o)
{
if (o is AHInt)
{
AHInt b = (AHInt)o;
return this.v == b.v;
}
else
{
return false;
}
}

public bool Equals(AHInt b)
{
return (object)b != null && v == b.v;
}

public override int GetHashCode()
{
return v.GetHashCode();
}

public override string ToString()
{
return InternalEncryptDecrypt().ToString();
}

public string ToString(string format)
{
return InternalEncryptDecrypt().ToString(format);
}

#if !UNITY_FLASH
public string ToString(IFormatProvider provider)
{
return InternalEncryptDecrypt().ToString(provider);
}

public string ToString(string format, IFormatProvider provider)
{
return InternalEncryptDecrypt().ToString(format, provider);
}
#endif
}测试:
public static void AHIntTest(){
AHInt test = 1;
test += 2;
int x = 3;
test += x;
Debug.Log("[AHIntTest] : " + test);
}结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# unity 加密