修复nan浮点加密的bug。cpu会自动调整nan数的尾数。怪事

backup
walon 2025-05-14 14:16:29 +08:00
parent 2fec77eeb5
commit 78b08a9069
1 changed files with 24 additions and 4 deletions

View File

@ -42,29 +42,49 @@ namespace Obfuz
public virtual float Encrypt(float value, int opts, int salt) public virtual float Encrypt(float value, int opts, int salt)
{ {
if (float.IsNaN(value) || float.IsInfinity(value))
{
return value;
}
ref int intValue = ref UnsafeUtility.As<float, int>(ref value); ref int intValue = ref UnsafeUtility.As<float, int>(ref value);
intValue = Encrypt(intValue, opts, salt); int xorValue = ((1 << 23) - 1) & (opts ^ salt);
intValue ^= xorValue;
return value; return value;
} }
public virtual float Decrypt(float value, int opts, int salt) public virtual float Decrypt(float value, int opts, int salt)
{ {
if (float.IsNaN(value) || float.IsInfinity(value))
{
return value;
}
ref int intValue = ref UnsafeUtility.As<float, int>(ref value); ref int intValue = ref UnsafeUtility.As<float, int>(ref value);
intValue = Decrypt(intValue, opts, salt); int xorValue = ((1 << 23) - 1) & (opts ^ salt);
intValue ^= xorValue;
return value; return value;
} }
public virtual double Encrypt(double value, int opts, int salt) public virtual double Encrypt(double value, int opts, int salt)
{ {
if (double.IsNaN(value) || double.IsInfinity(value))
{
return value;
}
ref long longValue = ref UnsafeUtility.As<double, long>(ref value); ref long longValue = ref UnsafeUtility.As<double, long>(ref value);
longValue = Encrypt(longValue, opts, salt); long xorValue = ((1L << 52) - 1) & (((long)opts << 32) | (uint)salt);
longValue ^= xorValue;
return value; return value;
} }
public virtual double Decrypt(double value, int opts, int salt) public virtual double Decrypt(double value, int opts, int salt)
{ {
if (double.IsNaN(value) || double.IsInfinity(value))
{
return value;
}
ref long longValue = ref UnsafeUtility.As<double, long>(ref value); ref long longValue = ref UnsafeUtility.As<double, long>(ref value);
longValue = Decrypt(longValue, opts, salt); long xorValue = ((1L << 52) - 1) & (((long)opts << 32) | (uint)salt);
longValue ^= xorValue;
return value; return value;
} }