From 6bc9192c13c5176139afb0a67190dfdf3a541fcc Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 23 Apr 2025 18:43:14 +0800 Subject: [PATCH] =?UTF-8?q?RvaData=E6=94=AF=E6=8C=81=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/Emit/RvaDataAllocator.cs | 15 ++++++++++----- .../DataNodes/ConstFromFieldRvaDataNode.cs | 2 +- Runtime/EncryptionService.cs | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 Runtime/EncryptionService.cs diff --git a/Editor/Emit/RvaDataAllocator.cs b/Editor/Emit/RvaDataAllocator.cs index 95ab5f0..3882c27 100644 --- a/Editor/Emit/RvaDataAllocator.cs +++ b/Editor/Emit/RvaDataAllocator.cs @@ -39,6 +39,7 @@ namespace Obfuz.Emit public FieldDef runtimeValueField; public uint size; public List bytes; + public int minorSecret; public void FillPadding() { @@ -118,6 +119,7 @@ namespace Obfuz.Emit runtimeValueField = runtimeValueField, size = dataHolderType.ClassSize, bytes = new List((int)dataHolderType.ClassSize), + minorSecret = _random.NextInt(), }; _rvaFields.Add(newRvaField); return newRvaField; @@ -222,9 +224,10 @@ namespace Obfuz.Emit cctor.Body = body; var ins = body.Instructions; - IMethod method = mod.Import(typeof(System.Runtime.CompilerServices.RuntimeHelpers).GetMethod("InitializeArray", new[] { typeof(Array), typeof(RuntimeFieldHandle) })); - - Assert.IsNotNull(method); + IMethod initializeArrayMethod = mod.Import(typeof(System.Runtime.CompilerServices.RuntimeHelpers).GetMethod("InitializeArray", new[] { typeof(Array), typeof(RuntimeFieldHandle) })); + IMethod decryptArrayMethod = mod.Import(typeof(EncryptionService).GetMethod("DecryptBytes", new[] { typeof(byte[]), typeof(int) })); + + Assert.IsNotNull(initializeArrayMethod); foreach (var field in _rvaFields) { // ldc @@ -236,11 +239,13 @@ namespace Obfuz.Emit ins.Add(Instruction.Create(OpCodes.Ldc_I4, (int)field.size)); ins.Add(Instruction.Create(OpCodes.Newarr, field.runtimeValueField.FieldType.Next.ToTypeDefOrRef())); ins.Add(Instruction.Create(OpCodes.Dup)); + ins.Add(Instruction.Create(OpCodes.Dup)); ins.Add(Instruction.Create(OpCodes.Stsfld, field.runtimeValueField)); ins.Add(Instruction.Create(OpCodes.Ldtoken, field.holderDataField)); - ins.Add(Instruction.Create(OpCodes.Call, method)); + ins.Add(Instruction.Create(OpCodes.Call, initializeArrayMethod)); + ins.Add(Instruction.Create(OpCodes.Ldc_I4, field.minorSecret)); + ins.Add(Instruction.Create(OpCodes.Call, decryptArrayMethod)); - // TODO Decrpyt } ins.Add(Instruction.Create(OpCodes.Ret)); } diff --git a/Editor/Virtualization/DataNodes/ConstFromFieldRvaDataNode.cs b/Editor/Virtualization/DataNodes/ConstFromFieldRvaDataNode.cs index c5edc10..d889b1f 100644 --- a/Editor/Virtualization/DataNodes/ConstFromFieldRvaDataNode.cs +++ b/Editor/Virtualization/DataNodes/ConstFromFieldRvaDataNode.cs @@ -44,7 +44,7 @@ namespace Obfuz.Virtualization private static IMethod s_convertDouble; private static IMethod s_convertString; - private static IField s_Encoding_Utf8; + //private static IField s_Encoding_Utf8; private static IMethod s_convertBytes; private void InitImportMetadatas(ModuleDef mod) diff --git a/Runtime/EncryptionService.cs b/Runtime/EncryptionService.cs new file mode 100644 index 0000000..104f786 --- /dev/null +++ b/Runtime/EncryptionService.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz +{ + public static class EncryptionService + { + public static void DecryptBytes(byte[] data, int minorSecret) + { + + } + } +}