新增 MultipleRotateXorInstruction MultipleXorRotateInstruction XorMultipleRotateInstruction指令
parent
a4e4d199be
commit
cabc6c2980
|
@ -0,0 +1,68 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Obfuz.EncryptionVM.Instructions
|
||||
{
|
||||
public class MultipleRotateXorInstruction : EncryptionInstructionBase
|
||||
{
|
||||
// x = x * p1 + secretKey[index1];
|
||||
// x = Rotate(x, p2)
|
||||
// x = x ^ p3 ^ salt;
|
||||
|
||||
private readonly int _multipleValue;
|
||||
private readonly int _revertMultipleValue;
|
||||
private readonly int _index1;
|
||||
private readonly int _rotateBitNum;
|
||||
private readonly int _xorValue;
|
||||
|
||||
public MultipleRotateXorInstruction(int multipleValue, int index1, int rotateBitNum, int xorValue)
|
||||
{
|
||||
_multipleValue = multipleValue;
|
||||
_revertMultipleValue = (int)MultipleInstruction.ModInverseOdd((uint)multipleValue);
|
||||
_index1 = index1;
|
||||
_rotateBitNum = rotateBitNum;
|
||||
_xorValue = xorValue;
|
||||
}
|
||||
|
||||
public override int Encrypt(int value, int[] secretKey, int salt)
|
||||
{
|
||||
value = value * _multipleValue + 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 = (value - secretKey[_index1]) * _revertMultipleValue;
|
||||
return value;
|
||||
}
|
||||
|
||||
public override void GenerateEncryptCode(List<string> lines, string indent)
|
||||
{
|
||||
lines.Add(indent + $"value = value * {_multipleValue} + _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 = (value - _secretKey[{_index1}]) * {_revertMultipleValue};");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Obfuz.EncryptionVM.Instructions
|
||||
{
|
||||
public class MultipleXorRotateInstruction : EncryptionInstructionBase
|
||||
{
|
||||
// x = x * p1 + secretKey[index1];
|
||||
// x = x ^ p3 ^ salt;
|
||||
// x = Rotate(x, p2)
|
||||
|
||||
private readonly int _multipleValue;
|
||||
private readonly int _revertMultipleValue;
|
||||
private readonly int _index1;
|
||||
private readonly int _rotateBitNum;
|
||||
private readonly int _xorValue;
|
||||
|
||||
public MultipleXorRotateInstruction(int multipleValue, int index1, int xorValue, int rotateBitNum)
|
||||
{
|
||||
_multipleValue = multipleValue;
|
||||
_revertMultipleValue = (int)MultipleInstruction.ModInverseOdd((uint)multipleValue);
|
||||
_index1 = index1;
|
||||
_rotateBitNum = rotateBitNum;
|
||||
_xorValue = xorValue;
|
||||
}
|
||||
|
||||
public override int Encrypt(int value, int[] secretKey, int salt)
|
||||
{
|
||||
value = value * _multipleValue + 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 = (value - secretKey[_index1]) * _revertMultipleValue;
|
||||
return value;
|
||||
}
|
||||
|
||||
public override void GenerateEncryptCode(List<string> lines, string indent)
|
||||
{
|
||||
lines.Add(indent + $"value = value * {_multipleValue} + _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 value2 = (uint)value >> {_rotateBitNum};");
|
||||
lines.Add(indent + $"uint part1 = (uint)value << (32 - {_rotateBitNum});");
|
||||
lines.Add(indent + $"value = (int)(value2 | part1);");
|
||||
lines.Add(indent + $"value ^= {_xorValue} ^ salt;");
|
||||
lines.Add(indent + $"value = (value - _secretKey[{_index1}]) * {_revertMultipleValue};");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Obfuz.EncryptionVM.Instructions
|
||||
{
|
||||
public class XorMultipleRotateInstruction : EncryptionInstructionBase
|
||||
{
|
||||
// x = x ^ p3 ^ salt;
|
||||
// x = x * p1 + secretKey[index1];
|
||||
// x = Rotate(x, p2)
|
||||
|
||||
private readonly int _multipleValue;
|
||||
private readonly int _revertMultipleValue;
|
||||
private readonly int _index1;
|
||||
private readonly int _rotateBitNum;
|
||||
private readonly int _xorValue;
|
||||
|
||||
public XorMultipleRotateInstruction(int xorValue, int multipleValue, int index1, int rotateBitNum)
|
||||
{
|
||||
_multipleValue = multipleValue;
|
||||
_revertMultipleValue = (int)MultipleInstruction.ModInverseOdd((uint)multipleValue);
|
||||
_index1 = index1;
|
||||
_rotateBitNum = rotateBitNum;
|
||||
_xorValue = xorValue;
|
||||
}
|
||||
|
||||
public override int Encrypt(int value, int[] secretKey, int salt)
|
||||
{
|
||||
value ^= _xorValue ^ salt;
|
||||
value = value * _multipleValue + 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 = (value - secretKey[_index1]) * _revertMultipleValue;
|
||||
value ^= _xorValue ^ salt;
|
||||
return value;
|
||||
}
|
||||
|
||||
public override void GenerateEncryptCode(List<string> lines, string indent)
|
||||
{
|
||||
lines.Add(indent + $"value ^= {_xorValue} ^ salt;");
|
||||
lines.Add(indent + $"value = value * {_multipleValue} + _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 = (value - _secretKey[{_index1}]) * {_revertMultipleValue};");
|
||||
lines.Add(indent + $"value ^= {_xorValue} ^ salt;");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -78,10 +78,10 @@ namespace Obfuz.EncryptionVM
|
|||
foreach (var opCode in _vm.opCodes)
|
||||
{
|
||||
lines.Add(@$" case {opCode.code}:
|
||||
{{");
|
||||
{{
|
||||
// {opCode.function.GetType().Name}");
|
||||
AppendEncryptCode(lines, opCode.function);
|
||||
lines.Add(@"
|
||||
return value;
|
||||
lines.Add(@" return value;
|
||||
}");
|
||||
}
|
||||
|
||||
|
@ -102,10 +102,10 @@ namespace Obfuz.EncryptionVM
|
|||
foreach (var opCode in _vm.opCodes)
|
||||
{
|
||||
lines.Add(@$" case {opCode.code}:
|
||||
{{");
|
||||
{{
|
||||
// {opCode.function.GetType().Name}");
|
||||
AppendDecryptCode(lines, opCode.function);
|
||||
lines.Add(@"
|
||||
return value;
|
||||
lines.Add(@" return value;
|
||||
}");
|
||||
}
|
||||
|
||||
|
|
|
@ -34,7 +34,9 @@ namespace Obfuz.EncryptionVM
|
|||
(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)),
|
||||
|
||||
(r, len) => new MultipleRotateXorInstruction(r.NextInt() | 0x1, r.NextInt(len), r.NextInt(32), r.NextInt()),
|
||||
(r, len) => new MultipleXorRotateInstruction(r.NextInt() | 0x1, r.NextInt(len), r.NextInt(), r.NextInt(32)),
|
||||
(r, len) => new XorMultipleRotateInstruction(r.NextInt(), r.NextInt() | 0x1, r.NextInt(len), r.NextInt(32)),
|
||||
};
|
||||
|
||||
private IEncryptionInstruction CreateRandomInstruction(int intSecretKeyLength)
|
||||
|
|
Loading…
Reference in New Issue