2025-05-11 17:36:58 +08:00
|
|
|
|
using Obfuz.Utils;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2025-05-11 19:28:19 +08:00
|
|
|
|
using System.IO;
|
2025-05-11 17:36:58 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2025-05-11 19:28:19 +08:00
|
|
|
|
using System.Text.RegularExpressions;
|
2025-05-11 17:36:58 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2025-05-11 19:28:19 +08:00
|
|
|
|
using UnityEngine;
|
2025-05-11 17:36:58 +08:00
|
|
|
|
|
|
|
|
|
namespace Obfuz.EncryptionVM
|
|
|
|
|
{
|
|
|
|
|
public class VirtualMachineCodeGenerator
|
|
|
|
|
{
|
2025-05-11 19:28:19 +08:00
|
|
|
|
private readonly int _opCodeCount;
|
|
|
|
|
private readonly int _opCodeBits;
|
|
|
|
|
private readonly VirtualMachine _vm;
|
2025-05-11 17:36:58 +08:00
|
|
|
|
|
2025-05-11 19:28:19 +08:00
|
|
|
|
public VirtualMachineCodeGenerator(string vmCodeGenerateSecretKey, int opCodeCount)
|
2025-05-11 17:36:58 +08:00
|
|
|
|
{
|
2025-05-11 19:28:19 +08:00
|
|
|
|
_opCodeCount = opCodeCount;
|
2025-05-11 20:12:33 +08:00
|
|
|
|
_opCodeBits = EncryptionUtil.GetBitCount(opCodeCount - 1);
|
2025-05-11 19:28:19 +08:00
|
|
|
|
_vm = new VirtualMachineCreator(vmCodeGenerateSecretKey).CreateVirtualMachine(opCodeCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public VirtualMachineCodeGenerator(VirtualMachine vm)
|
|
|
|
|
{
|
|
|
|
|
_opCodeCount = vm.opCodes.Length;
|
2025-05-11 20:12:33 +08:00
|
|
|
|
_opCodeBits = EncryptionUtil.GetBitCount(_opCodeCount - 1);
|
2025-05-11 19:28:19 +08:00
|
|
|
|
_vm = vm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool ValidateMatch(string outputFile)
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(outputFile))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
string oldCode = NormalizeText(File.ReadAllText(outputFile, Encoding.UTF8));
|
|
|
|
|
string newCode = NormalizeText(GenerateCode());
|
|
|
|
|
return oldCode == newCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string NormalizeText(string input)
|
|
|
|
|
{
|
|
|
|
|
return Regex.Replace(input, @"\s+", string.Empty);
|
2025-05-11 17:36:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Generate(string outputFile)
|
|
|
|
|
{
|
2025-05-11 19:28:19 +08:00
|
|
|
|
FileUtil.CreateParentDir(outputFile);
|
|
|
|
|
|
|
|
|
|
string code = GenerateCode();
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(outputFile, code, Encoding.UTF8);
|
|
|
|
|
Debug.Log($"Generate EncryptionVM code to {outputFile}");
|
|
|
|
|
UnityEditor.AssetDatabase.Refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GenerateCode()
|
|
|
|
|
{
|
|
|
|
|
var lines = new List<string>();
|
|
|
|
|
AppendHeader(lines);
|
|
|
|
|
AppendEncryptCodes(lines);
|
|
|
|
|
AppendDecryptCodes(lines);
|
|
|
|
|
AppendTailer(lines);
|
|
|
|
|
return string.Join("\n", lines);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AppendEncryptCodes(List<string> lines)
|
|
|
|
|
{
|
|
|
|
|
lines.Add(@"
|
|
|
|
|
private int ExecuteEncrypt(int value, int opCode, int salt)
|
|
|
|
|
{
|
|
|
|
|
switch (opCode)
|
|
|
|
|
{");
|
|
|
|
|
foreach (var opCode in _vm.opCodes)
|
|
|
|
|
{
|
2025-05-16 17:44:28 +08:00
|
|
|
|
lines.Add($@" case {opCode.code}:
|
2025-05-13 20:10:00 +08:00
|
|
|
|
{{
|
|
|
|
|
// {opCode.function.GetType().Name}");
|
2025-05-11 19:28:19 +08:00
|
|
|
|
AppendEncryptCode(lines, opCode.function);
|
2025-05-13 20:10:00 +08:00
|
|
|
|
lines.Add(@" return value;
|
2025-05-11 19:28:19 +08:00
|
|
|
|
}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lines.Add(@"
|
|
|
|
|
default:
|
|
|
|
|
throw new System.Exception($""Invalid opCode:{opCode}"");
|
|
|
|
|
}
|
|
|
|
|
}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AppendDecryptCodes(List<string> lines)
|
|
|
|
|
{
|
|
|
|
|
lines.Add(@"
|
|
|
|
|
private int ExecuteDecrypt(int value, int opCode, int salt)
|
|
|
|
|
{
|
|
|
|
|
switch (opCode)
|
|
|
|
|
{");
|
|
|
|
|
foreach (var opCode in _vm.opCodes)
|
|
|
|
|
{
|
2025-05-16 17:44:28 +08:00
|
|
|
|
lines.Add($@" case {opCode.code}:
|
2025-05-13 20:10:00 +08:00
|
|
|
|
{{
|
|
|
|
|
// {opCode.function.GetType().Name}");
|
2025-05-11 19:28:19 +08:00
|
|
|
|
AppendDecryptCode(lines, opCode.function);
|
2025-05-13 20:10:00 +08:00
|
|
|
|
lines.Add(@" return value;
|
2025-05-11 19:28:19 +08:00
|
|
|
|
}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lines.Add(@"
|
|
|
|
|
default:
|
|
|
|
|
throw new System.Exception($""Invalid opCode:{opCode}"");
|
|
|
|
|
}
|
|
|
|
|
}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AppendHeader(List<string> lines)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
lines.Add($"/// This file is auto-generated by Obfuz. Do not modify it.");
|
|
|
|
|
lines.Add($"///");
|
|
|
|
|
//lines.Add($"/// Created Time: {DateTime.Now}");
|
|
|
|
|
|
|
|
|
|
lines.Add($"/// Version: {_vm.version}");
|
|
|
|
|
lines.Add($"/// SecretKey: {_vm.codeGenerationSecretKey}");
|
|
|
|
|
lines.Add($"/// OpCodeCount: {_vm.opCodes.Length}");
|
|
|
|
|
|
|
|
|
|
lines.Add(@"
|
|
|
|
|
namespace Obfuz.EncryptionVM
|
|
|
|
|
{
|
|
|
|
|
public class GeneratedEncryptionVirtualMachine : Obfuz.EncryptorBase
|
|
|
|
|
{");
|
2025-05-16 17:44:28 +08:00
|
|
|
|
lines.Add($@"
|
2025-05-11 20:12:33 +08:00
|
|
|
|
private const int kOpCodeBits = {_opCodeBits};
|
2025-05-11 19:28:19 +08:00
|
|
|
|
|
2025-05-11 20:12:33 +08:00
|
|
|
|
private const int kOpCodeCount = {_opCodeCount};
|
2025-05-11 19:28:19 +08:00
|
|
|
|
|
2025-05-11 20:12:33 +08:00
|
|
|
|
private const int kOpCodeMask = {_opCodeCount - 1};
|
2025-05-11 19:28:19 +08:00
|
|
|
|
");
|
|
|
|
|
lines.Add(@"
|
|
|
|
|
|
|
|
|
|
private readonly int[] _secretKey;
|
|
|
|
|
|
|
|
|
|
public GeneratedEncryptionVirtualMachine(byte[] secretKey)
|
|
|
|
|
{
|
|
|
|
|
this._secretKey = ConvertToIntKey(secretKey);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-11 20:12:33 +08:00
|
|
|
|
public override int OpCodeCount => kOpCodeCount;
|
|
|
|
|
|
2025-05-11 19:28:19 +08:00
|
|
|
|
public override int Encrypt(int value, int opts, int salt)
|
|
|
|
|
{
|
2025-05-14 12:21:25 +08:00
|
|
|
|
uint uopts = (uint)opts;
|
|
|
|
|
uint revertOps = 0;
|
|
|
|
|
while (uopts != 0)
|
2025-05-11 19:28:19 +08:00
|
|
|
|
{
|
2025-05-14 12:21:25 +08:00
|
|
|
|
uint opCode = uopts & kOpCodeMask;
|
2025-05-14 10:46:42 +08:00
|
|
|
|
revertOps <<= kOpCodeBits;
|
|
|
|
|
revertOps |= opCode;
|
2025-05-14 12:21:25 +08:00
|
|
|
|
uopts >>= kOpCodeBits;
|
2025-05-11 19:28:19 +08:00
|
|
|
|
}
|
2025-05-14 10:46:42 +08:00
|
|
|
|
|
2025-05-14 12:21:25 +08:00
|
|
|
|
while (revertOps != 0)
|
2025-05-14 10:46:42 +08:00
|
|
|
|
{
|
2025-05-14 12:21:25 +08:00
|
|
|
|
uint opCode = revertOps & kOpCodeMask;
|
|
|
|
|
value = ExecuteEncrypt(value, (int)opCode, salt);
|
2025-05-14 10:46:42 +08:00
|
|
|
|
revertOps >>= kOpCodeBits;
|
|
|
|
|
}
|
2025-05-11 19:28:19 +08:00
|
|
|
|
return value;
|
|
|
|
|
}
|
2025-05-11 17:36:58 +08:00
|
|
|
|
|
2025-05-11 19:28:19 +08:00
|
|
|
|
public override int Decrypt(int value, int opts, int salt)
|
|
|
|
|
{
|
2025-05-14 12:21:25 +08:00
|
|
|
|
uint uopts = (uint)opts;
|
|
|
|
|
while (uopts != 0)
|
2025-05-11 19:28:19 +08:00
|
|
|
|
{
|
2025-05-14 12:21:25 +08:00
|
|
|
|
uint opCode = uopts & kOpCodeMask;
|
|
|
|
|
value = ExecuteDecrypt(value, (int)opCode, salt);
|
|
|
|
|
uopts >>= kOpCodeBits;
|
2025-05-11 19:28:19 +08:00
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AppendTailer(List<string> lines)
|
|
|
|
|
{
|
|
|
|
|
lines.Add(@"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AppendEncryptCode(List<string> lines, IEncryptionInstruction instruction)
|
|
|
|
|
{
|
|
|
|
|
instruction.GenerateEncryptCode(lines, " ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AppendDecryptCode(List<string> lines, IEncryptionInstruction instruction)
|
|
|
|
|
{
|
|
|
|
|
instruction.GenerateDecryptCode(lines, " ");
|
2025-05-11 17:36:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|