PDA

View Full Version : Reversing this code


mindoverflow
08-29-2009, 11:13 AM
Hello,

Could someone help me reverse this code(keygen) please, i can nomore think about it, it's not working with me.

var
a,b,c: integer;
d,x,y: word;
e:byte;
key, ref: string;
k: array[1..36] of byte;
begin
ref:='ABCDEFGHIJKLMNOPQRSTUVW123456789';
key:='BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBB'; // 56

for x:=1 to 36 do k[x]:=0; // zero the result

for x:=1 to 56 do
begin
a:=(x-1)*5;
if a>=0 then b:=0 else b:=-1; // cdq, if i'm not wrong about it
b:=b and 7;
a:=a+b;
asm sar a, 3 end;
e:=a;
asm shl a, 3 end;
c:=((x-1)*5)-a;
for y:=1 to 32 do
begin
if key[x]:=ref[y] then
begin
d:=(y-1) shl (c and $ff);
k[e+1]:=k[e+1] or (d and $ff);
k[e+2]:=k[e+2] or ((d shr 8) and $ff);

break;
end;
end;
end;
end;


Thanks

Git
08-29-2009, 02:31 PM
How can we reverse it, it is already reversed into full source code. It doesn't get any better than that.

Git

mindoverflow
08-29-2009, 03:33 PM
I mean, I'm unable to go the otherway and generate the 56 chars key, actually it calculates the comparator code 0x22 or 0x23 long if i remember well.

Git
08-29-2009, 05:12 PM
So the algorithm generates k[] given key[] and ref[], butyou want to do the inverse, ie generate key[] given ref[] and k[], is that right?

Git

mindoverflow
08-29-2009, 05:41 PM
that's it, i tried to zero as much as possible variables, and go backward, but i'm yet doing something wrong.

Git
08-30-2009, 07:44 AM
Do you actually need to know the inverse algorithm in a form you could, say, write on paper, or do you have some example k[] and/orref[] values that you want to find the corresponding key[] values for?. The two cases are very different. Given an infinite amount of time, the latter case is always possible, even if the algorithm is a black box.

Git

mindoverflow
08-30-2009, 08:14 AM
Both cases would be suitable, cuz my case is that i have a program that generates a hardware ID, calculate k[] from key according to ref[], XOR k[] with the ID and checks if it's 0.

What i came up with and that doesn't work is:

const c:array[1..8] of byte=(0,5,2,7,4,1,6,3); // (x*5)/8 reminders
begin
key:='';
i:=8;
for x:=55 downto 1 do
begin
a:=(x-1)*5;
if a>=0 then b:=0 else b:=-1;
b:=b and 7;
a:=a+b;
e:=a div 8;
e:=k[e+2] - k[e+1]; // someway to reverse the or, but not usefull
y:=e shr c[i];
inc(i); if i>8 then i:=1;
key:=ref[y+1]+key;
end;
end;