Skip to content

USecurity is a security and anti-cheat component used in the Unity project.

License

Notifications You must be signed in to change notification settings

ls9512/USecurity

Repository files navigation

USecurity

USecurity is a security component used in the Unity project. It provides encryption of commonly used data types at runtime, PlayerPrefs storage encryption, and a quick call interface for encryption and decryption of common encryption algorithms.

topLanguage size issue license last 996.icu

[中文文档]

1. Quick Start

  1. Copy the folder to the UnityProject/Assets/Plugins/ directory.
  2. Set the key file in the Resources/ subdirectory of each type of encryption needed under the EncDec/ directory, and set the encryption key you want, or click Create Key in the component menu to generate a random key.
  3. If the project does not use the Resources.Load method to load resources, you can adjust the storage path of the key configuration file by yourself and replace the interface implementation of USecurityInterface.Load, or you can do secondary encryption on the key resource file.

2. Anti Cheat

2.1. Anti Cheat Value - Runtime Data Encryption

2.1.1. Introduction

AntiCheatValue is the core function of the USecurity component. It provides runtime encrypted data types corresponding to the native data types of C# and Unity. In most cases, these data types can directly replace the original native data in the project. Type, and use the same style and method as the original type, which can be efficiently accessed so that existing projects can quickly obtain runtime anti-cheating capabilities.

2.1.2. C# Type

Anti Cheat Type Source Tyoe
cBool bool
cByte byte
cChar char
cDecimal decimal
cFloat float
cInt int
cLong long
cShort short
cString string

Provides most commonly used C# data types, and all implement the interfaces of IComparable, IFormattable, IConvertible, IEquatable, IFormattable, IEnumerable, ICloneable, etc. contained in the native type, and implement ISerializable `Interface to support serialized storage. All types implement implicit conversion and common operator overloading with corresponding primitive types.

2.1.3. Unity Type

Anti Cheat Type Source Tyoe
cColor Color
cQuaternion Quaternion
cVector2 Vector2
cVector3 Vector3
cVector4 Vector4

The Unity encrypted data type is a secondary encapsulation based on the C# encryption type. For example, cVector3 essentially encapsulates 3 cFloat, so it can be combined with actual needs to expand other complex encrypted data types.

2.1.4. Working Principle

By encapsulating the native type, the upper-level business logic accesses and manipulates the encapsulated type without directly operating the actual internal value. When the encapsulation type is operated, the real value passed in will be processed through encryption algorithms, memory offset, etc., and the processing methods for different data types are different. It can effectively avoid conventional cheating methods for searching and locating memory data.

2.1.5. How To Use

// Directly replace the native type
public cInt num = 1;

// Mutual assignment operations with native types
public int a = 1;
public cInt b = a;
a = b;

// Perform regular mathematical operations directly with native types
public int left = 1;
public cInt right = 1;
right++;
public int add = left + right;

// Routine mathematical operations between different encryption types
public cInt num1 = 1;
public cFloat num2 = 1f;
float result = num1 * num2;

// Compare with native type value
public cInt i1 = 1;
public int i2 = 1;
public cInt i3 = 1;
public Debug.Log(i1 == i2);
public Debug.Log(i1 == i3);

// Support using the Convert interface for type conversion
public cInt srcValue = 1;
public int dstValue = Convert.ToInt32(srcValue);

2.1.6. Attention

  • Since the dynamic encryption key is regenerated every time it runs, the same actual value will be different every time it runs, so the internal value should not be used for storage.
  • Encryption operations have a certain performance overhead. Use simple numeric encryption types as much as possible and apply only to important data as much as possible.

2.2. PlayerPrefsAES

2.2.1. Introduction

Provides AES encryption and decryption package for PlayerPrefs archive function, the style is consistent with the native interface, and some additional access interfaces for commonly used data types are added, and PlayerPrefs can be used as the main storage Directly replace in the project of the method to quickly obtain storage encryption capabilities.

2.2.2. How To Use

// Save value
PlayerPrefsAES.SetInt("Key", 100);

// Load value
var value = PlayerPrefsAES.GetInt("Key");

// Delete value
PlayerPrefsAES.DeleteKey("Key");

// Delete all data
PlayerPrefsAES.DeleteAll();

// Save & Write data
PlayerPrefsAES.Save();

3. DFA - Sensitive Word Filtering

3.1. Introduction

DFA is the full name Deterministic Finite Automaton, which is a more commonly used sensitive word filtering algorithm. This component compares and filters the user text input in the project with the sensitive word dictionary.

3.2. How To Use

// Set to replace sensitive characters
DFAUtil.ReplaceSymbol = "*";

// Set the thesaurus file separator
DFAUtil.Separator = new[] { ",", "\n", "\r" };

// Initialize with pre-prepared thesaurus text
var text = Resources.Load<TextAsset>("DFA").text;
DFAUtil.Init(text);

// Filter input content
var content = ".....";
var result = DFAUtil.FilterWords(content, out var isLimit);
Debug.Log(isLimit);
Debug.Log(result);

4. Encrypt / Decrypt

4.1. AES

Provides AES encryption and decryption of strings and files, with faster speed and higher security.

4.2. DES

Provides DES encryption and decryption of strings and files, which is slower and less secure.

4.3. RC4

Provide RC4 encryption and decryption of strings, the fastest speed, security is not guaranteed, and the password can be variable length.

4.4. RSA

It provides RSA signature and verification, encryption and decryption of strings and files, which is slower and has the highest security. The password is generated by the program.

4.5. Base64

Provide base64 encoding and decoding of text.


5. MD5

Provides standard MD5 value and short MD5 value calculation for text and files.