From ca3c15c44e1e8569daaacd95ef5e660aeda4255f Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 14 May 2025 11:05:32 +0800 Subject: [PATCH] =?UTF-8?q?AddInstruction=E5=92=8C=20XorInstruction?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E9=9D=9E=E7=BA=BF=E6=80=A7=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=20field=20encryption=E5=87=BA=E7=8E=B0=20=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=E6=95=88=E6=9E=9C=E4=B8=BA0=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/EncryptionVM/Instructions/AddInstruction.cs | 8 ++++---- Editor/EncryptionVM/Instructions/XorInstruction.cs | 8 ++++---- Editor/EncryptionVM/VirtualMachineSimulator.cs | 2 ++ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Editor/EncryptionVM/Instructions/AddInstruction.cs b/Editor/EncryptionVM/Instructions/AddInstruction.cs index f6c31f3..c75d943 100644 --- a/Editor/EncryptionVM/Instructions/AddInstruction.cs +++ b/Editor/EncryptionVM/Instructions/AddInstruction.cs @@ -14,22 +14,22 @@ namespace Obfuz.EncryptionVM.Instructions } public override int Encrypt(int value, int[] secretKey, int salt) { - return value + secretKey[_opKeyIndex] + salt + _addValue; + return (value ^ secretKey[_opKeyIndex] ^ salt) + _addValue; } public override int Decrypt(int value, int[] secretKey, int salt) { - return value - secretKey[_opKeyIndex] - salt - _addValue; + return (value - _addValue) ^ secretKey[_opKeyIndex] ^ salt; } public override void GenerateEncryptCode(List lines, string indent) { - lines.Add(indent + $"value += _secretKey[{_opKeyIndex}] + salt + {_addValue};"); + lines.Add(indent + $"value = (value ^ _secretKey[{_opKeyIndex}] ^ salt) + {_addValue};"); } public override void GenerateDecryptCode(List lines, string indent) { - lines.Add(indent + $"value -= _secretKey[{_opKeyIndex}] + salt + {_addValue};"); + lines.Add(indent + $"value = (value - {_addValue}) ^ _secretKey[{_opKeyIndex}] ^ salt;"); } } } diff --git a/Editor/EncryptionVM/Instructions/XorInstruction.cs b/Editor/EncryptionVM/Instructions/XorInstruction.cs index ed26756..a10d526 100644 --- a/Editor/EncryptionVM/Instructions/XorInstruction.cs +++ b/Editor/EncryptionVM/Instructions/XorInstruction.cs @@ -15,22 +15,22 @@ namespace Obfuz.EncryptionVM.Instructions public override int Encrypt(int value, int[] secretKey, int salt) { - return value ^ secretKey[_opKeyIndex] ^ salt ^ _xorValue; + return (value + secretKey[_opKeyIndex] + salt) ^ _xorValue; } public override int Decrypt(int value, int[] secretKey, int salt) { - return value ^ secretKey[_opKeyIndex] ^ salt ^ _xorValue; + return (value ^ _xorValue) - secretKey[_opKeyIndex] - salt; } public override void GenerateEncryptCode(List lines, string indent) { - lines.Add(indent + $"value ^= _secretKey[{_opKeyIndex}] ^ salt ^ {_xorValue};"); + lines.Add(indent + $"value = (value + _secretKey[{_opKeyIndex}] + salt) ^ {_xorValue};"); } public override void GenerateDecryptCode(List lines, string indent) { - lines.Add(indent + $"value ^= _secretKey[{_opKeyIndex}] ^ salt ^ {_xorValue};"); + lines.Add(indent + $"value = (value ^ {_xorValue}) - _secretKey[{_opKeyIndex}] - salt;"); } } } diff --git a/Editor/EncryptionVM/VirtualMachineSimulator.cs b/Editor/EncryptionVM/VirtualMachineSimulator.cs index 6f39359..43e1804 100644 --- a/Editor/EncryptionVM/VirtualMachineSimulator.cs +++ b/Editor/EncryptionVM/VirtualMachineSimulator.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq.Expressions; using System.Runtime.CompilerServices; using System.Text; +using UnityEngine; using UnityEngine.Assertions; using UnityEngine.UIElements; @@ -32,6 +33,7 @@ namespace Obfuz.EncryptionVM { int encryptedValue = _opCodes[i].Encrypt(value, _secretKey, i); int decryptedValue = _opCodes[i].Decrypt(encryptedValue, _secretKey, i); + Debug.Log($"instruction type:{_opCodes[i].function.GetType()}"); Assert.AreEqual(value, decryptedValue); }