2025-05-21 09:23:29 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
|
|
|
|
using Obfuz.Data;
|
2025-05-30 13:32:29 +08:00
|
|
|
|
using Obfuz.Emit;
|
|
|
|
|
using Obfuz.Settings;
|
2025-05-21 09:23:29 +08:00
|
|
|
|
using Obfuz.Utils;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2025-07-23 19:34:42 +08:00
|
|
|
|
using UnityEngine.Assertions;
|
2025-05-21 09:23:29 +08:00
|
|
|
|
|
|
|
|
|
namespace Obfuz.ObfusPasses.ConstEncrypt
|
|
|
|
|
{
|
|
|
|
|
public class DefaultConstEncryptor : IConstEncryptor
|
|
|
|
|
{
|
|
|
|
|
private readonly GroupByModuleEntityManager _moduleEntityManager;
|
2025-05-23 12:47:57 +08:00
|
|
|
|
private readonly ConstEncryptionSettingsFacade _settings;
|
2025-05-21 09:23:29 +08:00
|
|
|
|
|
2025-07-02 18:57:53 +08:00
|
|
|
|
public DefaultConstEncryptor(GroupByModuleEntityManager moduleEntityManager, ConstEncryptionSettingsFacade settings)
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
|
|
|
|
_moduleEntityManager = moduleEntityManager;
|
2025-05-23 12:47:57 +08:00
|
|
|
|
_settings = settings;
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IRandom CreateRandomForValue(EncryptionScopeInfo encryptionScope, int value)
|
|
|
|
|
{
|
|
|
|
|
return encryptionScope.localRandomCreator(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GenerateEncryptionOperations(EncryptionScopeInfo encryptionScope, IRandom random)
|
|
|
|
|
{
|
2025-05-23 12:47:57 +08:00
|
|
|
|
return EncryptionUtil.GenerateEncryptionOpCodes(random, encryptionScope.encryptor, _settings.encryptionLevel);
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GenerateSalt(IRandom random)
|
|
|
|
|
{
|
|
|
|
|
return random.NextInt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private DefaultMetadataImporter GetModuleMetadataImporter(MethodDef method)
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
return _moduleEntityManager.GetEntity<DefaultMetadataImporter>(method.Module);
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ObfuscateInt(MethodDef method, bool needCacheValue, int value, List<Instruction> obfuscatedInstructions)
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
EncryptionScopeInfo encryptionScope = _moduleEntityManager.EncryptionScopeProvider.GetScope(method.Module);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
2025-07-02 18:57:53 +08:00
|
|
|
|
ConstFieldAllocator constFieldAllocator = _moduleEntityManager.GetEntity<ConstFieldAllocator>(method.Module);
|
|
|
|
|
RvaDataAllocator rvaDataAllocator = _moduleEntityManager.GetEntity<RvaDataAllocator>(method.Module);
|
|
|
|
|
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
|
|
|
|
|
2025-06-21 09:20:36 +08:00
|
|
|
|
switch (random.NextInt(5))
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
2025-06-21 09:20:36 +08:00
|
|
|
|
case 0:
|
|
|
|
|
{
|
|
|
|
|
// = c = encrypted static field
|
2025-07-02 18:57:53 +08:00
|
|
|
|
FieldDef cacheField = constFieldAllocator.Allocate(value);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 1:
|
|
|
|
|
{
|
|
|
|
|
// c = a + b
|
|
|
|
|
int a = random.NextInt();
|
|
|
|
|
int b = value - a;
|
|
|
|
|
float constProbability = 0.5f;
|
2025-07-02 18:57:53 +08:00
|
|
|
|
ConstObfusUtil.LoadConstTwoInt(a, b, random, constProbability, constFieldAllocator, obfuscatedInstructions);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Add));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 2:
|
|
|
|
|
{
|
|
|
|
|
// c = a * b
|
|
|
|
|
int a = random.NextInt() | 0x1;
|
|
|
|
|
int ra = MathUtil.ModInverse32(a);
|
|
|
|
|
int b = ra * value;
|
|
|
|
|
float constProbability = 0.5f;
|
2025-07-02 18:57:53 +08:00
|
|
|
|
ConstObfusUtil.LoadConstTwoInt(a, b, random, constProbability, constFieldAllocator, obfuscatedInstructions);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Mul));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 3:
|
|
|
|
|
{
|
|
|
|
|
// c = a ^ b
|
|
|
|
|
int a = random.NextInt();
|
|
|
|
|
int b = a ^ value;
|
|
|
|
|
float constProbability = 0.5f;
|
2025-07-02 18:57:53 +08:00
|
|
|
|
ConstObfusUtil.LoadConstTwoInt(a, b, random, constProbability, constFieldAllocator, obfuscatedInstructions);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Xor));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
if (needCacheValue)
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
FieldDef cacheField = constFieldAllocator.Allocate(value);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
|
|
|
|
int salt = GenerateSalt(random);
|
|
|
|
|
int encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
2025-07-02 18:57:53 +08:00
|
|
|
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFromRvaInt));
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-21 09:20:36 +08:00
|
|
|
|
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ObfuscateLong(MethodDef method, bool needCacheValue, long value, List<Instruction> obfuscatedInstructions)
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
EncryptionScopeInfo encryptionScope = _moduleEntityManager.EncryptionScopeProvider.GetScope(method.Module);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
2025-07-02 18:57:53 +08:00
|
|
|
|
ConstFieldAllocator constFieldAllocator = _moduleEntityManager.GetEntity<ConstFieldAllocator>(method.Module);
|
|
|
|
|
RvaDataAllocator rvaDataAllocator = _moduleEntityManager.GetEntity<RvaDataAllocator>(method.Module);
|
|
|
|
|
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
|
|
|
|
|
switch (random.NextInt(5))
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
2025-06-21 09:20:36 +08:00
|
|
|
|
case 0:
|
|
|
|
|
{
|
|
|
|
|
// c = encrypted static field
|
2025-07-02 18:57:53 +08:00
|
|
|
|
FieldDef cacheField = constFieldAllocator.Allocate(value);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 1:
|
|
|
|
|
{
|
|
|
|
|
// c = a + b
|
|
|
|
|
long a = random.NextLong();
|
|
|
|
|
long b = value - a;
|
|
|
|
|
float constProbability = 0.5f;
|
2025-07-02 18:57:53 +08:00
|
|
|
|
ConstObfusUtil.LoadConstTwoLong(a, b, random, constProbability, constFieldAllocator, obfuscatedInstructions);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Add));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 2:
|
|
|
|
|
{
|
|
|
|
|
// c = a * b
|
|
|
|
|
long a = random.NextLong() | 0x1;
|
|
|
|
|
long ra = MathUtil.ModInverse64(a);
|
|
|
|
|
long b = ra * value;
|
|
|
|
|
float constProbability = 0.5f;
|
2025-07-02 18:57:53 +08:00
|
|
|
|
ConstObfusUtil.LoadConstTwoLong(a, b, random, constProbability, constFieldAllocator, obfuscatedInstructions);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Mul));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 3:
|
|
|
|
|
{
|
|
|
|
|
// c = a ^ b
|
|
|
|
|
long a = random.NextLong();
|
|
|
|
|
long b = a ^ value;
|
|
|
|
|
float constProbability = 0.5f;
|
2025-07-02 18:57:53 +08:00
|
|
|
|
ConstObfusUtil.LoadConstTwoLong(a, b, random, constProbability, constFieldAllocator, obfuscatedInstructions);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Xor));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
if (needCacheValue)
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
FieldDef cacheField = constFieldAllocator.Allocate(value);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
|
|
|
|
int salt = GenerateSalt(random);
|
|
|
|
|
long encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
2025-07-02 18:57:53 +08:00
|
|
|
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
2025-06-21 09:20:36 +08:00
|
|
|
|
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFromRvaLong));
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-21 09:20:36 +08:00
|
|
|
|
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ObfuscateFloat(MethodDef method, bool needCacheValue, float value, List<Instruction> obfuscatedInstructions)
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
EncryptionScopeInfo encryptionScope = _moduleEntityManager.EncryptionScopeProvider.GetScope(method.Module);
|
|
|
|
|
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
|
|
|
|
ConstFieldAllocator constFieldAllocator = _moduleEntityManager.GetEntity<ConstFieldAllocator>(method.Module);
|
|
|
|
|
RvaDataAllocator rvaDataAllocator = _moduleEntityManager.GetEntity<RvaDataAllocator>(method.Module);
|
|
|
|
|
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
|
|
|
|
|
2025-05-21 09:23:29 +08:00
|
|
|
|
if (needCacheValue)
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
FieldDef cacheField = constFieldAllocator.Allocate(value);
|
2025-05-21 09:23:29 +08:00
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 18:57:53 +08:00
|
|
|
|
|
2025-05-21 09:23:29 +08:00
|
|
|
|
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
|
|
|
|
int salt = GenerateSalt(random);
|
|
|
|
|
float encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
2025-07-02 18:57:53 +08:00
|
|
|
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
2025-05-21 09:23:29 +08:00
|
|
|
|
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFromRvaFloat));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ObfuscateDouble(MethodDef method, bool needCacheValue, double value, List<Instruction> obfuscatedInstructions)
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
EncryptionScopeInfo encryptionScope = _moduleEntityManager.EncryptionScopeProvider.GetScope(method.Module);
|
|
|
|
|
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
|
|
|
|
ConstFieldAllocator constFieldAllocator = _moduleEntityManager.GetEntity<ConstFieldAllocator>(method.Module);
|
|
|
|
|
RvaDataAllocator rvaDataAllocator = _moduleEntityManager.GetEntity<RvaDataAllocator>(method.Module);
|
|
|
|
|
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
|
|
|
|
|
2025-05-21 09:23:29 +08:00
|
|
|
|
if (needCacheValue)
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
FieldDef cacheField = constFieldAllocator.Allocate(value);
|
2025-05-21 09:23:29 +08:00
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 18:57:53 +08:00
|
|
|
|
|
2025-05-21 09:23:29 +08:00
|
|
|
|
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
|
|
|
|
int salt = GenerateSalt(random);
|
|
|
|
|
double encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
2025-07-02 18:57:53 +08:00
|
|
|
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
2025-05-21 09:23:29 +08:00
|
|
|
|
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFromRvaDouble));
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-28 17:23:24 +08:00
|
|
|
|
|
|
|
|
|
class EncryptedRvaDataInfo
|
|
|
|
|
{
|
|
|
|
|
public readonly FieldDef fieldDef;
|
|
|
|
|
public readonly byte[] originalBytes;
|
|
|
|
|
public readonly byte[] encryptedBytes;
|
|
|
|
|
public readonly int opts;
|
|
|
|
|
public readonly int salt;
|
|
|
|
|
|
|
|
|
|
public EncryptedRvaDataInfo(FieldDef fieldDef, byte[] originalBytes, byte[] encryptedBytes, int opts, int salt)
|
|
|
|
|
{
|
|
|
|
|
this.fieldDef = fieldDef;
|
|
|
|
|
this.originalBytes = originalBytes;
|
|
|
|
|
this.encryptedBytes = encryptedBytes;
|
|
|
|
|
this.opts = opts;
|
|
|
|
|
this.salt = salt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<FieldDef, EncryptedRvaDataInfo> _encryptedRvaFields = new Dictionary<FieldDef, EncryptedRvaDataInfo>();
|
|
|
|
|
|
|
|
|
|
private EncryptedRvaDataInfo GetEncryptedRvaData(FieldDef fieldDef)
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
2025-05-28 17:23:24 +08:00
|
|
|
|
if (!_encryptedRvaFields.TryGetValue(fieldDef, out var encryptedRvaData))
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
EncryptionScopeInfo encryptionScope = _moduleEntityManager.EncryptionScopeProvider.GetScope(fieldDef.Module);
|
2025-05-28 17:23:24 +08:00
|
|
|
|
IRandom random = CreateRandomForValue(encryptionScope, FieldEqualityComparer.CompareDeclaringTypes.GetHashCode(fieldDef));
|
|
|
|
|
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
|
|
|
|
int salt = GenerateSalt(random);
|
|
|
|
|
byte[] originalBytes = fieldDef.InitialValue;
|
|
|
|
|
byte[] encryptedBytes = (byte[])originalBytes.Clone();
|
|
|
|
|
encryptionScope.encryptor.EncryptBlock(encryptedBytes, ops, salt);
|
|
|
|
|
Assert.AreNotEqual(originalBytes, encryptedBytes, "Original bytes should not be the same as encrypted bytes.");
|
|
|
|
|
encryptedRvaData = new EncryptedRvaDataInfo(fieldDef, originalBytes, encryptedBytes, ops, salt);
|
|
|
|
|
_encryptedRvaFields.Add(fieldDef, encryptedRvaData);
|
|
|
|
|
fieldDef.InitialValue = encryptedBytes;
|
|
|
|
|
byte[] decryptedBytes = (byte[])encryptedBytes.Clone();
|
|
|
|
|
encryptionScope.encryptor.DecryptBlock(decryptedBytes, ops, salt);
|
2025-07-23 20:56:38 +08:00
|
|
|
|
AssertUtil.AreArrayEqual(originalBytes, decryptedBytes, "Decrypted bytes should match the original bytes after encryption and decryption.");
|
2025-05-28 17:23:24 +08:00
|
|
|
|
}
|
|
|
|
|
return encryptedRvaData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void ObfuscateBytes(MethodDef method, bool needCacheValue, FieldDef field, byte[] value, List<Instruction> obfuscatedInstructions)
|
|
|
|
|
{
|
|
|
|
|
EncryptedRvaDataInfo encryptedData = GetEncryptedRvaData(field);
|
|
|
|
|
Assert.AreEqual(value.Length, encryptedData.encryptedBytes.Length);
|
|
|
|
|
|
|
|
|
|
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(encryptedData.encryptedBytes.Length));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(encryptedData.opts));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(encryptedData.salt));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptInitializeArray));
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ObfuscateString(MethodDef method, bool needCacheValue, string value, List<Instruction> obfuscatedInstructions)
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
EncryptionScopeInfo encryptionScope = _moduleEntityManager.EncryptionScopeProvider.GetScope(method.Module);
|
|
|
|
|
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
|
|
|
|
ConstFieldAllocator constFieldAllocator = _moduleEntityManager.GetEntity<ConstFieldAllocator>(method.Module);
|
|
|
|
|
RvaDataAllocator rvaDataAllocator = _moduleEntityManager.GetEntity<RvaDataAllocator>(method.Module);
|
|
|
|
|
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
|
|
|
|
|
2025-05-21 09:23:29 +08:00
|
|
|
|
if (needCacheValue)
|
|
|
|
|
{
|
2025-07-02 18:57:53 +08:00
|
|
|
|
FieldDef cacheField = constFieldAllocator.Allocate(value);
|
2025-05-21 09:23:29 +08:00
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
|
|
|
|
int salt = GenerateSalt(random);
|
|
|
|
|
int stringByteLength = Encoding.UTF8.GetByteCount(value);
|
|
|
|
|
byte[] encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
|
|
|
|
Assert.AreEqual(stringByteLength, encryptedValue.Length);
|
2025-07-02 18:57:53 +08:00
|
|
|
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
2025-05-21 09:23:29 +08:00
|
|
|
|
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
|
|
|
|
// should use stringByteLength, can't use rvaData.size, because rvaData.size is align to 4, it's not the actual length.
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(stringByteLength));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt));
|
|
|
|
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFromRvaString));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Done()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|