obfuz/Obfuz/Packages/com.code-philosophy.obfuz/Editor/Utils/EncryptionUtil.cs

50 lines
1.4 KiB
C#
Raw Normal View History

2025-05-17 14:53:51 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
2025-05-17 14:53:51 +08:00
namespace Obfuz.Utils
{
public static class EncryptionUtil
{
public static int GetBitCount(int value)
{
int count = 0;
while (value > 0)
{
count++;
value >>= 1;
}
return count;
}
public static int GenerateEncryptionOpCodes(IRandom random, IEncryptor encryptor, int encryptionLevel)
{
if (encryptionLevel <= 0 || encryptionLevel > 4)
{
throw new ArgumentException($"Invalid encryption level: {encryptionLevel}, should be in range [1,4]");
}
int vmOpCodeCount = encryptor.OpCodeCount;
long ops = 0;
for (int i = 0; i < encryptionLevel; i++)
{
long newOps = ops * vmOpCodeCount;
2025-05-17 14:53:51 +08:00
// don't use 0
int op = random.NextInt(1, vmOpCodeCount);
newOps |= (uint)op;
if (newOps > uint.MaxValue)
2025-05-17 14:53:51 +08:00
{
Debug.LogWarning($"OpCode overflow. encryptionLevel:{encryptionLevel}, vmOpCodeCount:{vmOpCodeCount}");
}
else
{
ops = newOps;
2025-05-17 14:53:51 +08:00
}
}
return (int)ops;
}
}
}