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);
}结果:
基本思想就是设置一个密码,然后与真实数据进行异或操作。
为了使用方便,可以对运算符进行重载,这样就可以像使用基本数据类型一样使用了。
直接上代码:
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#控制台基础 返回类型为string的 int 类型参数的委托与拉姆塔表达式
- [C#基础] 数据类型
- C#语言基础之数据类型
- 自学笔记三:C#语法基础 数据类型之引用类型
- C#控制台基础 获取一维int类型数组的最大值,最小值
- Java基本数据类型与封装类型详解(int和Integer区别)
- C语言基础学习基本数据类型-int类型与int变量
- (C#基础) 数据类型
- python基础-基本数据类型总结_整型(int)_字符型(str)_day3
- java基本数据类型与封装类型详解(int和Integer区别)
- 使用 NSData 分类实现,对 NSData 数据类型进行 AES 加密
- C#基础--之数据类型
- C# int、long、double、char等各种内置数据类型的字节数、最大值、最小值
- JavaEE_Mybatis_SpringMVC_整合开发_数据绑定_int/Integer等 基础类型自动拆装包中的小问题
- C# 对 App.config的appSettings节点数据进行加密
- 对Map里的数据进行封装(数据类型和判空处理)
- C# 将基础数据类型转化成字节
- C#基础-数据类型转换
- Safe C++ 读书笔记:对基本数据类型进行封装,以编写更安全的C++代码
- C#基础——数据类型