c 中對int等基礎資料型別進行封裝加密

2021-08-15 06:15:24 字數 2518 閱讀 1926

為了防止遊戲過程中被第三方應用搜尋到真實資料資訊,我們需要對基礎資料型別進行二次封裝並加密。

基本思想就是設定乙個密碼,然後與真實資料進行異或操作。

為了使用方便,可以對運算子進行過載,這樣就可以像使用基本資料型別一樣使用了。

直接上**:

public struct ahint

set

}public ahint(int value)

public static void setnewcryptokey(int newkey)

public int getencrypted()

return hiddenvalue;

}public void setencrypted(int encrypted)

public static int encryptdecrypt(int value)

public static int encryptdecrypt(int value, int key)

else

}private int internalencryptdecrypt()

return encryptdecrypt(hiddenvalue, key);

}// operators

public static implicit operator ahint(int value)

public static implicit operator int(ahint value)

public static ahint operator ++ (ahint input)

public static ahint operator -- (ahint input)

public static ahint operator + (ahint a, ahint b)

public static ahint operator - (ahint a, ahint b)

public static ahint operator * (ahint a, ahint b)

public static ahint operator / (ahint a, ahint b)

public static ahint operator + (ahint a, int b)

public static ahint operator - (ahint a, int b)

public static ahint operator * (ahint a, int b)

public static ahint operator / (ahint a, int b)

public static ahint operator + (int a, ahint b)

public static ahint operator - (int a, ahint b)

public static ahint operator * (int a, ahint b)

public static ahint operator / (int a, ahint b)

public static int operator * (ahint a, float b) //@

public static bool operator < (ahint a, ahint b)

public static bool operator <= (ahint a, ahint b)

public static bool operator > (ahint a, ahint b)

public static bool operator >= (ahint a, ahint b)

public static bool operator < (ahint a, int b)

public static bool operator <= (ahint a, int b)

public static bool operator > (ahint a, int b)

public static bool operator >= (ahint a, int b)

public override bool equals(object o)

else

}public bool equals(ahint b)

public override int gethashcode()

public override string tostring()

public string tostring(string format)

#if !unity_flash

public string tostring(iformatprovider provider)

public string tostring(string format, iformatprovider provider)

#endif

}

測試:

public static void ahinttest()
結果:

char與int等資料型別的強制轉化

1.首先char與int都分為signed與unsigned型別,預設情況下都是signed型別。2.從長位元組資料型別轉換為短位元組資料型別,會產生截斷 如從4位元組的int型別轉換成1個位元組的char型別,則取int資料的最低的乙個位元組,將這個位元組的資料賦給char型資料,且是有符號的,即...

SQL SERVER資料型別與C 資料型別對照表

sql server型別 c 型別 精確數字 bigint 從 2 63 9223372036854775808 到 2 63 1 9223372036854775807 的整型資料 所有數字 儲存大小為 8 個位元組。int64 int從 2 31 2,147,483,648 到 2 31 1 2...

SQL SERVER資料型別與C 資料型別對照表

sql server 型別 c 型別 精確數字 bigint 從 2 63 9223372036854775808 到2 63 1 9223372036854775807 的整型資料 所有數字 儲存大小為 8 個位元組。int64 int 從 2 31 2,147,483,648 到2 31 1 2...