diff --git a/Editor/Encryption/RvaDataAllocator.cs b/Editor/Data/RvaDataAllocator.cs similarity index 99% rename from Editor/Encryption/RvaDataAllocator.cs rename to Editor/Data/RvaDataAllocator.cs index da1959d..3140196 100644 --- a/Editor/Encryption/RvaDataAllocator.cs +++ b/Editor/Data/RvaDataAllocator.cs @@ -9,7 +9,7 @@ using System.Text; using System.Threading.Tasks; using UnityEngine.Assertions; -namespace Obfuz.Encryption +namespace Obfuz.Data { public struct RvaData { diff --git a/Editor/Emit/CompileContext.cs b/Editor/Emit/CompileContext.cs index be87221..3037196 100644 --- a/Editor/Emit/CompileContext.cs +++ b/Editor/Emit/CompileContext.cs @@ -2,7 +2,7 @@ using dnlib.DotNet.Emit; using NUnit.Framework; using Obfuz.Emit; -using Obfuz.Encryption; +using Obfuz.Data; using System.Collections.Generic; namespace Obfuz.Emit diff --git a/Editor/Emit/DataNodes/BytesInitializeFromFieldRvaDataNode.cs b/Editor/Emit/DataNodes/BytesInitializeFromFieldRvaDataNode.cs index d1e25a4..7c98f30 100644 --- a/Editor/Emit/DataNodes/BytesInitializeFromFieldRvaDataNode.cs +++ b/Editor/Emit/DataNodes/BytesInitializeFromFieldRvaDataNode.cs @@ -9,7 +9,7 @@ using System.Text; using System.Threading.Tasks; using static UnityEngine.Networking.UnityWebRequest; using UnityEngine.UIElements; -using Obfuz.Encryption; +using Obfuz.Data; namespace Obfuz.Emit { diff --git a/Editor/Emit/DataNodes/ConstFromFieldRvaDataNode.cs b/Editor/Emit/DataNodes/ConstFromFieldRvaDataNode.cs index d1a40da..a4a47c9 100644 --- a/Editor/Emit/DataNodes/ConstFromFieldRvaDataNode.cs +++ b/Editor/Emit/DataNodes/ConstFromFieldRvaDataNode.cs @@ -2,14 +2,7 @@ using dnlib.DotNet.Emit; using Obfuz.Emit; using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using static UnityEngine.Networking.UnityWebRequest; -using UnityEngine.UIElements; -using Obfuz.Encryption; +using Obfuz.Data; namespace Obfuz.Emit { diff --git a/Editor/Emit/DefaultModuleMetadataImporter.cs b/Editor/Emit/DefaultModuleMetadataImporter.cs new file mode 100644 index 0000000..4780d18 --- /dev/null +++ b/Editor/Emit/DefaultModuleMetadataImporter.cs @@ -0,0 +1,102 @@ +using dnlib.DotNet; +using System; +using UnityEngine.Assertions; + +namespace Obfuz.Emit +{ + public class DefaultModuleMetadataImporter : ModuleMetadataImporterBase + { + public DefaultModuleMetadataImporter() { } + + public override void Init(ModuleDef mod) + { + _module = mod; + var constUtilityType = typeof(ConstUtility); + + _castIntAsFloat = mod.Import(constUtilityType.GetMethod("CastIntAsFloat")); + Assert.IsNotNull(_castIntAsFloat, "CastIntAsFloat not found"); + _castLongAsDouble = mod.Import(constUtilityType.GetMethod("CastLongAsDouble")); + Assert.IsNotNull(_castLongAsDouble, "CastLongAsDouble not found"); + _castFloatAsInt = mod.Import(constUtilityType.GetMethod("CastFloatAsInt")); + Assert.IsNotNull(_castFloatAsInt, "CastFloatAsInt not found"); + _castDoubleAsLong = mod.Import(constUtilityType.GetMethod("CastDoubleAsLong")); + Assert.IsNotNull(_castDoubleAsLong, "CastDoubleAsLong not found"); + + _initializeArray = mod.Import(typeof(System.Runtime.CompilerServices.RuntimeHelpers).GetMethod("InitializeArray", new[] { typeof(Array), typeof(RuntimeFieldHandle) })); + Assert.IsNotNull(_initializeArray); + _encryptBlock = mod.Import(typeof(EncryptionService).GetMethod("EncryptBlock", new[] { typeof(byte[]), typeof(long), typeof(int) })); + Assert.IsNotNull(_encryptBlock); + _decryptBlock = mod.Import(typeof(EncryptionService).GetMethod("DecryptBlock", new[] { typeof(byte[]), typeof(long), typeof(int) })); + Assert.IsNotNull(_decryptBlock); + _encryptInt = mod.Import(typeof(EncryptionService).GetMethod("Encrypt", new[] { typeof(int), typeof(int), typeof(int) })); + Assert.IsNotNull(_encryptInt); + _decryptInt = mod.Import(typeof(EncryptionService).GetMethod("Decrypt", new[] { typeof(int), typeof(int), typeof(int) })); + Assert.IsNotNull(_decryptInt); + _encryptLong = mod.Import(typeof(EncryptionService).GetMethod("Encrypt", new[] { typeof(long), typeof(int), typeof(int) })); + Assert.IsNotNull(_encryptLong); + _decryptLong = mod.Import(typeof(EncryptionService).GetMethod("Decrypt", new[] { typeof(long), typeof(int), typeof(int) })); + Assert.IsNotNull(_decryptLong); + _encryptFloat = mod.Import(typeof(EncryptionService).GetMethod("Encrypt", new[] { typeof(float), typeof(int), typeof(int) })); + Assert.IsNotNull(_encryptFloat); + _decryptFloat = mod.Import(typeof(EncryptionService).GetMethod("Decrypt", new[] { typeof(float), typeof(int), typeof(int) })); + Assert.IsNotNull(_decryptFloat); + _encryptDouble = mod.Import(typeof(EncryptionService).GetMethod("Encrypt", new[] { typeof(double), typeof(int), typeof(int) })); + Assert.IsNotNull(_encryptDouble); + _decryptDouble = mod.Import(typeof(EncryptionService).GetMethod("Decrypt", new[] { typeof(double), typeof(int), typeof(int) })); + Assert.IsNotNull(_decryptDouble); + _encryptString = mod.Import(typeof(EncryptionService).GetMethod("Encrypt", new[] { typeof(string), typeof(int), typeof(int) })); + Assert.IsNotNull(_encryptString); + _decryptString = mod.Import(typeof(EncryptionService).GetMethod("DecryptString", new[] { typeof(int[]), typeof(int), typeof(int), typeof(int), typeof(int) })); + Assert.IsNotNull(_decryptString); + _encryptBytes = mod.Import(typeof(EncryptionService).GetMethod("Encrypt", new[] { typeof(byte[]), typeof(int), typeof(int), typeof(int), typeof(int) })); + Assert.IsNotNull(_encryptBytes); + _decryptBytes = mod.Import(typeof(EncryptionService).GetMethod("Decrypt", new[] { typeof(int[]), typeof(int), typeof(int), typeof(int), typeof(int) })); + Assert.IsNotNull(_decryptBytes); + } + + private ModuleDef _module; + private IMethod _castIntAsFloat; + private IMethod _castLongAsDouble; + private IMethod _castFloatAsInt; + private IMethod _castDoubleAsLong; + private IMethod _initializeArray; + + private IMethod _encryptBlock; + private IMethod _decryptBlock; + private IMethod _encryptInt; + private IMethod _decryptInt; + private IMethod _encryptLong; + private IMethod _decryptLong; + private IMethod _encryptFloat; + private IMethod _decryptFloat; + private IMethod _encryptDouble; + private IMethod _decryptDouble; + private IMethod _encryptString; + private IMethod _decryptString; + private IMethod _encryptBytes; + private IMethod _decryptBytes; + + public IMethod CastIntAsFloat => _castIntAsFloat; + public IMethod CastLongAsDouble => _castLongAsDouble; + public IMethod CastFloatAsInt => _castFloatAsInt; + public IMethod CastDoubleAsLong => _castDoubleAsLong; + + public IMethod InitializedArrayMethod => _initializeArray; + + public IMethod EncryptBlock => _encryptBlock; + public IMethod DecryptBlock => _decryptBlock; + + public IMethod EncryptInt => _encryptInt; + public IMethod DecryptInt => _decryptInt; + public IMethod EncryptLong => _encryptLong; + public IMethod DecryptLong => _decryptLong; + public IMethod EncryptFloat => _encryptFloat; + public IMethod DecryptFloat => _decryptFloat; + public IMethod EncryptDouble => _encryptDouble; + public IMethod DecryptDouble => _decryptDouble; + public IMethod EncryptString => _encryptString; + public IMethod DecryptString => _decryptString; + public IMethod EncryptBytes => _encryptBytes; + public IMethod DecryptBytes => _decryptBytes; + } +} diff --git a/Editor/Emit/MetadataImporter.cs b/Editor/Emit/MetadataImporter.cs index ae0ee7f..0176389 100644 --- a/Editor/Emit/MetadataImporter.cs +++ b/Editor/Emit/MetadataImporter.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using UnityEngine.Assertions; namespace Obfuz.Emit { @@ -18,54 +17,6 @@ namespace Obfuz.Emit public abstract void Init(ModuleDef mod); } - public class DefaultModuleMetadataImporter : ModuleMetadataImporterBase - { - public override void Init(ModuleDef mod) - { - _module = mod; - var constUtilityType = typeof(ConstUtility); - - _castIntAsFloat = mod.Import(constUtilityType.GetMethod("CastIntAsFloat")); - Assert.IsNotNull(_castIntAsFloat, "CastIntAsFloat not found"); - _castLongAsDouble = mod.Import(constUtilityType.GetMethod("CastLongAsDouble")); - Assert.IsNotNull(_castLongAsDouble, "CastLongAsDouble not found"); - _castFloatAsInt = mod.Import(constUtilityType.GetMethod("CastFloatAsInt")); - Assert.IsNotNull(_castFloatAsInt, "CastFloatAsInt not found"); - _castDoubleAsLong = mod.Import(constUtilityType.GetMethod("CastDoubleAsLong")); - Assert.IsNotNull(_castDoubleAsLong, "CastDoubleAsLong not found"); - - _initializeArray = mod.Import(typeof(System.Runtime.CompilerServices.RuntimeHelpers).GetMethod("InitializeArray", new[] { typeof(Array), typeof(RuntimeFieldHandle) })); - Assert.IsNotNull(_initializeArray); - _encryptBlock = mod.Import(typeof(EncryptionService).GetMethod("EncryptBlock", new[] { typeof(byte[]), typeof(long), typeof(int) })); - Assert.IsNotNull(_encryptBlock); - _decryptBlock = mod.Import(typeof(EncryptionService).GetMethod("DecryptBlock", new[] { typeof(byte[]), typeof(long), typeof(int) })); - Assert.IsNotNull(_decryptBlock); - } - - private ModuleDef _module; - private IMethod _castIntAsFloat; - private IMethod _castLongAsDouble; - private IMethod _castFloatAsInt; - private IMethod _castDoubleAsLong; - private IMethod _initializeArray; - private IMethod _encryptBlock; - private IMethod _decryptBlock; - - public IMethod CastIntAsFloat => _castIntAsFloat; - - public IMethod CastLongAsDouble => _castLongAsDouble; - - public IMethod CastFloatAsInt => _castFloatAsInt; - - public IMethod CastDoubleAsLong => _castDoubleAsLong; - - public IMethod InitializedArrayMethod => _initializeArray; - - public IMethod EncryptBlock => _encryptBlock; - - public IMethod DecryptBlock => _decryptBlock; - } - public class MetadataImporter { private readonly Dictionary<(ModuleDef, Type), IModuleMetadataImporter> _customModuleMetadataImporters = new Dictionary<(ModuleDef, Type), IModuleMetadataImporter>(); @@ -98,7 +49,7 @@ namespace Obfuz.Emit } else { - importer = (IModuleMetadataImporter)Activator.CreateInstance(typeof(T), module); + importer = (IModuleMetadataImporter)Activator.CreateInstance(typeof(T)); } importer.Init(module); _customModuleMetadataImporters[key] = importer; diff --git a/Editor/ObfusPasses/ConstObfus/ConstObfusPass.cs b/Editor/ObfusPasses/ConstObfus/ConstObfusPass.cs index 94b3b55..58b6428 100644 --- a/Editor/ObfusPasses/ConstObfus/ConstObfusPass.cs +++ b/Editor/ObfusPasses/ConstObfus/ConstObfusPass.cs @@ -20,7 +20,7 @@ namespace Obfuz.ObfusPasses.ConstObfus public override void Start(ObfuscationPassContext ctx) { _dataObfuscatorPolicy = new RuleBasedObfuscationPolicy(); - _dataObfuscator = new DefaultDataObfuscator(); + _dataObfuscator = new DefaultConstObfuscator(); } public override void Stop(ObfuscationPassContext ctx) diff --git a/Editor/ObfusPasses/ConstObfus/DefaultConstObfuscator.cs b/Editor/ObfusPasses/ConstObfus/DefaultConstObfuscator.cs new file mode 100644 index 0000000..ef579cc --- /dev/null +++ b/Editor/ObfusPasses/ConstObfus/DefaultConstObfuscator.cs @@ -0,0 +1,132 @@ +using dnlib.DotNet; +using dnlib.DotNet.Emit; +using Obfuz.Emit; +using Obfuz.Data; +using Obfuz.Utils; +using System; +using System.Collections.Generic; +using NUnit.Framework; + +namespace Obfuz.ObfusPasses.ConstObfus +{ + public class DefaultConstObfuscator : IDataObfuscator + { + private readonly IRandom _random; + private readonly RandomDataNodeCreator _nodeCreator; + private readonly RvaDataAllocator _rvaDataAllocator; + private readonly ConstFieldAllocator _constFieldAllocator; + private readonly IEncryptor _encryptor; + + public DefaultConstObfuscator() + { + _random = new RandomWithKey(new byte[] { 0x1, 0x2, 0x3, 0x4 }, 0x5); + _encryptor = new DefaultEncryptor(new byte[] { 0x1A, 0x2B, 0x3C, 0x4D }); + _nodeCreator = new RandomDataNodeCreator(_random); + _rvaDataAllocator = new RvaDataAllocator(_random, _encryptor); + _constFieldAllocator = new ConstFieldAllocator(_random); + } + + private void CompileNode(IDataNode node, MethodDef method, List obfuscatedInstructions) + { + var ctx = new CompileContext + { + method = method, + output = obfuscatedInstructions, + rvaDataAllocator = _rvaDataAllocator, + constFieldAllocator = _constFieldAllocator, + }; + node.Compile(ctx); + } + + private int GenerateEncryptionOperations() + { + return _random.NextInt(); + } + + public int GenerateSalt() + { + return _random.NextInt(); + } + + private DefaultModuleMetadataImporter GetModuleMetadataImporter(MethodDef method) + { + return MetadataImporter.Instance.GetDefaultModuleMetadataImporter(method.Module); + } + + public void ObfuscateInt(MethodDef method, int value, List obfuscatedInstructions) + { + int ops = GenerateEncryptionOperations(); + int salt = GenerateSalt(); + int encryptedValue = _encryptor.Encrypt(value, ops, salt); + RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue); + + DefaultModuleMetadataImporter importer = GetModuleMetadataImporter(method); + obfuscatedInstructions.Add(Instruction.CreateLdcI4(encryptedValue)); + obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops)); + obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt)); + obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptInt)); + } + + public void ObfuscateLong(MethodDef method, long value, List obfuscatedInstructions) + { + int ops = GenerateEncryptionOperations(); + int salt = GenerateSalt(); + long encryptedValue = _encryptor.Encrypt(value, ops, salt); + + DefaultModuleMetadataImporter importer = GetModuleMetadataImporter(method); + obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I8, encryptedValue)); + obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops)); + obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt)); + obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptLong)); + } + + public void ObfuscateFloat(MethodDef method, float value, List obfuscatedInstructions) + { + int ops = GenerateEncryptionOperations(); + int salt = GenerateSalt(); + float encryptedValue = _encryptor.Encrypt(value, ops, salt); + + DefaultModuleMetadataImporter importer = GetModuleMetadataImporter(method); + obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R4, encryptedValue)); + obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops)); + obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt)); + obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFloat)); + } + + public void ObfuscateDouble(MethodDef method, double value, List obfuscatedInstructions) + { + int ops = GenerateEncryptionOperations(); + int salt = GenerateSalt(); + double encryptedValue = _encryptor.Encrypt(value, ops, salt); + DefaultModuleMetadataImporter importer = GetModuleMetadataImporter(method); + obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R8, encryptedValue)); + obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops)); + obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt)); + obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptDouble)); + } + + public void ObfuscateBytes(MethodDef method, byte[] value, List obfuscatedInstructions) + { + int ops = GenerateEncryptionOperations(); + int salt = GenerateSalt(); + + Assert.IsTrue(value.Length % 4 == 0); + int[] intArr = new int[value.Length / 4]; + Buffer.BlockCopy(value, 0, intArr, 0, value.Length); + byte[] encryptedValue = _encryptor.Decrypt(intArr, 0, value.Length, ops, salt); + } + + public void ObfuscateString(MethodDef method, string value, List obfuscatedInstructions) + { + IDataNode node = _nodeCreator.CreateRandom(DataNodeType.String, value); + CompileNode(node, method, obfuscatedInstructions); + //obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldstr, value)); + } + + public void Done() + { + _rvaDataAllocator.Done(); + _constFieldAllocator.Done(); + } + } +} diff --git a/Editor/ObfusPasses/ConstObfus/DefaultDataObfuscator.cs b/Editor/ObfusPasses/ConstObfus/DefaultDataObfuscator.cs deleted file mode 100644 index 5b7a208..0000000 --- a/Editor/ObfusPasses/ConstObfus/DefaultDataObfuscator.cs +++ /dev/null @@ -1,89 +0,0 @@ -using dnlib.DotNet; -using dnlib.DotNet.Emit; -using Obfuz.Emit; -using Obfuz.Encryption; -using Obfuz.Utils; -using System; -using System.Collections.Generic; - -namespace Obfuz.ObfusPasses.ConstObfus -{ - public class DefaultDataObfuscator : IDataObfuscator - { - private readonly IRandom _random; - private readonly RandomDataNodeCreator _nodeCreator; - private readonly RvaDataAllocator _rvaDataAllocator; - private readonly ConstFieldAllocator _constFieldAllocator; - private readonly IEncryptor _encryptor; - - public DefaultDataObfuscator() - { - _random = new RandomWithKey(new byte[] { 0x1, 0x2, 0x3, 0x4 }, 0x5); - _encryptor = new DefaultEncryptor(new byte[] { 0x1A, 0x2B, 0x3C, 0x4D }); - _nodeCreator = new RandomDataNodeCreator(_random); - _rvaDataAllocator = new RvaDataAllocator(_random, _encryptor); - _constFieldAllocator = new ConstFieldAllocator(_random); - } - - private void CompileNode(IDataNode node, MethodDef method, List obfuscatedInstructions) - { - var ctx = new CompileContext - { - method = method, - output = obfuscatedInstructions, - rvaDataAllocator = _rvaDataAllocator, - constFieldAllocator = _constFieldAllocator, - }; - node.Compile(ctx); - } - - public void ObfuscateInt(MethodDef method, int value, List obfuscatedInstructions) - { - IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Int32, value); - CompileNode(node, method, obfuscatedInstructions); - //obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I4, value)); - } - - public void ObfuscateLong(MethodDef method, long value, List obfuscatedInstructions) - { - IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Int64, value); - CompileNode(node, method, obfuscatedInstructions); - //obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I8, value)); - } - - public void ObfuscateFloat(MethodDef method, float value, List obfuscatedInstructions) - { - IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Float32, value); - CompileNode(node, method, obfuscatedInstructions); - //obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R4, value)); - } - - public void ObfuscateDouble(MethodDef method, double value, List obfuscatedInstructions) - { - IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Float64, value); - CompileNode(node, method, obfuscatedInstructions); - //obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R8, value)); - } - - public void ObfuscateBytes(MethodDef method, Array value, List obfuscatedInstructions) - { - IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Bytes, value); - CompileNode(node, method, obfuscatedInstructions); - //throw new NotSupportedException(); - //obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I4, value.Length)); - } - - public void ObfuscateString(MethodDef method, string value, List obfuscatedInstructions) - { - IDataNode node = _nodeCreator.CreateRandom(DataNodeType.String, value); - CompileNode(node, method, obfuscatedInstructions); - //obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldstr, value)); - } - - public void Done() - { - _rvaDataAllocator.Done(); - _constFieldAllocator.Done(); - } - } -} diff --git a/Editor/ObfusPasses/ConstObfus/IDataObfuscator.cs b/Editor/ObfusPasses/ConstObfus/IDataObfuscator.cs index e3307ca..19ef939 100644 --- a/Editor/ObfusPasses/ConstObfus/IDataObfuscator.cs +++ b/Editor/ObfusPasses/ConstObfus/IDataObfuscator.cs @@ -17,7 +17,7 @@ namespace Obfuz.ObfusPasses.ConstObfus void ObfuscateString(MethodDef method, string value, List obfuscatedInstructions); - void ObfuscateBytes(MethodDef method, Array value, List obfuscatedInstructions); + void ObfuscateBytes(MethodDef method, byte[] value, List obfuscatedInstructions); void Done(); } diff --git a/Runtime/ConstUtility.cs b/Runtime/ConstUtility.cs index 209661e..643d3d8 100644 --- a/Runtime/ConstUtility.cs +++ b/Runtime/ConstUtility.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Text; using Unity.Collections.LowLevel.Unsafe; using UnityEngine; +using UnityEngine.Assertions; namespace Obfuz { @@ -41,6 +42,14 @@ namespace Obfuz return result; } + public static int[] GetInts(byte[] data, int offset, int byteLength) + { + Assert.IsTrue(byteLength % 4 == 0); + int[] result = new int[byteLength >> 2]; + Buffer.BlockCopy(data, offset, result, 0, byteLength); + return result; + } + public static void InitializeArray(Array array, byte[] data, int offset, int length) { Buffer.BlockCopy(data, offset, array, 0, length); diff --git a/Runtime/DefaultEncryptor.cs b/Runtime/DefaultEncryptor.cs index 2e2056d..df44ff1 100644 --- a/Runtime/DefaultEncryptor.cs +++ b/Runtime/DefaultEncryptor.cs @@ -30,5 +30,72 @@ namespace Obfuz data[i] ^= (byte)(_key[i % _key.Length] ^ salt); } } + + public int Encrypt(int value, int opts, int salt) + { + return value; + } + + public int Decrypt(int value, int opts, int salt) + { + return value; + } + + public long Encrypt(long value, int opts, int salt) + { + return value; + } + + public long Decrypt(long value, int opts, int salt) + { + return value; + } + + public float Encrypt(float value, int opts, int salt) + { + return value; + } + + public float Decrypt(float value, int opts, int salt) + { + return value; + } + + public double Encrypt(double value, int opts, int salt) + { + return value; + } + + public double Decrypt(double value, int opts, int salt) + { + return value; + } + + public int[] Encrypt(byte[] value, int offset, int length, int opts, int salt) + { + var intArr = new int[(length + 3) / 4]; + Buffer.BlockCopy(value, offset, intArr, 0, length); + return intArr; + } + + public byte[] Decrypt(int[] value, int offset, int byteLength, int ops, int salt) + { + byte[] byteArr = new byte[byteLength]; + Buffer.BlockCopy(value, 0, byteArr, 0, byteLength); + return byteArr; + } + + public int[] Encrypt(string value, int ops, int salt) + { + byte[] bytes = Encoding.UTF8.GetBytes(value); + return Encrypt(bytes, 0, bytes.Length, ops, salt); + } + + public string DecryptString(int[] value, int offset, int stringBytesLength, int ops, int salt) + { + byte[] bytes = new byte[stringBytesLength]; + Buffer.BlockCopy(value, 0, bytes, 0, stringBytesLength); + return Encoding.UTF8.GetString(bytes); + } } } diff --git a/Runtime/EncryptionService.cs b/Runtime/EncryptionService.cs index a0167e4..f4e0308 100644 --- a/Runtime/EncryptionService.cs +++ b/Runtime/EncryptionService.cs @@ -19,5 +19,96 @@ namespace Obfuz { _encryptor.DecryptBlock(data, ops, salt); } + + public static int Encrypt(int value, int opts, int salt) + { + return _encryptor.Encrypt(value, opts, salt); + } + + public static int Decrypt(int value, int opts, int salt) + { + return _encryptor.Decrypt(value, opts, salt); + } + + public static long Encrypt(long value, int opts, int salt) + { + return _encryptor.Encrypt(value, opts, salt); + } + + public static long Decrypt(long value, int opts, int salt) + { + return _encryptor.Decrypt(value, opts, salt); + } + + public static float Encrypt(float value, int opts, int salt) + { + return _encryptor.Encrypt(value, opts, salt); + } + + public static float Decrypt(float value, int opts, int salt) + { + return _encryptor.Decrypt(value, opts, salt); + } + + public static double Encrypt(double value, int opts, int salt) + { + return _encryptor.Encrypt(value, opts, salt); + } + + public static double Decrypt(double value, int opts, int salt) + { + return _encryptor.Decrypt(value, opts, salt); + } + + public static int[] Encrypt(byte[] value, int offset, int length, int opts, int salt) + { + return _encryptor.Encrypt(value, offset, length, opts, salt); + } + + public static byte[] Decrypt(int[] value, int offset, int byteLength, int ops, int salt) + { + return _encryptor.Decrypt(value, offset, byteLength, ops, salt); + } + + public static int[] Encrypt(string value, int ops, int salt) + { + return _encryptor.Encrypt(value, ops, salt); + } + + public static string DecryptString(int[] value, int offset, int stringBytesLength, int ops, int salt) + { + return _encryptor.DecryptString(value, offset, stringBytesLength, ops, salt); + } + + + public static int DecryptFromRvaInt(byte[] data, int offset, int ops, int salt) + { + int encryptedValue = ConstUtility.GetInt(data, offset); + return Decrypt(encryptedValue, ops, salt); + } + + public static long DecryptFromRvaLong(byte[] data, int offset, int ops, int salt) + { + long encryptedValue = ConstUtility.GetLong(data, offset); + return Decrypt(encryptedValue, ops, salt); + } + + public static float DecryptFromRvaFloat(byte[] data, int offset, int ops, int salt) + { + int encryptedValue = ConstUtility.GetInt(data, offset); + return Decrypt(encryptedValue, ops, salt); + } + + public static double DecryptFromRvaDouble(byte[] data, int offset, int ops, int salt) + { + long encryptedValue = ConstUtility.GetLong(data, offset); + return Decrypt(encryptedValue, ops, salt); + } + + public static string DecryptFromRvaString(byte[] data, int offset, int stringBytesLength, int ops, int salt) + { + int[] encryptedValue = ConstUtility.GetBytes(data, offset, stringBytesLength); + return DecryptString(encryptedValue, 0, stringBytesLength, ops, salt); + } } } diff --git a/Runtime/IEncryptor.cs b/Runtime/IEncryptor.cs index 8ede0b1..ebe54a1 100644 --- a/Runtime/IEncryptor.cs +++ b/Runtime/IEncryptor.cs @@ -10,5 +10,23 @@ namespace Obfuz { void EncryptBlock(byte[] data, long ops, int salt); void DecryptBlock(byte[] data, long ops, int salt); + + int Encrypt(int value, int opts, int salt); + int Decrypt(int value, int opts, int salt); + + long Encrypt(long value, int opts, int salt); + long Decrypt(long value, int opts, int salt); + + float Encrypt(float value, int opts, int salt); + float Decrypt(float value, int opts, int salt); + + double Encrypt(double value, int opts, int salt); + double Decrypt(double value, int opts, int salt); + + int[] Encrypt(byte[] value, int offset, int length, int opts, int salt); + public byte[] Decrypt(int[] value, int offset, int byteLength, int ops, int salt); + + public int[] Encrypt(string value, int ops, int salt); + public string DecryptString(int[] value, int offset, int stringBytesLength, int ops, int salt); } }