Skip to content

Instantly share code, notes, and snippets.

@codemonkey85
Created April 22, 2015 17:38
Show Gist options
  • Save codemonkey85/4633acd399112c9ad58c to your computer and use it in GitHub Desktop.
Save codemonkey85/4633acd399112c9ad58c to your computer and use it in GitHub Desktop.
Flawed shuffle code - what is wrong with this?
internal static byte[,] shuffle_order = new byte[,]
{{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,2,3,1},{0,3,1,2},{0,3,2,1},{1,0,2,3},{1,0,3,2},
{1,2,0,3},{1,2,3,0},{1,3,0,2},{1,3,2,0},{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},{2,3,0,1},
{2,3,1,0},{3,0,1,2},{3,0,2,1},{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0}};
internal static byte[,] inv_shuffle_order = new byte[,]
{{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,3,1,2},{0,2,3,1},{0,3,2,1},{1,0,2,3},{1,0,3,2},{2,0,1,3},
{3,0,1,2},{2,0,3,1},{3,0,2,1},{1,2,0,3},{1,3,0,2},{2,1,0,3},{3,1,0,2},{2,3,0,1},{3,2,0,1},
{1,2,3,0},{1,3,2,0},{2,1,3,0},{3,1,2,0},{2,3,1,0},{3,2,1,0}};
internal static byte[] shuffle(byte[] pkx, bool un)
{
byte[] temp = new byte[232];
Array.Clear(temp, 0, 232);
uint pv = BitConverter.ToUInt32(pkx, 0);
uint sv = ((pv & 0x3E000) >> 0xD) % 24;
const int blocksize = 56;
if (un)
{
Array.Copy(pkx, 8 + (0 * blocksize), temp, 8 + (shuffle_order[sv, 0] * blocksize), blocksize);
Array.Copy(pkx, 8 + (1 * blocksize), temp, 8 + (shuffle_order[sv, 1] * blocksize), blocksize);
Array.Copy(pkx, 8 + (2 * blocksize), temp, 8 + (shuffle_order[sv, 2] * blocksize), blocksize);
Array.Copy(pkx, 8 + (3 * blocksize), temp, 8 + (shuffle_order[sv, 3] * blocksize), blocksize);
}
else
{
Array.Copy(pkx, 8 + (0 * blocksize), temp, 8 + (inv_shuffle_order[sv, 0] * blocksize), blocksize);
Array.Copy(pkx, 8 + (1 * blocksize), temp, 8 + (inv_shuffle_order[sv, 1] * blocksize), blocksize);
Array.Copy(pkx, 8 + (2 * blocksize), temp, 8 + (inv_shuffle_order[sv, 2] * blocksize), blocksize);
Array.Copy(pkx, 8 + (3 * blocksize), temp, 8 + (inv_shuffle_order[sv, 3] * blocksize), blocksize);
}
Array.Copy(temp, 8, pkx, 8, 224);
return pkx;
}
@SciresM
Copy link

SciresM commented Apr 22, 2015

I think you've got the offsets in the Copy call swapped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment