2025-10-29 20:40:37 +08:00
|
|
|
|
// Copyright 2025 Code Philosophy
|
|
|
|
|
|
//
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
|
//
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
//
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
|
// SOFTWARE.
|
|
|
|
|
|
|
2025-05-21 09:23:29 +08:00
|
|
|
|
using Obfuz.Utils;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Obfuz.EncryptionVM
|
|
|
|
|
|
{
|
|
|
|
|
|
public class VirtualMachineCodeGenerator
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly int _opCodeCount;
|
|
|
|
|
|
private readonly int _opCodeBits;
|
|
|
|
|
|
private readonly VirtualMachine _vm;
|
|
|
|
|
|
|
|
|
|
|
|
public VirtualMachineCodeGenerator(string vmCodeGenerateSecretKey, int opCodeCount)
|
|
|
|
|
|
{
|
|
|
|
|
|
_opCodeCount = opCodeCount;
|
|
|
|
|
|
_opCodeBits = EncryptionUtil.GetBitCount(opCodeCount - 1);
|
|
|
|
|
|
_vm = new VirtualMachineCreator(vmCodeGenerateSecretKey).CreateVirtualMachine(opCodeCount);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public VirtualMachineCodeGenerator(VirtualMachine vm)
|
|
|
|
|
|
{
|
|
|
|
|
|
_opCodeCount = vm.opCodes.Length;
|
|
|
|
|
|
_opCodeBits = EncryptionUtil.GetBitCount(_opCodeCount - 1);
|
|
|
|
|
|
_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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Generate(string outputFile)
|
|
|
|
|
|
{
|
|
|
|
|
|
FileUtil.CreateParentDir(outputFile);
|
|
|
|
|
|
|
|
|
|
|
|
string code = GenerateCode();
|
|
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(outputFile, code, Encoding.UTF8);
|
|
|
|
|
|
Debug.Log($"Generate EncryptionVM code to {outputFile}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-10-16 22:39:04 +08:00
|
|
|
|
lines.Add($@" case {opCode.code}:
|
2025-05-30 13:37:21 +08:00
|
|
|
|
{{
|
|
|
|
|
|
// {opCode.function.GetType().Name}");
|
2025-05-21 09:23:29 +08:00
|
|
|
|
AppendEncryptCode(lines, opCode.function);
|
2025-05-30 13:37:21 +08:00
|
|
|
|
lines.Add(@" return value;
|
|
|
|
|
|
}");
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lines.Add(@"
|
2025-10-16 22:39:04 +08:00
|
|
|
|
default: throw new System.Exception($""Invalid opCode:{opCode}"");
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-10-16 22:39:04 +08:00
|
|
|
|
lines.Add($@" case {opCode.code}:
|
2025-05-30 13:37:21 +08:00
|
|
|
|
{{
|
|
|
|
|
|
// {opCode.function.GetType().Name}");
|
2025-05-21 09:23:29 +08:00
|
|
|
|
AppendDecryptCode(lines, opCode.function);
|
2025-05-30 13:37:21 +08:00
|
|
|
|
lines.Add(@" return value;
|
|
|
|
|
|
}");
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lines.Add(@"
|
2025-10-16 22:39:04 +08:00
|
|
|
|
default: throw new System.Exception($""Invalid opCode:{opCode}"");
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
{");
|
|
|
|
|
|
lines.Add($@"
|
|
|
|
|
|
private const int kOpCodeBits = {_opCodeBits};
|
|
|
|
|
|
|
|
|
|
|
|
private const int kOpCodeCount = {_opCodeCount};
|
|
|
|
|
|
|
|
|
|
|
|
private const int kOpCodeMask = {_opCodeCount - 1};
|
|
|
|
|
|
");
|
|
|
|
|
|
lines.Add(@"
|
|
|
|
|
|
|
|
|
|
|
|
private readonly int[] _secretKey;
|
|
|
|
|
|
|
|
|
|
|
|
public GeneratedEncryptionVirtualMachine(byte[] secretKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
this._secretKey = ConvertToIntKey(secretKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override int OpCodeCount => kOpCodeCount;
|
|
|
|
|
|
|
|
|
|
|
|
public override int Encrypt(int value, int opts, int salt)
|
|
|
|
|
|
{
|
|
|
|
|
|
uint uopts = (uint)opts;
|
|
|
|
|
|
uint revertOps = 0;
|
|
|
|
|
|
while (uopts != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
uint opCode = uopts & kOpCodeMask;
|
|
|
|
|
|
revertOps <<= kOpCodeBits;
|
|
|
|
|
|
revertOps |= opCode;
|
|
|
|
|
|
uopts >>= kOpCodeBits;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (revertOps != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
uint opCode = revertOps & kOpCodeMask;
|
|
|
|
|
|
value = ExecuteEncrypt(value, (int)opCode, salt);
|
|
|
|
|
|
revertOps >>= kOpCodeBits;
|
|
|
|
|
|
}
|
|
|
|
|
|
return value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override int Decrypt(int value, int opts, int salt)
|
|
|
|
|
|
{
|
|
|
|
|
|
uint uopts = (uint)opts;
|
|
|
|
|
|
while (uopts != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
uint opCode = uopts & kOpCodeMask;
|
|
|
|
|
|
value = ExecuteDecrypt(value, (int)opCode, salt);
|
|
|
|
|
|
uopts >>= kOpCodeBits;
|
|
|
|
|
|
}
|
|
|
|
|
|
return value;
|
|
|
|
|
|
}
|
|
|
|
|
|
");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void AppendTailer(List<string> lines)
|
|
|
|
|
|
{
|
|
|
|
|
|
lines.Add(@"
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void AppendEncryptCode(List<string> lines, IEncryptionInstruction instruction)
|
|
|
|
|
|
{
|
2025-05-30 13:37:21 +08:00
|
|
|
|
instruction.GenerateEncryptCode(lines, " ");
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void AppendDecryptCode(List<string> lines, IEncryptionInstruction instruction)
|
|
|
|
|
|
{
|
2025-05-30 13:37:21 +08:00
|
|
|
|
instruction.GenerateDecryptCode(lines, " ");
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|