mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
接入obfuz->2.0
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
public static class AssetUtility
|
||||
{
|
||||
public static void VerifySecretKey(int expectedValue, int actualValue)
|
||||
{
|
||||
if (expectedValue != actualValue)
|
||||
{
|
||||
throw new Exception($"VerifySecretKey failed. Your secret key is unmatched with secret key used by current assembly in obfuscation");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 72fb691c46a883f4ea3b3dfe7d2280f3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using UnityEngine.Assertions;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
public static class ConstUtility
|
||||
{
|
||||
public static int GetInt(byte[] data, int offset)
|
||||
{
|
||||
return BitConverter.ToInt32(data, offset);
|
||||
}
|
||||
|
||||
public static long GetLong(byte[] data, int offset)
|
||||
{
|
||||
return BitConverter.ToInt64(data, offset);
|
||||
}
|
||||
|
||||
public static float GetFloat(byte[] data, int offset)
|
||||
{
|
||||
return BitConverter.ToSingle(data, offset);
|
||||
}
|
||||
|
||||
public static double GetDouble(byte[] data, int offset)
|
||||
{
|
||||
return BitConverter.ToDouble(data, offset);
|
||||
}
|
||||
|
||||
public static string GetString(byte[] data, int offset, int length)
|
||||
{
|
||||
return Encoding.UTF8.GetString(data, offset, length);
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(byte[] data, int offset, int length)
|
||||
{
|
||||
byte[] result = new byte[length];
|
||||
Array.Copy(data, offset, result, 0, length);
|
||||
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);
|
||||
}
|
||||
|
||||
public static unsafe int CastFloatAsInt(float value)
|
||||
{
|
||||
int* intValue = (int*)&value;
|
||||
return *intValue;
|
||||
}
|
||||
|
||||
public static unsafe float CastIntAsFloat(int value)
|
||||
{
|
||||
float* floatValue = (float*)&value;
|
||||
return *floatValue;
|
||||
}
|
||||
|
||||
public static unsafe long CastDoubleAsLong(double value)
|
||||
{
|
||||
long* longValue = (long*)&value;
|
||||
return *longValue;
|
||||
}
|
||||
|
||||
public static unsafe double CastLongAsDouble(long value)
|
||||
{
|
||||
double* doubleValue = (double*)&value;
|
||||
return *doubleValue;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 48bd592d1a1339643be1fafe4b97c941
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
|
||||
public class EncryptFieldAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 30f22110938816d4cb7e9cc9a176fd1e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,31 @@
|
||||
namespace Obfuz
|
||||
{
|
||||
public interface IEncryptionScope
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public abstract class EncryptionScopeBase : IEncryptionScope
|
||||
{
|
||||
public void ForcePreserveAOT()
|
||||
{
|
||||
EncryptionService<EncryptionScopeBase>.Encrypt(0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public struct DefaultDynamicEncryptionScope : IEncryptionScope
|
||||
{
|
||||
public void ForcePreserveAOT()
|
||||
{
|
||||
EncryptionService<DefaultDynamicEncryptionScope>.Encrypt(0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public struct DefaultStaticEncryptionScope : IEncryptionScope
|
||||
{
|
||||
public void ForcePreserveAOT()
|
||||
{
|
||||
EncryptionService<DefaultStaticEncryptionScope>.Encrypt(0, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1d729fe7cb7d0bc43a69f1ba09f99061
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,127 @@
|
||||
using System;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
|
||||
public static class EncryptionService<T> where T : IEncryptionScope
|
||||
{
|
||||
// for compatibility with Mono because Mono will raise FieldAccessException when try access private field
|
||||
public static IEncryptor _encryptor;
|
||||
|
||||
public static IEncryptor Encryptor
|
||||
{
|
||||
get => _encryptor;
|
||||
set { _encryptor = value; }
|
||||
}
|
||||
|
||||
public static void EncryptBlock(byte[] data, int ops, int salt)
|
||||
{
|
||||
_encryptor.EncryptBlock(data, ops, salt);
|
||||
}
|
||||
|
||||
public static void DecryptBlock(byte[] data, int ops, int salt)
|
||||
{
|
||||
_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 byte[] Encrypt(byte[] value, int offset, int length, int opts, int salt)
|
||||
{
|
||||
return _encryptor.Encrypt(value, offset, length, opts, 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 byte[] Encrypt(string value, int ops, int salt)
|
||||
{
|
||||
return _encryptor.Encrypt(value, ops, salt);
|
||||
}
|
||||
|
||||
public static string DecryptString(byte[] 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 = BitConverter.ToInt32(data, offset);
|
||||
return Decrypt(encryptedValue, ops, salt);
|
||||
}
|
||||
|
||||
public static long DecryptFromRvaLong(byte[] data, int offset, int ops, int salt)
|
||||
{
|
||||
long encryptedValue = BitConverter.ToInt64(data, offset);
|
||||
return Decrypt(encryptedValue, ops, salt);
|
||||
}
|
||||
|
||||
public static float DecryptFromRvaFloat(byte[] data, int offset, int ops, int salt)
|
||||
{
|
||||
float encryptedValue = BitConverter.ToSingle(data, offset);
|
||||
return Decrypt(encryptedValue, ops, salt);
|
||||
}
|
||||
|
||||
public static double DecryptFromRvaDouble(byte[] data, int offset, int ops, int salt)
|
||||
{
|
||||
double encryptedValue = BitConverter.ToDouble(data, offset);
|
||||
return Decrypt(encryptedValue, ops, salt);
|
||||
}
|
||||
|
||||
public static string DecryptFromRvaString(byte[] data, int offset, int length, int ops, int 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);
|
||||
}
|
||||
|
||||
public static void DecryptInitializeArray(System.Array arr, System.RuntimeFieldHandle field, int length, int ops, int salt)
|
||||
{
|
||||
_encryptor.DecryptInitializeArray(arr, field, length, ops, salt);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bbbeb7501a0d84542828cb1aa7103d1b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,362 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using Unity.Collections.LowLevel.Unsafe;
|
||||
using UnityEngine.Assertions;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
public abstract class EncryptorBase : IEncryptor
|
||||
{
|
||||
public abstract int OpCodeCount { get; }
|
||||
|
||||
public static int[] ConvertToIntKey(byte[] key)
|
||||
{
|
||||
Assert.AreEqual(0, key.Length % 4);
|
||||
int align4Length = key.Length / 4;
|
||||
int[] intKey = new int[align4Length];
|
||||
Buffer.BlockCopy(key, 0, intKey, 0, key.Length);
|
||||
return intKey;
|
||||
}
|
||||
|
||||
public abstract int Encrypt(int value, int opts, int salt);
|
||||
public abstract int Decrypt(int value, int opts, int salt);
|
||||
|
||||
public virtual long Encrypt(long value, int opts, int salt)
|
||||
{
|
||||
int low = (int)value;
|
||||
int high = (int)(value >> 32);
|
||||
int encryptedLow = Encrypt(low, opts, salt);
|
||||
int encryptedHigh = Encrypt(high, opts, salt);
|
||||
return ((long)encryptedHigh << 32) | (uint)encryptedLow;
|
||||
}
|
||||
|
||||
public virtual long Decrypt(long value, int opts, int salt)
|
||||
{
|
||||
int low = (int)value;
|
||||
int high = (int)(value >> 32);
|
||||
int decryptedLow = Decrypt(low, opts, salt);
|
||||
int decryptedHigh = Decrypt(high, opts, salt);
|
||||
return ((long)decryptedHigh << 32) | (uint)decryptedLow;
|
||||
}
|
||||
|
||||
public virtual unsafe float Encrypt(float value, int opts, int salt)
|
||||
{
|
||||
if (float.IsNaN(value) || float.IsInfinity(value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
ref int intValue = ref *(int*)&value;
|
||||
int xorValue = ((1 << 23) - 1) & Decrypt(0xABCD, opts, salt);
|
||||
intValue ^= xorValue;
|
||||
return value;
|
||||
}
|
||||
|
||||
public virtual unsafe float Decrypt(float value, int opts, int salt)
|
||||
{
|
||||
if (float.IsNaN(value) || float.IsInfinity(value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
ref int intValue = ref *(int*)&value;
|
||||
int xorValue = ((1 << 23) - 1) & Decrypt(0xABCD, opts, salt);
|
||||
intValue ^= xorValue;
|
||||
return value;
|
||||
}
|
||||
|
||||
public virtual unsafe double Encrypt(double value, int opts, int salt)
|
||||
{
|
||||
if (double.IsNaN(value) || double.IsInfinity(value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
ref long longValue = ref *(long*)&value;
|
||||
long xorValue = ((1L << 52) - 1) & Decrypt(0xAABBCCDDL, opts, salt);
|
||||
longValue ^= xorValue;
|
||||
return value;
|
||||
}
|
||||
|
||||
public virtual unsafe double Decrypt(double value, int opts, int salt)
|
||||
{
|
||||
if (double.IsNaN(value) || double.IsInfinity(value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
ref long longValue = ref *(long*)&value;
|
||||
long xorValue = ((1L << 52) - 1) & Decrypt(0xAABBCCDDL, opts, salt);
|
||||
longValue ^= xorValue;
|
||||
return value;
|
||||
}
|
||||
|
||||
public virtual unsafe byte[] Encrypt(byte[] value, int offset, int length, int ops, int salt)
|
||||
{
|
||||
if (length == 0)
|
||||
{
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
|
||||
var encryptedBytes = new byte[length];
|
||||
int intArrLength = length >> 2;
|
||||
|
||||
// align to 4
|
||||
if ((offset & 0x3) != 0)
|
||||
{
|
||||
Buffer.BlockCopy(value, offset, encryptedBytes, 0, length);
|
||||
|
||||
// encrypt int
|
||||
|
||||
fixed (byte* dstBytePtr = &encryptedBytes[0])
|
||||
{
|
||||
int* dstIntPtr = (int*)dstBytePtr;
|
||||
int last = 0;
|
||||
for (int i = 0; i < intArrLength; i++)
|
||||
{
|
||||
last ^= Encrypt(dstIntPtr[i], ops, salt);
|
||||
dstIntPtr[i] = last;
|
||||
}
|
||||
}
|
||||
for (int i = intArrLength * 4; i < length; i++)
|
||||
{
|
||||
encryptedBytes[i] = (byte)(encryptedBytes[i] ^ salt);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// encrypt int
|
||||
fixed (byte* srcBytePtr = &value[offset])
|
||||
{
|
||||
fixed (byte* dstBytePtr = &encryptedBytes[0])
|
||||
{
|
||||
int* srcIntPtr = (int*)srcBytePtr;
|
||||
int* dstIntPtr = (int*)dstBytePtr;
|
||||
|
||||
int last = 0;
|
||||
for (int i = 0; i < intArrLength; i++)
|
||||
{
|
||||
last ^= Encrypt(srcIntPtr[i], ops, salt);
|
||||
dstIntPtr[i] = last;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = intArrLength * 4; i < length; i++)
|
||||
{
|
||||
encryptedBytes[i] = (byte)(value[offset + i] ^ salt);
|
||||
}
|
||||
}
|
||||
return encryptedBytes;
|
||||
}
|
||||
|
||||
public unsafe virtual byte[] Decrypt(byte[] value, int offset, int length, int ops, int salt)
|
||||
{
|
||||
if (length == 0)
|
||||
{
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
var decryptedBytes = new byte[length];
|
||||
int intArrLength = length >> 2;
|
||||
|
||||
// align to 4
|
||||
if ((offset & 0x3) != 0)
|
||||
{
|
||||
Buffer.BlockCopy(value, offset, decryptedBytes, 0, length);
|
||||
|
||||
// encrypt int
|
||||
|
||||
fixed (byte* dstBytePtr = &decryptedBytes[0])
|
||||
{
|
||||
int* dstIntPtr = (int*)dstBytePtr;
|
||||
int last = 0;
|
||||
for (int i = 0; i < intArrLength; i++)
|
||||
{
|
||||
int oldLast = last;
|
||||
last = dstIntPtr[i];
|
||||
dstIntPtr[i] = Decrypt(last ^ oldLast, ops, salt);
|
||||
}
|
||||
}
|
||||
for (int i = intArrLength * 4; i < length; i++)
|
||||
{
|
||||
decryptedBytes[i] = (byte)(decryptedBytes[i] ^ salt);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// encrypt int
|
||||
fixed (byte* srcBytePtr = &value[offset])
|
||||
{
|
||||
fixed (byte* dstBytePtr = &decryptedBytes[0])
|
||||
{
|
||||
int* srcIntPtr = (int*)srcBytePtr;
|
||||
int* dstIntPtr = (int*)dstBytePtr;
|
||||
int last = 0;
|
||||
for (int i = 0; i < intArrLength; i++)
|
||||
{
|
||||
int oldLast = last;
|
||||
last = srcIntPtr[i];
|
||||
dstIntPtr[i] = Decrypt(last ^ oldLast, ops, salt);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = intArrLength * 4; i < length; i++)
|
||||
{
|
||||
decryptedBytes[i] = (byte)(value[offset + i] ^ salt);
|
||||
}
|
||||
}
|
||||
return decryptedBytes;
|
||||
}
|
||||
|
||||
public virtual byte[] Encrypt(string value, int ops, int salt)
|
||||
{
|
||||
if (value.Length == 0)
|
||||
{
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(value);
|
||||
return Encrypt(bytes, 0, bytes.Length, ops, salt);
|
||||
}
|
||||
|
||||
public virtual string DecryptString(byte[] value, int offset, int length, int ops, int salt)
|
||||
{
|
||||
if (length == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
byte[] bytes = Decrypt(value, offset, length, ops, salt);
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
|
||||
public virtual unsafe void EncryptBlock(byte[] data, int ops, int salt)
|
||||
{
|
||||
int length = data.Length;
|
||||
int intArrLength = length >> 2;
|
||||
|
||||
fixed (byte* dstBytePtr = &data[0])
|
||||
{
|
||||
int* dstIntPtr = (int*)dstBytePtr;
|
||||
int last = 0;
|
||||
for (int i = 0; i < intArrLength; i++)
|
||||
{
|
||||
last ^= Encrypt(dstIntPtr[i], ops, salt);
|
||||
dstIntPtr[i] = last;
|
||||
}
|
||||
}
|
||||
for (int i = intArrLength * 4; i < length; i++)
|
||||
{
|
||||
data[i] = (byte)(data[i] ^ salt);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual unsafe void DecryptBlock(byte[] data, int ops, int salt)
|
||||
{
|
||||
fixed (byte* dataPtr = &data[0])
|
||||
{
|
||||
DecryptBlock(dataPtr, data.Length, ops, salt);
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe void DecryptBlock(byte* data, int length, int ops, int salt)
|
||||
{
|
||||
int intArrLength = length >> 2;
|
||||
|
||||
int* dstIntPtr = (int*)data;
|
||||
int last = 0;
|
||||
for (int i = 0; i < intArrLength; i++)
|
||||
{
|
||||
int oldLast = last;
|
||||
last = dstIntPtr[i];
|
||||
dstIntPtr[i] = Decrypt(oldLast ^ last, ops, salt);
|
||||
}
|
||||
for (int i = intArrLength * 4; i < length; i++)
|
||||
{
|
||||
data[i] = (byte)(data[i] ^ salt);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual unsafe void DecryptInitializeArray(System.Array arr, System.RuntimeFieldHandle field, int length, int ops, int salt)
|
||||
{
|
||||
//Assert.AreEqual(Marshal.SizeOf(arr.GetType().GetElementType()), arr.Length);
|
||||
RuntimeHelpers.InitializeArray(arr, field);
|
||||
if (arr is byte[] byteArr)
|
||||
{
|
||||
fixed (byte* dataPtr = &byteArr[0])
|
||||
{
|
||||
DecryptBlock(dataPtr, length, ops, salt);
|
||||
}
|
||||
}
|
||||
else if (arr is int[] intArr)
|
||||
{
|
||||
fixed (int* dataPtr = &intArr[0])
|
||||
{
|
||||
DecryptBlock((byte*)dataPtr, length, ops, salt);
|
||||
}
|
||||
}
|
||||
else if (arr is long[] longArr)
|
||||
{
|
||||
fixed (long* dataPtr = &longArr[0])
|
||||
{
|
||||
DecryptBlock((byte*)dataPtr, length, ops, salt);
|
||||
}
|
||||
}
|
||||
else if (arr is sbyte[] sbyteArr)
|
||||
{
|
||||
fixed (sbyte* dataPtr = &sbyteArr[0])
|
||||
{
|
||||
DecryptBlock((byte*)dataPtr, length, ops, salt);
|
||||
}
|
||||
}
|
||||
else if (arr is short[] shortArr)
|
||||
{
|
||||
fixed (short* dataPtr = &shortArr[0])
|
||||
{
|
||||
DecryptBlock((byte*)dataPtr, length, ops, salt);
|
||||
}
|
||||
}
|
||||
else if (arr is ushort[] ushortArr)
|
||||
{
|
||||
fixed (ushort* dataPtr = &ushortArr[0])
|
||||
{
|
||||
DecryptBlock((byte*)dataPtr, length, ops, salt);
|
||||
}
|
||||
}
|
||||
else if (arr is uint[] uintArr)
|
||||
{
|
||||
fixed (uint* dataPtr = &uintArr[0])
|
||||
{
|
||||
DecryptBlock((byte*)dataPtr, length, ops, salt);
|
||||
}
|
||||
}
|
||||
else if (arr is ulong[] ulongArr)
|
||||
{
|
||||
fixed (ulong* dataPtr = &ulongArr[0])
|
||||
{
|
||||
DecryptBlock((byte*)dataPtr, length, ops, salt);
|
||||
}
|
||||
}
|
||||
else if (arr is float[] floatArr)
|
||||
{
|
||||
fixed (float* dataPtr = &floatArr[0])
|
||||
{
|
||||
DecryptBlock((byte*)dataPtr, length, ops, salt);
|
||||
}
|
||||
}
|
||||
else if (arr is double[] doubleArr)
|
||||
{
|
||||
fixed (double* dataPtr = &doubleArr[0])
|
||||
{
|
||||
DecryptBlock((byte*)dataPtr, length, ops, salt);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
void* dataPtr = UnsafeUtility.PinGCArrayAndGetDataAddress(arr, out ulong handle);
|
||||
try
|
||||
{
|
||||
DecryptBlock((byte*)dataPtr, length, ops, salt);
|
||||
}
|
||||
finally
|
||||
{
|
||||
UnsafeUtility.ReleaseGCObject(handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d1d4c5725e7ad624ba8e55ecb63bb440
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,247 @@
|
||||
using System;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
public static class ExprUtility
|
||||
{
|
||||
public static int Add(int a, int b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public static long Add(long a, long b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public static float Add(float a, float b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public static double Add(double a, double b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public static IntPtr Add(IntPtr a, IntPtr b)
|
||||
{
|
||||
return (IntPtr)((long)a + (long)b);
|
||||
}
|
||||
|
||||
public static IntPtr Add(IntPtr a, int b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public static int Subtract(int a, int b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
||||
|
||||
public static long Subtract(long a, long b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
||||
|
||||
public static float Subtract(float a, float b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
||||
|
||||
public static double Subtract(double a, double b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
||||
|
||||
public static IntPtr Subtract(IntPtr a, IntPtr b)
|
||||
{
|
||||
return (IntPtr)((long)a - (long)b);
|
||||
}
|
||||
|
||||
public static IntPtr Subtract(IntPtr a, int b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
||||
|
||||
public static int Multiply(int a, int b)
|
||||
{
|
||||
return a * b;
|
||||
}
|
||||
|
||||
public static long Multiply(long a, long b)
|
||||
{
|
||||
return a * b;
|
||||
}
|
||||
|
||||
public static float Multiply(float a, float b)
|
||||
{
|
||||
return a * b;
|
||||
}
|
||||
|
||||
public static double Multiply(double a, double b)
|
||||
{
|
||||
return a * b;
|
||||
}
|
||||
|
||||
public static IntPtr Multiply(IntPtr a, IntPtr b)
|
||||
{
|
||||
return (IntPtr)((long)a * (long)b);
|
||||
}
|
||||
|
||||
public static IntPtr Multiply(IntPtr a, int b)
|
||||
{
|
||||
return (IntPtr)((long)a * b);
|
||||
}
|
||||
|
||||
public static int Divide(int a, int b)
|
||||
{
|
||||
return a / b;
|
||||
}
|
||||
|
||||
public static long Divide(long a, long b)
|
||||
{
|
||||
return a / b;
|
||||
}
|
||||
|
||||
public static float Divide(float a, float b)
|
||||
{
|
||||
return a / b;
|
||||
}
|
||||
|
||||
public static double Divide(double a, double b)
|
||||
{
|
||||
return a / b;
|
||||
}
|
||||
|
||||
public static int DivideUn(int a, int b)
|
||||
{
|
||||
return (int)((uint)a / (uint)b);
|
||||
}
|
||||
|
||||
public static long DivideUn(long a, long b)
|
||||
{
|
||||
return (long)((ulong)a / (ulong)b);
|
||||
}
|
||||
|
||||
public static int Rem(int a, int b)
|
||||
{
|
||||
return a % b;
|
||||
}
|
||||
|
||||
public static long Rem(long a, long b)
|
||||
{
|
||||
return a % b;
|
||||
}
|
||||
|
||||
public static float Rem(float a, float b)
|
||||
{
|
||||
return a % b;
|
||||
}
|
||||
|
||||
public static double Rem(double a, double b)
|
||||
{
|
||||
return a % b;
|
||||
}
|
||||
|
||||
public static int RemUn(int a, int b)
|
||||
{
|
||||
return (int)((uint)a % (uint)b);
|
||||
}
|
||||
|
||||
public static long RemUn(long a, long b)
|
||||
{
|
||||
return (long)((ulong)a % (ulong)b);
|
||||
}
|
||||
|
||||
public static int Negate(int a)
|
||||
{
|
||||
return -a;
|
||||
}
|
||||
|
||||
public static long Negate(long a)
|
||||
{
|
||||
return -a;
|
||||
}
|
||||
|
||||
public static float Negate(float a)
|
||||
{
|
||||
return -a;
|
||||
}
|
||||
|
||||
public static double Negate(double a)
|
||||
{
|
||||
return -a;
|
||||
}
|
||||
|
||||
public static int And(int a, int b)
|
||||
{
|
||||
return a & b;
|
||||
}
|
||||
|
||||
public static long And(long a, long b)
|
||||
{
|
||||
return a & b;
|
||||
}
|
||||
|
||||
public static int Or(int a, int b)
|
||||
{
|
||||
return a | b;
|
||||
}
|
||||
|
||||
public static long Or(long a, long b)
|
||||
{
|
||||
return a | b;
|
||||
}
|
||||
|
||||
public static int Xor(int a, int b)
|
||||
{
|
||||
return a ^ b;
|
||||
}
|
||||
|
||||
public static long Xor(long a, long b)
|
||||
{
|
||||
return a ^ b;
|
||||
}
|
||||
|
||||
public static int Not(int a)
|
||||
{
|
||||
return ~a;
|
||||
}
|
||||
|
||||
public static long Not(long a)
|
||||
{
|
||||
return ~a;
|
||||
}
|
||||
|
||||
public static int ShiftLeft(int a, int b)
|
||||
{
|
||||
return a << b;
|
||||
}
|
||||
|
||||
public static long ShiftLeft(long a, int b)
|
||||
{
|
||||
return a << b;
|
||||
}
|
||||
|
||||
public static int ShiftRight(int a, int b)
|
||||
{
|
||||
return a >> b;
|
||||
}
|
||||
|
||||
public static long ShiftRight(long a, int b)
|
||||
{
|
||||
return a >> b;
|
||||
}
|
||||
|
||||
public static int ShiftRightUn(int a, int b)
|
||||
{
|
||||
return (int)((uint)a >> b);
|
||||
}
|
||||
|
||||
public static long ShiftRightUn(long a, int b)
|
||||
{
|
||||
return (long)((ulong)a >> b);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9aba5050818a0224696fcf73752fb225
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,30 @@
|
||||
namespace Obfuz
|
||||
{
|
||||
public interface IEncryptor
|
||||
{
|
||||
int OpCodeCount { get; }
|
||||
|
||||
void EncryptBlock(byte[] data, int ops, int salt);
|
||||
void DecryptBlock(byte[] data, int 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);
|
||||
|
||||
byte[] Encrypt(byte[] value, int offset, int length, int opts, int salt);
|
||||
byte[] Decrypt(byte[] value, int offset, int byteLength, int ops, int salt);
|
||||
|
||||
byte[] Encrypt(string value, int ops, int salt);
|
||||
string DecryptString(byte[] value, int offset, int stringBytesLength, int ops, int salt);
|
||||
|
||||
void DecryptInitializeArray(System.Array arr, System.RuntimeFieldHandle field, int length, int ops, int salt);
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3078fa59ff0af6b4cbbee25e20bc41c1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
public class NullEncryptor : EncryptorBase
|
||||
{
|
||||
private readonly byte[] _key;
|
||||
|
||||
public override int OpCodeCount => 256;
|
||||
|
||||
public NullEncryptor(byte[] key)
|
||||
{
|
||||
_key = key;
|
||||
}
|
||||
|
||||
public override int Encrypt(int value, int opts, int salt)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
public override int Decrypt(int value, int opts, int salt)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
public override long Encrypt(long value, int opts, int salt)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
public override long Decrypt(long value, int opts, int salt)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
public override float Encrypt(float value, int opts, int salt)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
public override float Decrypt(float value, int opts, int salt)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
public override double Encrypt(double value, int opts, int salt)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
public override double Decrypt(double value, int opts, int salt)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
public override byte[] Encrypt(byte[] value, int offset, int length, int opts, int salt)
|
||||
{
|
||||
if (length == 0)
|
||||
{
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
var encryptedBytes = new byte[length];
|
||||
Buffer.BlockCopy(value, offset, encryptedBytes, 0, length);
|
||||
return encryptedBytes;
|
||||
}
|
||||
|
||||
public override byte[] Decrypt(byte[] value, int offset, int length, int ops, int salt)
|
||||
{
|
||||
if (length == 0)
|
||||
{
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
byte[] byteArr = new byte[length];
|
||||
Buffer.BlockCopy(value, 0, byteArr, 0, length);
|
||||
return byteArr;
|
||||
}
|
||||
|
||||
public override byte[] Encrypt(string value, int ops, int salt)
|
||||
{
|
||||
return Encoding.UTF8.GetBytes(value);
|
||||
}
|
||||
|
||||
public override string DecryptString(byte[] value, int offset, int length, int ops, int salt)
|
||||
{
|
||||
return Encoding.UTF8.GetString(value, offset, length);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c53481f2ec513be4783a5ae2f76dc6e7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,34 @@
|
||||
namespace Obfuz
|
||||
{
|
||||
public static class ObfuscationInstincts
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the original full name before obfuscated of the type T
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static string FullNameOf<T>()
|
||||
{
|
||||
return typeof(T).FullName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the original name before obfuscated of the type T
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static string NameOf<T>()
|
||||
{
|
||||
return typeof(T).Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// register original type name to type mapping.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static void RegisterReflectionType<T>()
|
||||
{
|
||||
ObfuscationTypeMapper.RegisterType<T>(typeof(T).FullName);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 84320ab4adc4cbc49bf5e8f4009b4a96
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
public static class ObfuscationTypeMapper
|
||||
{
|
||||
private static readonly Dictionary<Type, string> _type2OriginalFullName = new Dictionary<Type, string>();
|
||||
private static readonly Dictionary<Assembly, Dictionary<string, Type>> _originalFullName2Types = new Dictionary<Assembly, Dictionary<string, Type>>();
|
||||
|
||||
internal static void RegisterType<T>(string originalFullName)
|
||||
{
|
||||
RegisterType(typeof(T), originalFullName);
|
||||
}
|
||||
|
||||
internal static void RegisterType(Type type, string originalFullName)
|
||||
{
|
||||
if (_type2OriginalFullName.ContainsKey(type))
|
||||
{
|
||||
throw new ArgumentException($"Type '{type.FullName}' is already registered with original name '{_type2OriginalFullName[type]}'.");
|
||||
}
|
||||
_type2OriginalFullName.Add(type, originalFullName);
|
||||
Assembly assembly = type.Assembly;
|
||||
if (!_originalFullName2Types.TryGetValue(assembly, out var originalFullName2Types))
|
||||
{
|
||||
originalFullName2Types = new Dictionary<string, Type>();
|
||||
_originalFullName2Types[assembly] = originalFullName2Types;
|
||||
}
|
||||
if (originalFullName2Types.ContainsKey(originalFullName))
|
||||
{
|
||||
throw new ArgumentException($"Original full name '{originalFullName}' is already registered with type '{originalFullName2Types[originalFullName].FullName}'.");
|
||||
}
|
||||
originalFullName2Types.Add(originalFullName, type);
|
||||
}
|
||||
|
||||
public static string GetOriginalTypeFullName(Type type)
|
||||
{
|
||||
return _type2OriginalFullName.TryGetValue(type, out string originalFullName)
|
||||
? originalFullName
|
||||
: throw new KeyNotFoundException($"Type '{type.FullName}' not found in the obfuscation mapping.");
|
||||
}
|
||||
|
||||
public static string GetOriginalTypeFullNameOrCurrent(Type type)
|
||||
{
|
||||
if (_type2OriginalFullName.TryGetValue(type, out string originalFullName))
|
||||
{
|
||||
return originalFullName;
|
||||
}
|
||||
return type.FullName;
|
||||
}
|
||||
|
||||
public static Type GetTypeByOriginalFullName(Assembly assembly, string originalFullName)
|
||||
{
|
||||
if (_originalFullName2Types.TryGetValue(assembly, out var n2t))
|
||||
{
|
||||
if (n2t.TryGetValue(originalFullName, out Type type))
|
||||
{
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void Clear()
|
||||
{
|
||||
_type2OriginalFullName.Clear();
|
||||
_originalFullName2Types.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: db6168acedd85984fa2c197fee1b0c15
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "Obfuz.Runtime",
|
||||
"rootNamespace": "",
|
||||
"references": [],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": true,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4140bd2e2764f1f47ab93125ecb61942
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
|
||||
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)]
|
||||
public class ObfuzIgnoreAttribute : Attribute
|
||||
{
|
||||
public ObfuzScope Scope { get; set; }
|
||||
|
||||
public bool ApplyToNestedTypes { get; set; } = true;
|
||||
|
||||
public bool ApplyToChildTypes { get; set; } = false;
|
||||
|
||||
public ObfuzIgnoreAttribute(ObfuzScope scope = ObfuzScope.All)
|
||||
{
|
||||
this.Scope = scope;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c2b4cf04729157b4dab504167ab5f703
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
[Flags]
|
||||
public enum ObfuzScope
|
||||
{
|
||||
None = 0x0,
|
||||
TypeName = 0x1,
|
||||
Field = 0x2,
|
||||
MethodName = 0x4,
|
||||
MethodParameter = 0x8,
|
||||
MethodBody = 0x10,
|
||||
Method = MethodName | MethodParameter | MethodBody,
|
||||
PropertyName = 0x20,
|
||||
PropertyGetterSetterName = 0x40,
|
||||
Property = PropertyName | PropertyGetterSetterName,
|
||||
EventName = 0x100,
|
||||
EventAddRemoveFireName = 0x200,
|
||||
Event = EventName | PropertyGetterSetterName,
|
||||
Module = 0x1000,
|
||||
All = TypeName | Field | Method | Property | Event,
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3c7e51fe12f206347b08a4b0be48605d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user