Heh yeah sorry that wasn't meant as a critique, I was just happy to find another reference source and something to compare it to.Spirited wrote: Fri May 14, 2021 11:06 pm Oh man, maybe don't listen to anything I said a decade ago. That was exactly when I started learning how to program... lol. Just looking at the code sample you provided, did you need to ref pwd_data when passing it into Decrypt for the scan codes? That's one of the reasons why I avoided using ref in a lot of those ciphers and opted for passing in a source and destination (even if the destination completely overlaps).
The decrypt function is Cpt Sky's decrypt function which I figured should still work, it has the ref keyword.
Cpt Sky's Decrypt
Code: Select all
public unsafe void Decrypt(ref Byte[] aBuf, Int32 aLength)
{
if (aLength % 8 != 0)
throw new ArgumentException("Length of the buffer must be a multiple of 64 bits.", "aLength");
fixed (Byte* buf = aBuf)
{
UInt32* data = (UInt32*)buf;
for (Int32 k = 0, len = aLength / 8; k < len; ++k)
{
UInt32 lv = data[2 * k];
UInt32 rv = data[2 * k + 1]; // <--- +1 here.
for (Int32 i = RC5_12; i >= 1; --i)
{
rv = rotr((rv - mSub[2 * i + 1]), lv) ^ lv;
lv = rotr((lv - mSub[2 * i]), rv) ^ rv;
}
data[2 * k] = lv - mSub[0];
data[2 * k + 1] = rv - mSub[1];
}
}
}
Code: Select all
public void Decrypt(Span<byte> src, Span<byte> dst)
{
// Pad the buffer
var length = src.Length / 8;
if (src.Length % 8 > 0) length = length + 1;
src.CopyTo(dst);
// Decrypt the buffer
for (int word = 0; word < length; word++)
{
uint a = BitConverter.ToUInt32(dst.Slice(8 * word));
uint b = BitConverter.ToUInt32(dst.Slice((8 * word) + 4)); // <--- +4 here
for (int round = RC5.Rounds; round > 0; round--)
{
b = (b - this.Sub[2 * round + 1]).RotateRight((int)a) ^ a;
a = (a - this.Sub[2 * round]).RotateRight((int)b) ^ b;
}
BitConverter.GetBytes(a - this.Sub[0]).CopyTo(dst.Slice(8 * word));
BitConverter.GetBytes(b - this.Sub[1]).CopyTo(dst.Slice(8 * word + 4));
}
}