Anti-Cheat Toolkit  2024.0.0
Multi-purpose anti-cheat solution for Unity Engine
ObscuredString Class Reference

Inherits IEquatable< ObscuredString >, IEquatable< string >, IComparable< ObscuredString >, IComparable< string >, IComparable, and IObscuredType.

Public Member Functions

string Substring (int startIndex)
 
string Substring (int startIndex, int length)
 
bool StartsWith (string value, StringComparison comparisonType=StringComparison.CurrentCulture)
 
bool EndsWith (string value, StringComparison comparisonType=StringComparison.CurrentCulture)
 
char[] GetEncrypted (out char[] key)
 
void SetEncrypted (char[] encrypted, char[] key)
 
void SetEncrypted (string encrypted, string key)
 
string GetDecrypted ()
 
char[] GetDecryptedToChars ()
 
void RandomizeCryptoKey ()
 

Static Public Member Functions

static bool operator== (ObscuredString a, ObscuredString b)
 
static bool operator!= (ObscuredString a, ObscuredString b)
 
static char[] Encrypt (string value, string key)
 
static char[] Encrypt (string value, char[] key)
 
static char[] Encrypt (char[] value, char[] key)
 
static string Decrypt (char[] value, string key)
 
static string Decrypt (char[] value, char[] key)
 
static ObscuredString FromEncrypted (char[] encrypted, char[] key)
 
static ObscuredString FromEncrypted (string encrypted, string key="4441")
 
static char[] GenerateKey ()
 
static void GenerateKey (ref char[] arrayToFill)
 

Properties

char this[int index] [get]
 

Detailed Description

Use it instead of regular string for any cheating-sensitive variables.

Regular type is faster and memory wiser comparing to the obscured one!
Feel free to use regular types for all short-term operations and calculations while keeping obscured type only at the long-term declaration (i.e. class field).

Member Function Documentation

◆ operator==()

static bool operator== ( ObscuredString  a,
ObscuredString  b 
)
inlinestatic

Determines whether two specified ObscuredStrings have the same value.

Returns
true if the value of a is the same as the value of b ; otherwise, false.
Parameters
aAn ObscuredString or null.
bAn ObscuredString or null.

◆ operator!=()

static bool operator!= ( ObscuredString  a,
ObscuredString  b 
)
inlinestatic

Determines whether two specified ObscuredStrings have different values.

Returns
true if the value of a is different from the value of b ; otherwise, false.
Parameters
aAn ObscuredString or null.
bAn ObscuredString or null.

◆ Substring() [1/2]

string Substring ( int  startIndex)
inline

Proxy to the String API. Please consider avoiding using this in a hot path since it invokes decryption on every access call.

◆ Substring() [2/2]

string Substring ( int  startIndex,
int  length 
)
inline

Proxy to the String API. Please consider avoiding using this in a hot path since it invokes decryption on every access call.

◆ StartsWith()

bool StartsWith ( string  value,
StringComparison  comparisonType = StringComparison.CurrentCulture 
)
inline

Proxy to the String API. Please consider avoiding using this in a hot path since it invokes decryption on every access call.

◆ EndsWith()

bool EndsWith ( string  value,
StringComparison  comparisonType = StringComparison.CurrentCulture 
)
inline

Proxy to the String API. Please consider avoiding using this in a hot path since it invokes decryption on every access call.

◆ Encrypt() [1/3]

static char [] Encrypt ( string  value,
string  key 
)
inlinestatic

Encrypts passed value using passed key.

Key can be generated automatically using GenerateKey().

See also
Decrypt(), GenerateKey()

◆ Encrypt() [2/3]

static char [] Encrypt ( string  value,
char[]  key 
)
inlinestatic

Encrypts passed value using passed key.

Key can be generated automatically using GenerateKey().

See also
Decrypt(), GenerateKey()

◆ Encrypt() [3/3]

