diff --git a/Editor/EncryptionVM/Instructions/AddRotateXorInstruction.cs b/Editor/EncryptionVM/Instructions/AddRotateXorInstruction.cs new file mode 100644 index 0000000..099a566 --- /dev/null +++ b/Editor/EncryptionVM/Instructions/AddRotateXorInstruction.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz.EncryptionVM.Instructions +{ + public class AddRotateXorInstruction : EncryptionInstructionBase + { + // x = x + p1 + secretKey[index1]; + // x = Rotate(x, p2) + // x = x ^ p3 ^ salt; + + private readonly int _addValue; + private readonly int _index1; + private readonly int _rotateBitNum; + private readonly int _xorValue; + + public AddRotateXorInstruction(int addValue, int index1, int rotateBitNum, int xorValue) + { + _addValue = addValue; + _index1 = index1; + _rotateBitNum = rotateBitNum; + _xorValue = xorValue; + } + + public override int Encrypt(int value, int[] secretKey, int salt) + { + value += _addValue + secretKey[_index1]; + uint part1 = (uint)value << _rotateBitNum; + uint part2 = (uint)value >> (32 - _rotateBitNum); + value = (int)(part1 | part2); + value ^= _xorValue ^ salt; + return value; + } + + public override int Decrypt(int value, int[] secretKey, int salt) + { + value ^= _xorValue ^ salt; + uint value2 = (uint)value >> _rotateBitNum; + uint part1 = (uint)value << (32 - _rotateBitNum); + value = (int)(value2 | part1); + value -= _addValue + secretKey[_index1]; + return value; + } + + public override void GenerateEncryptCode(List lines, string indent) + { + lines.Add(indent + $"value += {_addValue} + _secretKey[{_index1}];"); + lines.Add(indent + $"uint part1 = (uint)value << {_rotateBitNum};"); + lines.Add(indent + $"uint part2 = (uint)value >> (32 - {_rotateBitNum});"); + lines.Add(indent + $"value = (int)(part1 | part2);"); + lines.Add(indent + $"value ^= {_xorValue} ^ salt;"); + } + + public override void GenerateDecryptCode(List lines, string indent) + { + lines.Add(indent + $"value ^= {_xorValue} ^ salt;"); + lines.Add(indent + $"uint value2 = (uint)value >> {_rotateBitNum};"); + lines.Add(indent + $"uint part1 = (uint)value << (32 - {_rotateBitNum});"); + lines.Add(indent + $"value = (int)(value2 | part1);"); + lines.Add(indent + $"value -= {_addValue} + _secretKey[{_index1}];"); + } + } +} diff --git a/Editor/EncryptionVM/Instructions/AddXorRotateInstruction.cs b/Editor/EncryptionVM/Instructions/AddXorRotateInstruction.cs new file mode 100644 index 0000000..cc453a7 --- /dev/null +++ b/Editor/EncryptionVM/Instructions/AddXorRotateInstruction.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz.EncryptionVM.Instructions +{ + public class AddXorRotateInstruction : EncryptionInstructionBase + { + // x = x + p1 + secretKey[index1]; + // x = x ^ p3 ^ salt; + // x = Rotate(x, p2) + + private readonly int _addValue; + private readonly int _index1; + private readonly int _rotateBitNum; + private readonly int _xorValue; + + public AddXorRotateInstruction(int addValue, int index1, int xorValue, int rotateBitNum) + { + _addValue = addValue; + _index1 = index1; + _rotateBitNum = rotateBitNum; + _xorValue = xorValue; + } + + public override int Encrypt(int value, int[] secretKey, int salt) + { + value += _addValue + secretKey[_index1]; + value ^= _xorValue ^ salt; + uint part1 = (uint)value << _rotateBitNum; + uint part2 = (uint)value >> (32 - _rotateBitNum); + value = (int)(part1 | part2); + return value; + } + + public override int Decrypt(int value, int[] secretKey, int salt) + { + uint value2 = (uint)value >> _rotateBitNum; + uint part1 = (uint)value << (32 - _rotateBitNum); + value = (int)(value2 | part1); + value ^= _xorValue ^ salt; + value -= _addValue + secretKey[_index1]; + return value; + } + + public override void GenerateEncryptCode(List lines, string indent) + { + lines.Add(indent + $"value += {_addValue} + _secretKey[{_index1}];"); + lines.Add(indent + $"value ^= {_xorValue} ^ salt;"); + lines.Add(indent + $"uint part1 = (uint)value << {_rotateBitNum};"); + lines.Add(indent + $"uint part2 = (uint)value >> (32 - {_rotateBitNum});"); + lines.Add(indent + $"value = (int)(part1 | part2);"); + } + + public override void GenerateDecryptCode(List lines, string indent) + { + lines.Add(indent + $"uint part1 = (uint)value >> {_rotateBitNum};"); + lines.Add(indent + $"uint part2 = (uint)value << (32 - {_rotateBitNum});"); + lines.Add(indent + $"value = (int)(part1 | part2);"); + lines.Add(indent + $"value ^= {_xorValue} ^ salt;"); + lines.Add(indent + $"value -= {_addValue} + _secretKey[{_index1}];"); + } + } +} diff --git a/Editor/EncryptionVM/Instructions/MultipleInstruction.cs b/Editor/EncryptionVM/Instructions/MultipleInstruction.cs new file mode 100644 index 0000000..2635f5e --- /dev/null +++ b/Editor/EncryptionVM/Instructions/MultipleInstruction.cs @@ -0,0 +1,64 @@ +using NUnit.Framework; +using System; +using System.Collections.Generic; + +namespace Obfuz.EncryptionVM.Instructions +{ + public class MultipleInstruction : EncryptionInstructionBase + { + private readonly int _multiValue; + private readonly int _revertMultiValue; + private readonly int _opKeyIndex; + + public MultipleInstruction(int addValue, int opKeyIndex) + { + _multiValue = addValue; + _opKeyIndex = opKeyIndex; + _revertMultiValue = (int)ModInverseOdd((uint)addValue); + Verify(); + } + + private void Verify() + { + int a = 1122334; + Assert.AreEqual(a, a * _multiValue * _revertMultiValue); + } + + public static uint ModInverseOdd(uint a) + { + if (a % 2 == 0) + throw new ArgumentException("Input must be an odd number.", nameof(a)); + + uint x = 1; // 初始解:x₀ = 1 (mod 2) + for (int i = 0; i < 5; i++) // 迭代5次(2^1 → 2^32) + { + int shift = 2 << i; // 当前模数为 2^(2^(i+1)) + ulong mod = 1UL << shift; // 使用 ulong 避免溢出 + ulong ax = (ulong)a * x; // 计算 a*x(64位避免截断) + ulong term = (2 - ax) % mod; + x = (uint)((x * term) % mod); // 更新 x,结果截断为 uint + } + return x; // 最终解为 x₅ mod 2^32 + } + + public override int Encrypt(int value, int[] secretKey, int salt) + { + return value * _multiValue + secretKey[_opKeyIndex] + salt; + } + + public override int Decrypt(int value, int[] secretKey, int salt) + { + return (value - secretKey[_opKeyIndex] - salt) * _revertMultiValue; + } + + public override void GenerateEncryptCode(List lines, string indent) + { + lines.Add(indent + $"value = value * {_multiValue} + _secretKey[{_opKeyIndex}] + salt;"); + } + + public override void GenerateDecryptCode(List lines, string indent) + { + lines.Add(indent + $"value = (value - _secretKey[{_opKeyIndex}] - salt) * {_revertMultiValue};"); + } + } +} diff --git a/Editor/EncryptionVM/Instructions/XorAddRotateInstruction.cs b/Editor/EncryptionVM/Instructions/XorAddRotateInstruction.cs new file mode 100644 index 0000000..10a9226 --- /dev/null +++ b/Editor/EncryptionVM/Instructions/XorAddRotateInstruction.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz.EncryptionVM.Instructions +{ + public class XorAddRotateInstruction : EncryptionInstructionBase + { + // x = x ^ p3 ^ salt; + // x = x + p1 + secretKey[index1]; + // x = Rotate(x, p2) + + private readonly int _addValue; + private readonly int _index1; + private readonly int _rotateBitNum; + private readonly int _xorValue; + + public XorAddRotateInstruction(int xorValue, int addValue, int index1, int rotateBitNum) + { + _addValue = addValue; + _index1 = index1; + _rotateBitNum = rotateBitNum; + _xorValue = xorValue; + } + + public override int Encrypt(int value, int[] secretKey, int salt) + { + value ^= _xorValue ^ salt; + value += _addValue + secretKey[_index1]; + uint part1 = (uint)value << _rotateBitNum; + uint part2 = (uint)value >> (32 - _rotateBitNum); + value = (int)(part1 | part2); + return value; + } + + public override int Decrypt(int value, int[] secretKey, int salt) + { + uint value2 = (uint)value >> _rotateBitNum; + uint part1 = (uint)value << (32 - _rotateBitNum); + value = (int)(value2 | part1); + value -= _addValue + secretKey[_index1]; + value ^= _xorValue ^ salt; + return value; + } + + public override void GenerateEncryptCode(List lines, string indent) + { + lines.Add(indent + $"value ^= {_xorValue} ^ salt;"); + lines.Add(indent + $"value += {_addValue} + _secretKey[{_index1}];"); + lines.Add(indent + $"uint part1 = (uint)value << {_rotateBitNum};"); + lines.Add(indent + $"uint part2 = (uint)value >> (32 - {_rotateBitNum});"); + lines.Add(indent + $"value = (int)(part1 | part2);"); + } + + public override void GenerateDecryptCode(List lines, string indent) + { + lines.Add(indent + $"uint value2 = (uint)value >> {_rotateBitNum};"); + lines.Add(indent + $"uint part1 = (uint)value << (32 - {_rotateBitNum});"); + lines.Add(indent + $"value = (int)(value2 | part1);"); + lines.Add(indent + $"value -= {_addValue} + _secretKey[{_index1}];"); + lines.Add(indent + $"value ^= {_xorValue} ^ salt;"); + } + } +} diff --git a/Editor/EncryptionVM/VirtualMachineCreator.cs b/Editor/EncryptionVM/VirtualMachineCreator.cs index 93c76f5..e6b76e7 100644 --- a/Editor/EncryptionVM/VirtualMachineCreator.cs +++ b/Editor/EncryptionVM/VirtualMachineCreator.cs @@ -1,6 +1,10 @@ -using Obfuz.EncryptionVM.Instructions; +using NUnit.Framework; +using Obfuz.EncryptionVM.Instructions; using Obfuz.Utils; +using System; +using System.Collections.Generic; using UnityEngine.Assertions; +using UnityEngine.UIElements; namespace Obfuz.EncryptionVM { @@ -21,19 +25,21 @@ namespace Obfuz.EncryptionVM _random = new RandomWithKey(intGenerationSecretKey, 0); } + private readonly List> _instructionCreators = new List> + { + (r, len) => new AddInstruction(r.NextInt(), r.NextInt(len)), + (r, len) => new XorInstruction(r.NextInt(), r.NextInt(len)), + (r, len) => new BitRotateInstruction(r.NextInt(32), r.NextInt(len)), + (r, len) => new MultipleInstruction(r.NextInt() | 0x1, r.NextInt(len)), + (r, len) => new AddRotateXorInstruction(r.NextInt(), r.NextInt(len), r.NextInt(32), r.NextInt()), + (r, len) => new AddXorRotateInstruction(r.NextInt(), r.NextInt(len), r.NextInt(), r.NextInt(32)), + (r, len) => new XorAddRotateInstruction(r.NextInt(), r.NextInt(), r.NextInt(len), r.NextInt(32)), + + }; + private IEncryptionInstruction CreateRandomInstruction(int intSecretKeyLength) { - switch (_random.NextInt(3)) - { - case 0: - return new AddInstruction(_random.NextInt(), _random.NextInt(intSecretKeyLength)); - case 1: - return new XorInstruction(_random.NextInt(), _random.NextInt(intSecretKeyLength)); - case 2: - return new BitRotateInstruction(_random.NextInt(32), _random.NextInt(intSecretKeyLength)); - default: - throw new System.Exception("Invalid instruction type"); - } + return _instructionCreators[_random.NextInt(_instructionCreators.Count)](_random, intSecretKeyLength); } private EncryptionInstructionWithOpCode CreateEncryptOpCode(ushort code) diff --git a/package.json b/package.json index 23ed31b..27f3d9f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.code-philosophy.obfuz", - "version": "1.0.0-beta", + "version": "1.0.0-alpha", "displayName": "Obfuz", "description": "Obfuz is a powerful code obfuscation tool designed specifically for Unity projects.", "category": "Editor",