obfuz/Editor/EncryptionVM/VirtualMachineSimulator.cs

97 lines
3.1 KiB
C#
Raw Permalink Normal View History

2025-05-11 17:36:58 +08:00
using Obfuz.Utils;
using System;
2025-05-11 10:37:42 +08:00
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Text;
using UnityEngine;
2025-05-11 10:37:42 +08:00
using UnityEngine.Assertions;
using UnityEngine.UIElements;
2025-05-11 12:53:24 +08:00
namespace Obfuz.EncryptionVM
2025-05-11 10:37:42 +08:00
{
2025-05-11 12:53:24 +08:00
public class VirtualMachineSimulator : EncryptorBase
2025-05-11 10:37:42 +08:00
{
2025-05-11 12:48:53 +08:00
private readonly EncryptionInstructionWithOpCode[] _opCodes;
2025-05-11 10:37:42 +08:00
private readonly int[] _secretKey;
public override int OpCodeCount => _opCodes.Length;
2025-05-11 17:36:58 +08:00
public VirtualMachineSimulator(VirtualMachine vm, byte[] byteSecretKey)
2025-05-11 10:37:42 +08:00
{
2025-05-11 12:48:53 +08:00
_opCodes = vm.opCodes;
2025-05-11 17:36:58 +08:00
_secretKey = KeyGenerator.ConvertToIntKey(byteSecretKey);
2025-05-11 12:48:53 +08:00
VerifyInstructions();
}
private void VerifyInstructions()
{
int value = 0x11223344;
for (int i = 0; i < _opCodes.Length; i++)
{
int encryptedValue = _opCodes[i].Encrypt(value, _secretKey, i);
int decryptedValue = _opCodes[i].Decrypt(encryptedValue, _secretKey, i);
2025-05-14 14:36:25 +08:00
//Debug.Log($"instruction type:{_opCodes[i].function.GetType()}");
2025-05-11 12:48:53 +08:00
Assert.AreEqual(value, decryptedValue);
}
int ops = 11223344;
int salt = 789;
Assert.AreEqual(1, Decrypt(Encrypt(1, ops, salt), ops, salt));
Assert.AreEqual(1L, Decrypt(Encrypt(1L, ops, salt), ops, salt));
Assert.AreEqual(1.0f, Decrypt(Encrypt(1.0f, ops, salt), ops, salt));
Assert.AreEqual(1.0, Decrypt(Encrypt(1.0, ops, salt), ops, salt));
byte[] strBytes = Encrypt("abcdef", ops, salt);
Assert.AreEqual("abcdef", DecryptString(strBytes, 0, strBytes.Length, ops, salt));
var arr = new byte[100];
for (int i = 0; i < arr.Length ; i++)
{
arr[i] = (byte)i;
}
EncryptBlock(arr, ops, salt);
DecryptBlock(arr, ops, salt);
for (int i = 0; i < arr.Length; i++)
{
Assert.AreEqual(i, arr[i]);
}
2025-05-11 10:37:42 +08:00
}
private List<uint> DecodeOps(uint ops)
2025-05-11 10:37:42 +08:00
{
var codes = new List<uint>();
while (ops != 0)
2025-05-11 10:37:42 +08:00
{
uint code = ops % (uint)_opCodes.Length;
2025-05-11 10:37:42 +08:00
codes.Add(code);
ops /= (uint)_opCodes.Length;
2025-05-11 10:37:42 +08:00
}
return codes;
}
public override int Encrypt(int value, int ops, int salt)
{
var codes = DecodeOps((uint)ops);
for (int i = codes.Count - 1; i >= 0; i--)
2025-05-11 10:37:42 +08:00
{
var opCode = _opCodes[codes[i]];
2025-05-11 10:37:42 +08:00
value = opCode.Encrypt(value, _secretKey, salt);
}
return value;
}
public override int Decrypt(int value, int ops, int salt)
{
var codes = DecodeOps((uint)ops);
foreach (var code in codes)
2025-05-11 10:37:42 +08:00
{
var opCode = _opCodes[code];
2025-05-11 10:37:42 +08:00
value = opCode.Decrypt(value, _secretKey, salt);
}
return value;
}
}
}