优化float和double加密

backup
walon 2025-05-14 14:59:49 +08:00
parent 334ff8095f
commit 6b487f58c0
1 changed files with 26 additions and 4 deletions

View File

@ -40,6 +40,28 @@ namespace Obfuz
return ((long)decryptedHigh << 32) | (uint)decryptedLow;
}
private int? _floatSalt;
private int GetFloatSalt()
{
if (_floatSalt == null)
{
_floatSalt = Encrypt(0xABCD, 0x12345678, 0);
}
return _floatSalt.Value;
}
private long? _doubleSalt;
private long GetDoubleSalt()
{
if (_doubleSalt == null)
{
_doubleSalt = Encrypt(0xAABBCCDDL, 0x12345678, 0);
}
return _doubleSalt.Value;
}
public virtual float Encrypt(float value, int opts, int salt)
{
if (float.IsNaN(value) || float.IsInfinity(value))
@ -47,7 +69,7 @@ namespace Obfuz
return value;
}
ref int intValue = ref UnsafeUtility.As<float, int>(ref value);
int xorValue = ((1 << 23) - 1) & (opts ^ salt);
int xorValue = ((1 << 23) - 1) & (opts ^ salt ^ GetFloatSalt());
intValue ^= xorValue;
return value;
}
@ -59,7 +81,7 @@ namespace Obfuz
return value;
}
ref int intValue = ref UnsafeUtility.As<float, int>(ref value);
int xorValue = ((1 << 23) - 1) & (opts ^ salt);
int xorValue = ((1 << 23) - 1) & (opts ^ salt ^ GetFloatSalt());
intValue ^= xorValue;
return value;
}
@ -71,7 +93,7 @@ namespace Obfuz
return value;
}
ref long longValue = ref UnsafeUtility.As<double, long>(ref value);
long xorValue = ((1L << 52) - 1) & (((long)opts << 32) | (uint)salt);
long xorValue = ((1L << 52) - 1) & ((((long)opts << 32) | (uint)salt) ^ GetDoubleSalt());
longValue ^= xorValue;
return value;
}
@ -83,7 +105,7 @@ namespace Obfuz
return value;
}
ref long longValue = ref UnsafeUtility.As<double, long>(ref value);
long xorValue = ((1L << 52) - 1) & (((long)opts << 32) | (uint)salt);
long xorValue = ((1L << 52) - 1) & ((((long)opts << 32) | (uint)salt) ^ GetDoubleSalt());
longValue ^= xorValue;
return value;
}