From fcb2cb1d892aaa502f3980c2516d5a9bccf11213 Mon Sep 17 00:00:00 2001 From: walon Date: Sun, 11 May 2025 12:53:24 +0800 Subject: [PATCH] =?UTF-8?q?Encryption=E6=94=B9=E5=90=8D=E4=B8=BAEncryption?= =?UTF-8?q?VM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EncryptionInstructionWithOpCode.cs | 2 +- .../IEncryptionInstruction.cs | 2 +- .../Instructions/AddInstruction.cs | 2 +- .../Instructions/BitRotateInstruction.cs | 2 +- .../Instructions/EncryptFunction.cs | 2 +- .../Instructions/XorInstruction.cs | 2 +- .../VirtualMachine.cs} | 6 +++--- .../VirtualMachineCreator.cs} | 16 +++++++--------- .../VirtualMachineSimulator.cs} | 7 +++---- Editor/Obfuscator.cs | 8 ++++---- 10 files changed, 23 insertions(+), 26 deletions(-) rename Editor/{Encryption => EncryptionVM}/EncryptionInstructionWithOpCode.cs (95%) rename Editor/{Encryption => EncryptionVM}/IEncryptionInstruction.cs (92%) rename Editor/{Encryption => EncryptionVM}/Instructions/AddInstruction.cs (93%) rename Editor/{Encryption => EncryptionVM}/Instructions/BitRotateInstruction.cs (95%) rename Editor/{Encryption => EncryptionVM}/Instructions/EncryptFunction.cs (95%) rename Editor/{Encryption => EncryptionVM}/Instructions/XorInstruction.cs (93%) rename Editor/{Encryption/EncryptionVirtualMachine.cs => EncryptionVM/VirtualMachine.cs} (61%) rename Editor/{Encryption/EncryptionVirtualMachineCreator.cs => EncryptionVM/VirtualMachineCreator.cs} (77%) rename Editor/{Encryption/EncryptionVirtualMachineSimulator.cs => EncryptionVM/VirtualMachineSimulator.cs} (89%) diff --git a/Editor/Encryption/EncryptionInstructionWithOpCode.cs b/Editor/EncryptionVM/EncryptionInstructionWithOpCode.cs similarity index 95% rename from Editor/Encryption/EncryptionInstructionWithOpCode.cs rename to Editor/EncryptionVM/EncryptionInstructionWithOpCode.cs index c31aa3a..19ded45 100644 --- a/Editor/Encryption/EncryptionInstructionWithOpCode.cs +++ b/Editor/EncryptionVM/EncryptionInstructionWithOpCode.cs @@ -1,4 +1,4 @@ -namespace Obfuz.Encryption +namespace Obfuz.EncryptionVM { public class EncryptionInstructionWithOpCode { diff --git a/Editor/Encryption/IEncryptionInstruction.cs b/Editor/EncryptionVM/IEncryptionInstruction.cs similarity index 92% rename from Editor/Encryption/IEncryptionInstruction.cs rename to Editor/EncryptionVM/IEncryptionInstruction.cs index bb3b667..c1765ca 100644 --- a/Editor/Encryption/IEncryptionInstruction.cs +++ b/Editor/EncryptionVM/IEncryptionInstruction.cs @@ -1,4 +1,4 @@ -namespace Obfuz.Encryption +namespace Obfuz.EncryptionVM { public interface IEncryptionInstruction { diff --git a/Editor/Encryption/Instructions/AddInstruction.cs b/Editor/EncryptionVM/Instructions/AddInstruction.cs similarity index 93% rename from Editor/Encryption/Instructions/AddInstruction.cs rename to Editor/EncryptionVM/Instructions/AddInstruction.cs index 2b3de74..e8af5b9 100644 --- a/Editor/Encryption/Instructions/AddInstruction.cs +++ b/Editor/EncryptionVM/Instructions/AddInstruction.cs @@ -1,4 +1,4 @@ -namespace Obfuz.Encryption.Instructions +namespace Obfuz.EncryptionVM.Instructions { public class AddInstruction : EncryptionInstructionBase { diff --git a/Editor/Encryption/Instructions/BitRotateInstruction.cs b/Editor/EncryptionVM/Instructions/BitRotateInstruction.cs similarity index 95% rename from Editor/Encryption/Instructions/BitRotateInstruction.cs rename to Editor/EncryptionVM/Instructions/BitRotateInstruction.cs index dab8252..c3271ed 100644 --- a/Editor/Encryption/Instructions/BitRotateInstruction.cs +++ b/Editor/EncryptionVM/Instructions/BitRotateInstruction.cs @@ -1,4 +1,4 @@ -namespace Obfuz.Encryption.Instructions +namespace Obfuz.EncryptionVM.Instructions { public class BitRotateInstruction : EncryptionInstructionBase { diff --git a/Editor/Encryption/Instructions/EncryptFunction.cs b/Editor/EncryptionVM/Instructions/EncryptFunction.cs similarity index 95% rename from Editor/Encryption/Instructions/EncryptFunction.cs rename to Editor/EncryptionVM/Instructions/EncryptFunction.cs index 192e5f1..3fc2a35 100644 --- a/Editor/Encryption/Instructions/EncryptFunction.cs +++ b/Editor/EncryptionVM/Instructions/EncryptFunction.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Obfuz.Encryption.Instructions +namespace Obfuz.EncryptionVM.Instructions { public class EncryptFunction : EncryptionInstructionBase diff --git a/Editor/Encryption/Instructions/XorInstruction.cs b/Editor/EncryptionVM/Instructions/XorInstruction.cs similarity index 93% rename from Editor/Encryption/Instructions/XorInstruction.cs rename to Editor/EncryptionVM/Instructions/XorInstruction.cs index 4e8fcc2..60b8c9a 100644 --- a/Editor/Encryption/Instructions/XorInstruction.cs +++ b/Editor/EncryptionVM/Instructions/XorInstruction.cs @@ -1,4 +1,4 @@ -namespace Obfuz.Encryption.Instructions +namespace Obfuz.EncryptionVM.Instructions { public class XorInstruction : EncryptionInstructionBase { diff --git a/Editor/Encryption/EncryptionVirtualMachine.cs b/Editor/EncryptionVM/VirtualMachine.cs similarity index 61% rename from Editor/Encryption/EncryptionVirtualMachine.cs rename to Editor/EncryptionVM/VirtualMachine.cs index d3d1c48..d70c61b 100644 --- a/Editor/Encryption/EncryptionVirtualMachine.cs +++ b/Editor/EncryptionVM/VirtualMachine.cs @@ -1,13 +1,13 @@ -namespace Obfuz.Encryption +namespace Obfuz.EncryptionVM { - public class EncryptionVirtualMachine + public class VirtualMachine { public const int SecretKeyLength = 1024; public readonly int[] secretKey; public readonly EncryptionInstructionWithOpCode[] opCodes; - public EncryptionVirtualMachine(int[] secretKey, EncryptionInstructionWithOpCode[] opCodes) + public VirtualMachine(int[] secretKey, EncryptionInstructionWithOpCode[] opCodes) { this.secretKey = secretKey; this.opCodes = opCodes; diff --git a/Editor/Encryption/EncryptionVirtualMachineCreator.cs b/Editor/EncryptionVM/VirtualMachineCreator.cs similarity index 77% rename from Editor/Encryption/EncryptionVirtualMachineCreator.cs rename to Editor/EncryptionVM/VirtualMachineCreator.cs index c3c4c53..2ebda84 100644 --- a/Editor/Encryption/EncryptionVirtualMachineCreator.cs +++ b/Editor/EncryptionVM/VirtualMachineCreator.cs @@ -1,21 +1,19 @@ -using Obfuz.Encryption.Instructions; +using Obfuz.EncryptionVM.Instructions; using Obfuz.Utils; using UnityEngine.Assertions; -namespace Obfuz.Encryption +namespace Obfuz.EncryptionVM { - public class EncryptionVirtualMachineCreator + public class VirtualMachineCreator { private readonly int[] _vmGenerationSecretKey; - private readonly byte[] _encryptionSecretKey; private readonly IRandom _random; public const int CodeGenerationSecretKeyLength = 1024; - public EncryptionVirtualMachineCreator(string vmGenerationSecretKey, byte[] encryptionSecretKey) + public VirtualMachineCreator(string vmGenerationSecretKey, byte[] encryptionSecretKey) { _vmGenerationSecretKey = KeyGenerator.ConvertToIntKey(KeyGenerator.GenerateKey(vmGenerationSecretKey, CodeGenerationSecretKeyLength)); - _encryptionSecretKey = encryptionSecretKey; _random = new RandomWithKey(encryptionSecretKey, 0); } @@ -36,12 +34,12 @@ namespace Obfuz.Encryption private EncryptionInstructionWithOpCode CreateEncryptOpCode(ushort code) { - IEncryptionInstruction inst = CreateRandomInstruction(EncryptionVirtualMachine.SecretKeyLength / sizeof(int)); + IEncryptionInstruction inst = CreateRandomInstruction(VirtualMachine.SecretKeyLength / sizeof(int)); Assert.AreEqual(1234, inst.Decrypt(inst.Encrypt(1234, _vmGenerationSecretKey, 0x12345678), _vmGenerationSecretKey, 0x12345678)); return new EncryptionInstructionWithOpCode(code, inst); } - public EncryptionVirtualMachine CreateVirtualMachine(int opCodeCount) + public VirtualMachine CreateVirtualMachine(int opCodeCount) { Assert.IsTrue(opCodeCount > 0); Assert.AreEqual(0, opCodeCount & (opCodeCount - 1)); @@ -50,7 +48,7 @@ namespace Obfuz.Encryption { opCodes[i] = CreateEncryptOpCode((ushort)i); } - return new EncryptionVirtualMachine(_vmGenerationSecretKey, opCodes); + return new VirtualMachine(_vmGenerationSecretKey, opCodes); } } } diff --git a/Editor/Encryption/EncryptionVirtualMachineSimulator.cs b/Editor/EncryptionVM/VirtualMachineSimulator.cs similarity index 89% rename from Editor/Encryption/EncryptionVirtualMachineSimulator.cs rename to Editor/EncryptionVM/VirtualMachineSimulator.cs index 119ae93..8e43a31 100644 --- a/Editor/Encryption/EncryptionVirtualMachineSimulator.cs +++ b/Editor/EncryptionVM/VirtualMachineSimulator.cs @@ -3,19 +3,18 @@ using System.Collections.Generic; using System.Linq.Expressions; using System.Runtime.CompilerServices; using System.Text; -using Unity.Collections.LowLevel.Unsafe; using UnityEngine.Assertions; using UnityEngine.UIElements; -namespace Obfuz.Encryption +namespace Obfuz.EncryptionVM { - public class EncryptionVirtualMachineSimulator : EncryptorBase + public class VirtualMachineSimulator : EncryptorBase { private readonly EncryptionInstructionWithOpCode[] _opCodes; private readonly int[] _secretKey; - public EncryptionVirtualMachineSimulator(EncryptionVirtualMachine vm) + public VirtualMachineSimulator(VirtualMachine vm) { _opCodes = vm.opCodes; _secretKey = vm.secretKey; diff --git a/Editor/Obfuscator.cs b/Editor/Obfuscator.cs index ff643d6..be33e0a 100644 --- a/Editor/Obfuscator.cs +++ b/Editor/Obfuscator.cs @@ -2,7 +2,7 @@ using dnlib.Protection; using Obfuz.Data; using Obfuz.Emit; -using Obfuz.Encryption; +using Obfuz.EncryptionVM; using Obfuz.ObfusPasses; using Obfuz.ObfusPasses.CleanUp; using Obfuz.Utils; @@ -41,7 +41,7 @@ namespace Obfuz string obfuscatedAssemblyOutputDir, List obfuscationPasses, string rawSecretKey, int globalRandomSeed, string encryptionVmSecretKey) { - _secretKey = KeyGenerator.GenerateKey(rawSecretKey, EncryptionVirtualMachine.SecretKeyLength); + _secretKey = KeyGenerator.GenerateKey(rawSecretKey, VirtualMachine.SecretKeyLength); _globalRandomSeed = globalRandomSeed; _encryptionVmSecretKey = encryptionVmSecretKey; @@ -67,9 +67,9 @@ namespace Obfuz private IEncryptor CreateEncryptionVirtualMachine() { - var vmCreator = new EncryptionVirtualMachineCreator(_encryptionVmSecretKey, _secretKey); + var vmCreator = new VirtualMachineCreator(_encryptionVmSecretKey, _secretKey); var vm = vmCreator.CreateVirtualMachine(1); - return new EncryptionVirtualMachineSimulator(vm); + return new VirtualMachineSimulator(vm); } private void OnPreObfuscation()