Reverse Engineering for Beginners

(avery) #1

CHAPTER 79. “QR9”: RUBIK’S CUBE INSPIRED AMATEUR CRYPTO-ALGORITHM CHAPTER 79. “QR9”: RUBIK’S CUBE INSPIRED AMATEUR CRYPTO-ALGORITHM


.text:00541131 add ebx, 8
.text:00541134 cmp edi, 0FFFFFFFFh
.text:00541137 jg short loc_541119
.text:00541139 pop edi
.text:0054113A pop esi
.text:0054113B pop ebp
.text:0054113C pop ebx
.text:0054113D add esp, 40h
.text:00541140 retn
.text:00541140 rotate2 endp


It isalmostthe same, except the order of the arguments of theget_bit()andset_bit()is different. Let’s rewrite it in
C-like code:


void rotate2 (int v)
{
bool tmp[8][8]; // internal array
int i, j;


for (i=0; i<8; i++)
for (j=0; j<8; j++)
tmp[i][j]=get_bit (v, i, j);

for (i=0; i<8; i++)
for (j=0; j<8; j++)
set_bit (v, j, 7-i, tmp[i][j]);
};


Let’s also rewrite therotate3()function:


void rotate3 (int v)
{
bool tmp[8][8];
int i, j;


for (i=0; i<8; i++)
for (j=0; j<8; j++)
tmp[i][j]=get_bit (i, v, j);

for (i=0; i<8; i++)
for (j=0; j<8; j++)
set_bit (7-j, v, i, tmp[i][j]);
};


Well, now things are simpler. If we consider cube64 as a 3D cube of size 888, where each element is a bit,get_bit()
andset_bit()take just the coordinates of a bit as input.


The rotate1/2/3 functions are in fact rotating all bits in a specific plane. These three functions are one for each cube side
and thevargument sets the plane in the range of 0..7.


Maybe, the algorithm’s author was thinking of a 888 Rubik’s cube^3 ?!


Yes, indeed.


Let’s look closer into thedecrypt()function, here is its rewritten version:


void decrypt (BYTE buf, int sz, char pw)
{
char *p=strdup (pw);
strrev (p);
int i=0;


do
{
memcpy (cube, buf+i, 8*8);
rotate_all (p, 3);
memcpy (buf+i, cube, 8*8);
i+=64;
}
while (i<sz);

(^3) wikipedia

Free download pdf