static char [] Encrypt ( char[]  value,
char[]  key 
)
inlinestatic

Encrypts passed value using passed key.

Key can be generated automatically using GenerateKey().

See also
Decrypt(), GenerateKey()

◆ Decrypt() [1/2]

static string Decrypt ( char[]  value,
string  key 
)
inlinestatic

Decrypts passed value you got from Encrypt() using same key.

See also
Encrypt()

◆ Decrypt() [2/2]

static string Decrypt ( char[]  value,
char[]  key 
)
inlinestatic

Decrypts passed value you got from Encrypt() using same key.

See also
Encrypt()

◆ FromEncrypted() [1/2]

static ObscuredString FromEncrypted ( char[]  encrypted,
char[]  key 
)
inlinestatic

Creates and fills obscured variable with raw encrypted value previously got from GetEncrypted().

Literally does same job as SetEncrypted() but makes new instance instead of filling existing one, making it easier to initialize new variables from saved encrypted values.

Parameters
encryptedRaw encrypted value you got from GetEncrypted().
keyEncryption key you've got from GetEncrypted().
Returns
New obscured variable initialized from specified encrypted value.
See also
GetEncrypted(), SetEncrypted()

◆ FromEncrypted() [2/2]

static ObscuredString FromEncrypted ( string  encrypted,
string  key = "4441" 
)
inlinestatic

Use this only to decrypt data encrypted with previous ACTk versions.

Please use FromEncrypted(char[], char[]) in other cases.

◆ GenerateKey() [1/2]

static char [] GenerateKey ( )
inlinestatic

Generates random key in new allocated array. Used internally and can be used to generate key for manual Encrypt() calls.

Returns
Key suitable for manual Encrypt() calls.

◆ GenerateKey() [2/2]

static void GenerateKey ( ref char[]  arrayToFill)
inlinestatic

Generates random key. Used internally and can be used to generate key for manual Encrypt() calls.

Parameters
arrayToFillPreallocated char array. Only first 7 bytes are filled.

◆ GetEncrypted()

char [] GetEncrypted ( out char[]  key)
inline

Allows to pick current obscured value as is.

Parameters
keyEncryption key needed to decrypt returned value.
Returns
Encrypted value as is.

Use it in conjunction with SetEncrypted().
Useful for saving data in obscured state.

See also
FromEncrypted(), SetEncrypted()

◆ SetEncrypted() [1/2]

void SetEncrypted ( char[]  encrypted,
char[]  key 
)
inline

Allows to explicitly set current obscured value. Crypto key should be same as when encrypted value was got with GetEncrypted().

Use it in conjunction with GetEncrypted().
Useful for loading data stored in obscured state.

See also
FromEncrypted()

◆ SetEncrypted() [2/2]

void SetEncrypted ( string  encrypted,
string  key 
)
inline

Use this only to decrypt data encrypted with previous ACTk versions.

Please use SetEncrypted(char[], char[]) in other cases.

◆ GetDecrypted()

string GetDecrypted ( )
inline

Alternative to the type cast, use if you wish to get decrypted value but can't or don't want to use cast to the regular type.

Returns
Decrypted value.

◆ GetDecryptedToChars()

char [] GetDecryptedToChars ( )
inline

GC-friendly alternative to the type cast, use if you wish to get decrypted value but can't or don't want to use cast to the regular type.

Returns
Decrypted value as a raw chars array in case you don't wish to allocate new string.

◆ RandomizeCryptoKey()

void RandomizeCryptoKey ( )
inline

Allows to change current crypto key to the new random value and re-encrypt variable using it. Use it for extra protection against 'unknown value' search. Just call it sometimes when your variable doesn't change to fool the cheater.

WARNING: produces some GC allocations, be careful when using it!

Implements IObscuredType.

Property Documentation

◆ this[int index]

char this[int index]
get

Proxy to the String API. Please consider avoiding using this in a hot path since it invokes decryption on every access call.