常量加密

backup
walon 2025-05-08 08:54:18 +08:00
parent 807ead7cfc
commit fda0e5c66d
7 changed files with 156 additions and 96 deletions

View File

@ -24,34 +24,49 @@ namespace Obfuz.Emit
_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) }));
Type encryptionService = typeof(EncryptionService);
_encryptBlock = mod.Import(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) }));
_decryptBlock = mod.Import(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) }));
_encryptInt = mod.Import(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) }));
_decryptInt = mod.Import(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) }));
_encryptLong = mod.Import(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) }));
_decryptLong = mod.Import(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) }));
_encryptFloat = mod.Import(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) }));
_decryptFloat = mod.Import(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) }));
_encryptDouble = mod.Import(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) }));
_decryptDouble = mod.Import(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) }));
_encryptString = mod.Import(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) }));
_decryptString = mod.Import(encryptionService.GetMethod("DecryptString", new[] { typeof(byte[]), 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) }));
_encryptBytes = mod.Import(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) }));
_decryptBytes = mod.Import(encryptionService.GetMethod("Decrypt", new[] { typeof(byte[]), typeof(int), typeof(int), typeof(int), typeof(int) }));
Assert.IsNotNull(_decryptBytes);
_decryptFromRvaInt = mod.Import(encryptionService.GetMethod("DecryptFromRvaInt", new[] { typeof(byte[]), typeof(int), typeof(int), typeof(int) }));
Assert.IsNotNull(_decryptFromRvaInt);
_decryptFromRvaLong = mod.Import(encryptionService.GetMethod("DecryptFromRvaLong", new[] { typeof(byte[]), typeof(int), typeof(int), typeof(int) }));
Assert.IsNotNull(_decryptFromRvaLong);
_decryptFromRvaFloat = mod.Import(encryptionService.GetMethod("DecryptFromRvaFloat", new[] { typeof(byte[]), typeof(int), typeof(int), typeof(int) }));
Assert.IsNotNull(_decryptFromRvaFloat);
_decryptFromRvaDouble = mod.Import(encryptionService.GetMethod("DecryptFromRvaDouble", new[] { typeof(byte[]), typeof(int), typeof(int), typeof(int) }));
Assert.IsNotNull(_decryptFromRvaDouble);
_decryptFromRvaBytes = mod.Import(encryptionService.GetMethod("DecryptFromRvaBytes", new[] { typeof(byte[]), typeof(int), typeof(int), typeof(int), typeof(int) }));
Assert.IsNotNull(_decryptFromRvaBytes);
_decryptFromRvaString = mod.Import(encryptionService.GetMethod("DecryptFromRvaString", new[] { typeof(byte[]), typeof(int), typeof(int), typeof(int), typeof(int) }));
Assert.IsNotNull(_decryptFromRvaString);
}
private ModuleDef _module;
@ -76,6 +91,13 @@ namespace Obfuz.Emit
private IMethod _encryptBytes;
private IMethod _decryptBytes;
private IMethod _decryptFromRvaInt;
private IMethod _decryptFromRvaLong;
private IMethod _decryptFromRvaFloat;
private IMethod _decryptFromRvaDouble;
private IMethod _decryptFromRvaString;
private IMethod _decryptFromRvaBytes;
public IMethod CastIntAsFloat => _castIntAsFloat;
public IMethod CastLongAsDouble => _castLongAsDouble;
public IMethod CastFloatAsInt => _castFloatAsInt;
@ -98,5 +120,13 @@ namespace Obfuz.Emit
public IMethod DecryptString => _decryptString;
public IMethod EncryptBytes => _encryptBytes;
public IMethod DecryptBytes => _decryptBytes;
public IMethod DecryptFromRvaInt => _decryptFromRvaInt;
public IMethod DecryptFromRvaLong => _decryptFromRvaLong;
public IMethod DecryptFromRvaFloat => _decryptFromRvaFloat;
public IMethod DecryptFromRvaDouble => _decryptFromRvaDouble;
public IMethod DecryptFromRvaBytes => _decryptFromRvaBytes;
public IMethod DecryptFromRvaString => _decryptFromRvaString;
}
}

View File

