Obfuscate Data
Another technique to protect memory from being modified is to obfuscate data. Obfuscating or encrypting key data is possible through various means. Before writing to memory, either obfuscate a variable or encrypt it. There are various techniques for achieving this, ranging from a simple XOR to more complex encryption methods. If you are combining this with a duplication technique you could even hash the duplicate and incorporate the hash check into the verify logic.
Obfuscating your data will ensure that it is harder for a cheater to find key variables in memory.
Example Code
// Make sure to xor the initial score
- (id)init {
self = [super init];
if (self) {
xorCode_ = 12345; // USE A DIFFERENT XOR VALUE THAN THIS!
score_ = score_ ^ xorCode_;
}
}
// Functions and mutators should xor the value before using it, and xor
// the value again before the function exits
- (void) addScore: (int) increase {
score_ = score_ ^ xorCode_;
score_ += increase;
score_ = score_ ^ xorCode_;
}
// Accessors just need to return the xor'd value
- (int) getScore {
return score_ ^ xorCode_;
}
private int xorCode = 12345; // USE A DIFFERENT XOR VALUE THAN THIS!
private int score = 0;
// Make sure to xor the initial score
@Override
private void onCreate(Bundle savedInstanceState) {
score = score ^ xorCode;
}
// Functions and mutators should xor the value before using it, and xor
// the value again before the function exits
private void addScore(int increase) {
score ^= xorCode;
score += increase;
score ^= xorCode;
}
// Accessors just need to return the xor'd value
private int getScore () {
return score ^ xorCode;
}
private int xorCode = 12345; // USE A DIFFERENT XOR VALUE THAN THIS!
private int score = 0;
// Make sure to xor the initial score
void Awake() {
score = score ^ xorCode;
}
// Functions and mutators should xor the value before using it, and xor
// the value again before the function exits
private void AddScore(int increase) {
score ^= xorCode;
score += increase;
score ^= xorCode;
}
// Accessors just need to return the xor'd value
private int GetScore () {
return score ^ xorCode;
}