修复 VirtualMachineCodeGenerator生成的Encrypt和Decrypt代码的bug

backup
walon 2025-05-14 12:21:25 +08:00
parent b216ed1eb3
commit 2fec77eeb5
3 changed files with 25 additions and 20 deletions

View File

@ -152,19 +152,20 @@ namespace Obfuz.EncryptionVM
public override int Encrypt(int value, int opts, int salt)
{
int revertOps = 0;
while (opts > 0)
uint uopts = (uint)opts;
uint revertOps = 0;
while (uopts != 0)
{
int opCode = opts & kOpCodeMask;
uint opCode = uopts & kOpCodeMask;
revertOps <<= kOpCodeBits;
revertOps |= opCode;
opts >>= kOpCodeBits;
uopts >>= kOpCodeBits;
}
while (revertOps > 0)
while (revertOps != 0)
{
int opCode = revertOps & kOpCodeMask;
value = ExecuteEncrypt(value, opCode, salt);
uint opCode = revertOps & kOpCodeMask;
value = ExecuteEncrypt(value, (int)opCode, salt);
revertOps >>= kOpCodeBits;
}
return value;
@ -172,11 +173,12 @@ namespace Obfuz.EncryptionVM
public override int Decrypt(int value, int opts, int salt)
{
while (opts > 0)
uint uopts = (uint)opts;
while (uopts != 0)
{
int opCode = opts & kOpCodeMask;
value = ExecuteDecrypt(value, opCode, salt);
opts >>= kOpCodeBits;
uint opCode = uopts & kOpCodeMask;
value = ExecuteDecrypt(value, (int)opCode, salt);
uopts >>= kOpCodeBits;
}
return value;
}

View File

@ -62,7 +62,7 @@ namespace Obfuz.EncryptionVM
private List<uint> DecodeOps(uint ops)
{
var codes = new List<uint>();
while (ops > 0)
while (ops != 0)
{
uint code = ops % (uint)_opCodes.Length;
codes.Add(code);

View File

@ -133,14 +133,17 @@ namespace Obfuz
for (int i = 0; i < vm.opCodes.Length; i++)
{
int ops = i * vm.opCodes.Length + i;
int encryptedValueOfVms = vms.Encrypt(testValue, ops, i);
int decryptedValueOfVms = vms.Decrypt(encryptedValueOfVms, ops, i);
//int salt = i;
//int ops = -1135538782;
int salt = -879409147;
int encryptedValueOfVms = vms.Encrypt(testValue, ops, salt);
int decryptedValueOfVms = vms.Decrypt(encryptedValueOfVms, ops, salt);
if (decryptedValueOfVms != testValue)
{
throw new Exception($"VirtualMachineSimulator decrypt failed! opCode:{i}, originalValue:{testValue} decryptedValue:{decryptedValueOfVms}");
}
int encryptedValueOfGvm = gvmInstance.Encrypt(testValue, ops, i);
int decryptedValueOfGvm = gvmInstance.Decrypt(encryptedValueOfGvm, ops, i);
int encryptedValueOfGvm = gvmInstance.Encrypt(testValue, ops, salt);
int decryptedValueOfGvm = gvmInstance.Decrypt(encryptedValueOfGvm, ops, salt);
if (encryptedValueOfGvm != encryptedValueOfVms)
{
throw new Exception($"encryptedValue not match! opCode:{i}, originalValue:{testValue} encryptedValue VirtualMachineSimulator:{encryptedValueOfVms} GeneratedEncryptionVirtualMachine:{encryptedValueOfGvm}");
@ -150,14 +153,14 @@ namespace Obfuz
throw new Exception($"GeneratedEncryptionVirtualMachine decrypt failed! opCode:{i}, originalValue:{testValue} decryptedValue:{decryptedValueOfGvm}");
}
byte[] encryptedStrOfVms = vms.Encrypt(testString, ops, i);
string descryptedStrOfVms = vms.DecryptString(encryptedStrOfVms, 0, encryptedStrOfVms.Length, ops, i);
byte[] encryptedStrOfVms = vms.Encrypt(testString, ops, salt);
string descryptedStrOfVms = vms.DecryptString(encryptedStrOfVms, 0, encryptedStrOfVms.Length, ops, salt);
if (descryptedStrOfVms != testString)
{
throw new Exception($"VirtualMachineSimulator decrypt string failed! opCode:{i}, originalValue:{testString} decryptedValue:{descryptedStrOfVms}");
}
byte[] encryptedStrOfGvm = gvmInstance.Encrypt(testString, ops, i);
string descryptedStrOfGvm = gvmInstance.DecryptString(encryptedStrOfGvm, 0, encryptedStrOfGvm.Length, ops, i);
byte[] encryptedStrOfGvm = gvmInstance.Encrypt(testString, ops, salt);
string descryptedStrOfGvm = gvmInstance.DecryptString(encryptedStrOfGvm, 0, encryptedStrOfGvm.Length, ops, salt);
if (!encryptedStrOfGvm.SequenceEqual(encryptedStrOfVms))
{
throw new Exception($"encryptedValue not match! opCode:{i}, originalValue:{testString} encryptedValue VirtualMachineSimulator:{encryptedStrOfVms} GeneratedEncryptionVirtualMachine:{encryptedStrOfGvm}");