using System;
using System.Diagnostics;
namespace Pulsar.Common.Cryptography
{
///
/// Provides byte rotation obfuscation methods for simple data protection.
///
public static class ByteRotationObfuscator
{
///
/// The rotation amount used for obfuscation.
///
private const int ROTATION_AMOUNT = 16;
///
/// Obfuscates data by rotating each byte by a fixed amount with overflow wrapping.
///
/// The data to obfuscate.
/// The obfuscated data.
public static byte[] Obfuscate(byte[] data)
{
if (data == null)
{
Debug.WriteLine("Failed to Obfuscate. Data is null.");
return data;
}
byte[] result = new byte[data.Length];
for (int i = 0; i < data.Length; i++)
{
result[i] = RotateByte(data[i], ROTATION_AMOUNT);
}
return result;
}
///
/// Deobfuscates data by rotating each byte back by the fixed amount with overflow wrapping.
///
/// The obfuscated data to deobfuscate.
/// The original data.
public static byte[] Deobfuscate(byte[] data)
{
if (data == null)
{
Debug.WriteLine("Failed to Deobfuscate. Data is null.");
return data;
}
byte[] result = new byte[data.Length];
for (int i = 0; i < data.Length; i++)
{
result[i] = RotateByte(data[i], -ROTATION_AMOUNT);
}
return result;
}
///
/// Rotates a byte by the specified amount with overflow wrapping.
///
/// The byte to rotate.
/// The rotation amount (can be positive or negative).
/// The rotated byte.
private static byte RotateByte(byte value, int amount)
{
amount = ((amount % 256) + 256) % 256;
int result = (value + amount) % 256;
return (byte)result;
}
///
/// Calculates the rotated value for a given byte and rotation amount.
/// This is a helper method for testing and verification.
///
/// The byte value to rotate.
/// The rotation amount.
/// The rotated byte value.
public static byte CalculateRotation(byte value, int amount)
{
return RotateByte(value, amount);
}
}
}