添加 AddRotateXorInstruction AddXorRotateInstruction XorAddRotateInstruction MultipleInstruction这几种基础加密指令
parent
09e4f3269c
commit
a4e4d199be
|
@ -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<string> 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<string> 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}];");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<string> 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<string> 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}];");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<string> lines, string indent)
|
||||||
|
{
|
||||||
|
lines.Add(indent + $"value = value * {_multiValue} + _secretKey[{_opKeyIndex}] + salt;");
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void GenerateDecryptCode(List<string> lines, string indent)
|
||||||
|
{
|
||||||
|
lines.Add(indent + $"value = (value - _secretKey[{_opKeyIndex}] - salt) * {_revertMultiValue};");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<string> 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<string> 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;");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,10 @@
|
||||||
using Obfuz.EncryptionVM.Instructions;
|
using NUnit.Framework;
|
||||||
|
using Obfuz.EncryptionVM.Instructions;
|
||||||
using Obfuz.Utils;
|
using Obfuz.Utils;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using UnityEngine.Assertions;
|
using UnityEngine.Assertions;
|
||||||
|
using UnityEngine.UIElements;
|
||||||
|
|
||||||
namespace Obfuz.EncryptionVM
|
namespace Obfuz.EncryptionVM
|
||||||
{
|
{
|
||||||
|
@ -21,19 +25,21 @@ namespace Obfuz.EncryptionVM
|
||||||
_random = new RandomWithKey(intGenerationSecretKey, 0);
|
_random = new RandomWithKey(intGenerationSecretKey, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private readonly List<Func<IRandom, int, EncryptionInstructionBase>> _instructionCreators = new List<Func<IRandom, int, EncryptionInstructionBase>>
|
||||||
|
{
|
||||||
|
(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)
|
private IEncryptionInstruction CreateRandomInstruction(int intSecretKeyLength)
|
||||||
{
|
{
|
||||||
switch (_random.NextInt(3))
|
return _instructionCreators[_random.NextInt(_instructionCreators.Count)](_random, intSecretKeyLength);
|
||||||
{
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private EncryptionInstructionWithOpCode CreateEncryptOpCode(ushort code)
|
private EncryptionInstructionWithOpCode CreateEncryptOpCode(ushort code)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "com.code-philosophy.obfuz",
|
"name": "com.code-philosophy.obfuz",
|
||||||
"version": "1.0.0-beta",
|
"version": "1.0.0-alpha",
|
||||||
"displayName": "Obfuz",
|
"displayName": "Obfuz",
|
||||||
"description": "Obfuz is a powerful code obfuscation tool designed specifically for Unity projects.",
|
"description": "Obfuz is a powerful code obfuscation tool designed specifically for Unity projects.",
|
||||||
"category": "Editor",
|
"category": "Editor",
|
||||||
|
|
Loading…
Reference in New Issue