@ -2,9 +2,11 @@
using System;
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
{
@ -185,76 +187,71 @@ namespace Obfuz.Encryption
return UnsafeUtility.As<long, double>(ref decValue);
}
public int[] Encrypt(byte[] bytes, int offset, int length, int ops, int salt)
public unsafe byte[] Encrypt(byte[] bytes, int offset, int length, int ops, int salt)
{
if (length == 0)
{
return Array.Empty<int>();
return Array.Empty<byte>();
}
int intLength = (length + 3) / 4;
int[] encInts = new int[intLength];
// align size to 4
int align4Length = (length + 3) & ~3;
byte[] encInts = new byte[align4Length];
Buffer.BlockCopy(bytes, offset, encInts, 0, length);
for (int i = 0; i < intLength; i++)
int intLength = align4Length >> 2;
fixed (byte* intArr = &bytes[0])
{
encInts[i] = Encrypt(encInts[i], ops, salt);
for (int i = 0; i < intLength; i++)
{
int* ele = (int*)intArr + i;
*ele = Encrypt(*ele, ops, salt);
}
}
return encInts;
}
public int[] Encrypt(byte[] bytes, int ops, int salt)
public unsafe byte[] Decrypt(byte[] value, int offset, int length, int ops, int salt)
{
if (length == 0)
{
return Array.Empty<byte>();
}
int align4Length = (length + 3) & ~3;
int intLength = align4Length >> 2;
byte[] decInts = new byte[align4Length];
fixed (byte* intArr = &decInts[0])
{
for (int i = 0; i < intLength; i++)
{
int* ele = (int*)intArr + i;
*ele = Decrypt(*ele, ops, salt);
}
}
return decInts;
}
public byte[] Encrypt(byte[] bytes, int ops, int salt)
{
return Encrypt(bytes, 0, bytes.Length, ops, salt);
}
public byte[] Decrypt(int[] value, int offset, int byteLength, int ops, int salt)
{
if (byteLength == 0)
{
return Array.Empty<byte>();
}
int intLength = (byteLength + 3) / 4;
int[] decValue = new int[intLength];
for (int i = 0; i < intLength; i++)
{
decValue[i] = Decrypt(value[i], ops, salt);
}
byte[] bytes = new byte[byteLength];
Buffer.BlockCopy(decValue, 0, bytes, 0, byteLength);
return bytes;
}
public byte[] Decrypt(int[] value, int byteLength, int ops, int salt)
{
return Decrypt(value, 0, byteLength, ops, salt);
}
public int[] Encrypt(string value, int ops, int salt)
public byte[] Encrypt(string value, int ops, int salt)
{
if (value.Length == 0)
{
return Array.Empty<int>();
return Array.Empty<byte>();
}
byte[] bytes = System.Text.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)
public string DecryptString(byte[] value, int offset, int length, int ops, int salt)
{
if (stringBytesLength == 0)
if (length == 0)
{
return string.Empty;
}
int intLength = (stringBytesLength + 3) / 4;
int[] intValue = new int[intLength];
for (int i = 0; i < intLength; i++)
{
intValue[i] = Decrypt(value[i], ops, salt);
}
byte[] bytes = new byte[stringBytesLength];
Buffer.BlockCopy(intValue, 0, bytes, 0, stringBytesLength);
return System.Text.Encoding.UTF8.GetString(bytes);
return System.Text.Encoding.UTF8.GetString(Decrypt(value, offset, length, ops, salt));
}
}
}

View File

@ -6,6 +6,7 @@ using Obfuz.Utils;
using System;
using System.Collections.Generic;
using NUnit.Framework;
using System.Text;
namespace Obfuz.ObfusPasses.ConstObfus
{
@ -61,10 +62,11 @@ namespace Obfuz.ObfusPasses.ConstObfus
RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue);
DefaultModuleMetadataImporter importer = GetModuleMetadataImporter(method);
obfuscatedInstructions.Add(Instruction.CreateLdcI4(encryptedValue));
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.DecryptInt));
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFromRvaInt));
}
public void ObfuscateLong(MethodDef method, long value, List<Instruction> obfuscatedInstructions)
@ -72,12 +74,14 @@ namespace Obfuz.ObfusPasses.ConstObfus
int ops = GenerateEncryptionOperations();
int salt = GenerateSalt();
long encryptedValue = _encryptor.Encrypt(value, ops, salt);
RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue);
DefaultModuleMetadataImporter importer = GetModuleMetadataImporter(method);
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I8, encryptedValue));
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.DecryptLong));
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFromRvaLong));
}
public void ObfuscateFloat(MethodDef method, float value, List<Instruction> obfuscatedInstructions)
@ -85,12 +89,14 @@ namespace Obfuz.ObfusPasses.ConstObfus
int ops = GenerateEncryptionOperations();
int salt = GenerateSalt();
float encryptedValue = _encryptor.Encrypt(value, ops, salt);
RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue);
DefaultModuleMetadataImporter importer = GetModuleMetadataImporter(method);
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R4, encryptedValue));
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.DecryptFloat));
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFromRvaFloat));
}
public void ObfuscateDouble(MethodDef method, double value, List<Instruction> obfuscatedInstructions)
@ -98,29 +104,51 @@ namespace Obfuz.ObfusPasses.ConstObfus
int ops = GenerateEncryptionOperations();
int salt = GenerateSalt();
double encryptedValue = _encryptor.Encrypt(value, ops, salt);
RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue);
DefaultModuleMetadataImporter importer = GetModuleMetadataImporter(method);
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R8, encryptedValue));
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.DecryptDouble));
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFromRvaDouble));
}
public void ObfuscateBytes(MethodDef method, byte[] value, List<Instruction> obfuscatedInstructions)
{
int ops = GenerateEncryptionOperations();
int salt = GenerateSalt();
byte[] encryptedValue = _encryptor.Encrypt(value, 0, value.Length, ops, salt);
Assert.IsTrue(encryptedValue.Length % 4 == 0);
RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue);
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);
DefaultModuleMetadataImporter importer = GetModuleMetadataImporter(method);
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
// should use value.Length, can't use rvaData.size, because rvaData.size is align to 4, it's not the actual length.
obfuscatedInstructions.Add(Instruction.CreateLdcI4(value.Length));
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt));
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFromRvaBytes));
}
public void ObfuscateString(MethodDef method, string value, List<Instruction> obfuscatedInstructions)
{
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.String, value);
CompileNode(node, method, obfuscatedInstructions);
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldstr, value));
int ops = GenerateEncryptionOperations();
int salt = GenerateSalt();
int stringByteLength = Encoding.UTF8.GetByteCount(value);
byte[] encryptedValue = _encryptor.Encrypt(value, ops, salt);
Assert.IsTrue(encryptedValue.Length % 4 == 0);
RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue);
DefaultModuleMetadataImporter importer = GetModuleMetadataImporter(method);
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()

