News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

How to convert this C code to MASM?

Started by woonsan, July 01, 2007, 09:11:05 AM

Previous topic - Next topic

woonsan

I want to convert this code to MASM.


static char Key[]="NexonInc.";
void Crypt(char *pkt){
   int len=((unsigned char)*(pkt+1)<<8)+(unsigned char)*(pkt+2)-2;
   int inc=(unsigned char)*(pkt+4);
   register int i, j;
   for(i=0,pkt+=5;i<len;i++,pkt++){
      j=(i/9)&0xFF;
      *pkt ^= (Key[i % 9] ^ ((j == inc) ? 0:j) ^ inc);
   }
}


I tried convert many times, but I can't success it.

That is my code, It isn't success decrypt data.
Crypt_Deploy         proc   XPacket:DWORD
   local PLength:DWORD, PIncrement:BYTE, j:DWORD
   ; Get size of packet
   mov edx, XPacket
   inc edx ; Index : 1
   mov cl, byte ptr [edx]
   movzx eax, cl
   mov ebx, 256d
   push edx
   mul ebx
   pop edx
   inc edx ; Index : 2
   mov cl, byte ptr [edx]
   movzx ebx, cl
   add eax, ebx
   ;sub eax, 2d
   mov PLength, eax
   ; Get increment
   add edx, 2d ; Index : 4
   mov cl, byte ptr [edx]
   mov PIncrement, cl
   ; Crypt packet
   inc edx ; Index : 5
   xor ecx, ecx
   CR_LPH:
      ; j = (i / 9) & 0xFF
      mov eax, ecx
      push edx
      cdq
      mov ebx, sizeof Crypt_RootKey ;9d
      div ebx
      pop edx
      and eax, 255d
      mov j, eax
      
      call WriteDec
      ; ROUTINE ALPHA | ((j == inc) ? 0:j) ^ inc
      movzx eax, PIncrement
      .if j == eax
         xor eax, 0      
      .else
         xor eax, j
      .endif
      ; ROUTINE BETA | Key[i % 9] ^
      movzx ebx, Crypt_RootKey[j]
      mov eax, ebx
      
      call WriteDec
      
      
      inc edx
      inc ecx
      cmp PLength, ecx
      je CR_LPE
      ;jmp CR_LPH
   CR_LPE:
   ret
Crypt_Deploy endp



Please help me.

Biterider

Hi
The best way to learn is to compile the C code with your favourite C compiler and to look at the disassembly. Once you understand what is happening at asm level, you can try to optimise the code.

Biterider

hutch--

Here is a partial conversion. The code is highly UNoptimised on purpose so it can be optimised manually. It may not be handling the "Key" variable correctly.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

static char Key[]="NexonInc.";
void Crypt(char *pkt)
  {
    int leng=((unsigned char)*(pkt+1)<<8)+(unsigned char)*(pkt+2)-2;
    int incr=(unsigned char)*(pkt+4);
    register int i, j;

    for(i=0,pkt+=5;i<leng;i++,pkt++)
    {
      j=(i/9)&0xFF;
      *pkt ^= (Key[i % 9] ^ ((j == incr) ? 0:j) ^ incr);
    }
  }

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

Crypt proc pkt:DWORD

    LOCAL j:DWORD
    LOCAL leng:DWORD
    LOCAL incr:DWORD
    LOCAL i:DWORD

    mov eax, pkt
    movzx ecx, BYTE PTR [eax+1]
    shl ecx, 8
    mov edx, pkt
    movzx eax, BYTE PTR [edx+2]
    lea ecx, [ecx+eax-2]
    mov leng, ecx
    mov edx, pkt
    movzx eax, BYTE PTR [edx+4]
    mov incr, eax
    mov i, 0
    add pkt, 1
    jmp lbl1

  lbl0:
    add i, 1
    add pkt, 1

  lbl1:
    mov ecx, i
    cmp ecx, leng
    jge lbl2
    mov eax, i
    cdq
    mov ecx, 9
    idiv ecx
    and eax, 255
    mov j, eax
    mov eax, i
    cdq
    mov ecx, 9
    idiv ecx
    movsx edx, BYTE PTR _Key[edx]
    mov eax, j
    sub eax, incr
    neg eax
    sbb eax, eax
    and eax, j
    xor edx, eax
    xor edx, incr
    mov ecx, pkt
    movsx eax, BYTE PTR [ecx]
    xor eax, edx
    mov ecx, pkt
    mov BYTE PTR [ecx], al
    jmp lbl0

  lbl2:

    ret

Crypt endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

woonsan

Thanks. (I developing free server of 'Nexus', the code is Nexus's crypt code)

drizz

.data
Key db "NexonInc."
.code
Crypt proc uses esi edi ebx pkt:ptr byte
local len:dword,inc_:dword
mov esi,pkt
xor eax,eax
mov ax,[esi]
mov al,[esi+2]
sub eax,2
mov len,eax
xor eax,eax
mov al,[esi+4]
mov inc_,eax
xor ecx,ecx
xor ebx,ebx; mod 9
xor edi,edi; div 9
add esi,5
.while (ecx<len)
cmp ebx,9
sbb edx,edx
lea edi,[edi+edx+1]
and ebx,edx
neg ebx
movzx eax,[Key+8+ebx]
neg ebx
xor eax,inc_
xor edx,edx
cmp edi,inc_
setne dl
neg edx
and edx,edi
xor eax,edx
add ecx,1
xor [esi],al
add ebx,1
add esi,1
.endw
ret
Crypt endp
The truth cannot be learned ... it can only be recognized.

raleeper

Quote from: hutch-- on July 01, 2007, 10:36:39 AM
Here is a partial conversion. The code is highly UNoptimised on purpose so it can be optimised manually. It may not be handling the "Key" variable correctly.

Hutch - did  you do all this by hand, using something like Biterider's suggestion:

    The best way to learn is to compile the C code with your favourite C compiler and to look at the disassembly. Once you understand what is happening at asm level, you can 
    try to optimise the code.

or do you have a [would be really, really useful to masm programmers] program for converting C to masm?


hutch--

raleeper,

Its a tool I wrote some time ago called ccon.exe. It uses the CL.EXE compiler and formats the asm output so its more or less readable. It is not perfect, will only work on single functions written in C only.

It should be posted somewhere in the masm32 sub forum.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

raleeper

Quote from: hutch-- on July 07, 2007, 12:09:04 PM
raleeper,

Its a tool I wrote some time ago called ccon.exe. It uses the CL.EXE compiler and formats the asm output so its more or less readable. It is not perfect, will only work on single functions written in C only.

It should be posted somewhere in the masm32 sub forum.

Sounds great, but, sorry to be obtuse, I can't find it.  Tried masm forum search for "ccon.exe" and "ccon". Tried masm forum search for "masm32 sub forum" and "masm32 subforum". Tried google for  "ccon.exe" and "ccon" and "masm32 subforum" - all without useful result.

Should I keep looking, or can you suggest how I might be looking better?

Tedd

No snowflake in an avalanche feels responsible.