版本:2.4.3
参考:
修改内存工具,类似以前玩仙剑奇侠传的修改器金手指之类,查找金币1000,然后金币改变到1200,再查找1200。根据多次查找锁定金币的内存位置,然后修改为99999。
可以将保存的关键数据进行异或后保存,取出来时才经过异或获取。或者和参考中一样使用md5等方式将数据改变后再保存,避开内存修改器直接查找数值来进行修改。
@ccclass
export default class test_setShader extends cc.Component {
public num:number = 999;
public key:number = 0;
onLoad() {
this.key = this.num*Math.random();
this.num = this.num^this.key;
console.log(this.num); //随机值:164、128、117等
console.log(this.num^this.key); //999
}
}
工具类
class EncodeNumber {
//单例
private static instance: EncodeNumber;
public static get ins(): EncodeNumber {
if (EncodeNumber.instance == null) {
EncodeNumber.instance = new EncodeNumber();
}
return EncodeNumber.instance;
}
private encodeNumberId: number = 0; //唯一id
private oriValue = {}; //存放原始值
private secret = {}; //存放密钥
private encryptValue = {}; //存放加密后的值
/**
* 修改对象所有number属性set和get方法,在获取和存储值时进行加密和解密
* @param obj 需要修改的对象
*/
public encode(obj: any) {
if (obj.encodeNumberId == null) {
obj.encodeNumberId = ++this.encodeNumberId + "";
}
const propertyNames: string[] = Object.getOwnPropertyNames(obj);
for (const propName of propertyNames) {
const value = obj[propName];
if (typeof value === 'number') {
console.log("属性名:", propName);
Object.defineProperty(obj, propName, {
get: () => {
const key = obj.encodeNumberId + propName;
if (this.encryptValue[key] != null && this.secret[key] != null) {
console.log("get", propName, this.oriValue[key], this.secret[key], this.encryptValue[key]);
if ((this.oriValue[key] ^ this.secret[key]) != this.encryptValue[key]) {
console.log("值被篡改");
return 0;
} else {
console.log("值正常");
return this.encryptValue[key] ^ this.secret[key];
}
} else {
console.log("没有初始值", value);
return value;
}
},
set: (value) => {
const key = obj.encodeNumberId + propName;
this.oriValue[key] = value;
this.secret[key] = Math.random() * 1000;
this.encryptValue[key] = this.secret[key] ^ value;
console.log("set", propName, this.oriValue[key], this.secret[key], this.encryptValue[key]);
}
});
}
}
}
/**
* 清理对象的存储数据
* @param obj
*/
public clear(obj: any) {
if (obj.encodeNumberId == null) {
return;
}
console.log("清理前:", this.oriValue, this.secret, this.encryptValue);
const propertyNames: string[] = Object.getOwnPropertyNames(obj);
for (const propName of propertyNames) {
const value = obj[propName];
if (typeof value === 'number') {
console.log("属性名:", propName);
const key = obj.encodeNumberId + propName;
delete this.oriValue[key];
delete this.secret[key];
delete this.encryptValue[key];
}
}
console.log("清理后:", this.oriValue, this.secret, this.encryptValue);
}
}
使用
class Test {
public gold: number = 999;
public strList: string[] = ["a", "b", "c"];
public diamond: number = 10;
}
const test: Test = new Test();
EncodeNumber.ins.encode(test);
test.gold += 1;
console.log("拥有金币:", test.gold); //拥有金币1000
用一个自定义类的方式。 新创建一个自定义number类来进行异或。
class SecureValue {
private oriValue: number; //原始值
private encryptedValue: number; //加密后值
private key: number = Math.floor(Math.random() * 1000); //随机密钥
constructor(initialValue: number) {
this.oriValue = initialValue;
this.encryptedValue = initialValue ^ this.key; //加密:异或运算
}
get value(): number {
if ((this.oriValue ^ this.key) != this.encryptedValue) {
console.log("数据被篡改,发出警告提示");
}
console.log("解密获取:", this.oriValue, this.encryptedValue, this.key);
return this.encryptedValue ^ this.key; //解密
}
set value(newValue: number) {
this.oriValue = newValue;
this.encryptedValue = newValue ^ this.key; //重新加密
console.log("加密保存:", this.oriValue, this.encryptedValue, this.key);
}
}
使用
private gold: SecureValue = new SecureValue(999); this.gold.value += 1;
浙公网安备 33010602011771号