View File

@ -9,7 +9,7 @@
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"allowUnsafeCode": true,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,

View File

@ -71,30 +71,31 @@ namespace Obfuz
return value;
}
public int[] Encrypt(byte[] value, int offset, int length, int opts, int salt)
public byte[] 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;
int align4Length = (length + 3) & ~3;
var encryptedBytes = new byte[align4Length];
Buffer.BlockCopy(value, offset, encryptedBytes, 0, length);
return encryptedBytes;
}
public byte[] Decrypt(int[] value, int offset, int byteLength, int ops, int salt)
public byte[] Decrypt(byte[] value, int offset, int length, int ops, int salt)
{
byte[] byteArr = new byte[byteLength];
Buffer.BlockCopy(value, 0, byteArr, 0, byteLength);
byte[] byteArr = new byte[length];
Buffer.BlockCopy(value, 0, byteArr, 0, length);
return byteArr;
}
public int[] Encrypt(string value, int ops, int salt)
public byte[] 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)
public string DecryptString(byte[] value, int offset, int length, int ops, int salt)
{
byte[] bytes = new byte[stringBytesLength];
Buffer.BlockCopy(value, 0, bytes, 0, stringBytesLength);
byte[] bytes = new byte[length];
Buffer.BlockCopy(value, 0, bytes, 0, length);
return Encoding.UTF8.GetString(bytes);
}
}

View File

@ -60,22 +60,22 @@ namespace Obfuz
return _encryptor.Decrypt(value, opts, salt);
}
public static int[] Encrypt(byte[] value, int offset, int length, int opts, int salt)
public static byte[] 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)
public static byte[] Decrypt(byte[] 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)
public static byte[] 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)
public static string DecryptString(byte[] value, int offset, int stringBytesLength, int ops, int salt)
{
return _encryptor.DecryptString(value, offset, stringBytesLength, ops, salt);
}
@ -105,10 +105,14 @@ namespace Obfuz
return Decrypt(encryptedValue, ops, salt);
}
public static string DecryptFromRvaString(byte[] data, int offset, int stringBytesLength, int ops, int salt)
public static string DecryptFromRvaString(byte[] data, int offset, int length, int ops, int salt)
{
int[] encryptedValue = ConstUtility.GetBytes(data, offset, stringBytesLength);
return DecryptString(encryptedValue, 0, stringBytesLength, ops, salt);
return DecryptString(data, offset, length, ops, salt);
}
public static byte[] DecryptFromRvaBytes(byte[] data, int offset, int bytesLength, int ops, int salt)
{
return Decrypt(data, offset, bytesLength, ops, salt);
}
}
}

View File

@ -23,10 +23,10 @@ namespace Obfuz
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);
byte[] Encrypt(byte[] value, int offset, int length, int opts, int salt);
public byte[] Decrypt(byte[] 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);
public byte[] Encrypt(string value, int ops, int salt);
public string DecryptString(byte[] value, int offset, int stringBytesLength, int ops, int salt);
}
}