obfuz/Runtime/DefaultEncryptor.cs

102 lines
2.6 KiB
C#
Raw Normal View History

2025-04-23 18:58:44 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz
{
public class DefaultEncryptor : IEncryptor
{
private readonly byte[] _key;
public DefaultEncryptor(byte[] key)
{
_key = key;
}
2025-05-07 10:14:15 +08:00
public void EncryptBlock(byte[] data, long ops, int salt)
2025-04-23 18:58:44 +08:00
{
for (int i = 0; i < data.Length; i++)
{
2025-05-07 10:14:15 +08:00
data[i] ^= (byte)(_key[i % _key.Length] ^ salt);
2025-04-23 18:58:44 +08:00
}
}
2025-05-07 10:14:15 +08:00
public void DecryptBlock(byte[] data, long ops, int salt)
2025-04-23 18:58:44 +08:00
{
for (int i = 0; i < data.Length; i++)
{
2025-05-07 10:14:15 +08:00
data[i] ^= (byte)(_key[i % _key.Length] ^ salt);
2025-04-23 18:58:44 +08:00
}
}
2025-05-07 22:55:07 +08:00
public int Encrypt(int value, int opts, int salt)
{
return value;
}
public int Decrypt(int value, int opts, int salt)
{
return value;
}
public long Encrypt(long value, int opts, int salt)
{
return value;
}
public long Decrypt(long value, int opts, int salt)
{
return value;
}
public float Encrypt(float value, int opts, int salt)
{
return value;
}
public float Decrypt(float value, int opts, int salt)
{
return value;
}
public double Encrypt(double value, int opts, int salt)
{
return value;
}
public double Decrypt(double value, int opts, int salt)
{
return value;
}
public int[] Encrypt(byte[] value, int offset, int length, int opts, int salt)
{
var intArr = new int[(length + 3) / 4];
Buffer.BlockCopy(value, offset, intArr, 0, length);
return intArr;
}
public byte[] Decrypt(int[] value, int offset, int byteLength, int ops, int salt)
{
byte[] byteArr = new byte[byteLength];
Buffer.BlockCopy(value, 0, byteArr, 0, byteLength);
return byteArr;
}
public int[] Encrypt(string value, int ops, int salt)
{
byte[] bytes = Encoding.UTF8.GetBytes(value);
return Encrypt(bytes, 0, bytes.Length, ops, salt);
}
public string DecryptString(int[] value, int offset, int stringBytesLength, int ops, int salt)
{
byte[] bytes = new byte[stringBytesLength];
Buffer.BlockCopy(value, 0, bytes, 0, stringBytesLength);
return Encoding.UTF8.GetString(bytes);
}
2025-04-23 18:58:44 +08:00
}
}