-2

I'm currently doing an encryption in some of my files as an exercise. However, I'm having a trouble in overwriting a .txt file and having a hard time identifying what went wrong. Below is the part of the code I've been working on which I think has the problem.

  push 0
  push FILE_ATTRIBUTE_NORMAL
  push OPEN_EXISTING
  push 0
  push 0
  push FILE_READ_DATA
  push offset fData.cFileName
  call CreateFile

  mov hndl, eax

  push 0
  push hndl
  call GetFileSize

  mov fSize, eax

  push 0
  push offset bfrLen
  push fSize
  push offset bfr
  push hndl
  call ReadFile

  push hndl
  call CloseHandle

  lea esi, bfr
  mov al, [esi]
  cmp al, 7fh
  jg skip
  encrypt:
    mov al, [esi]
    xor al, 0ffh
    mov [esi], al
    inc esi
    mov al, [esi]
    cmp al, 00h
    jne encrypt

  push 0
  push FILE_ATTRIBUTE_NORMAL
  push CREATE_ALWAYS
  push 0
  push 0
  push FILE_WRITE_DATA
  push offset file
  call CreateFile

  mov hndl, eax

  push offset bfr
  call lstrlen

  push 0
  push offset bfrLen
  push fSize
  push offset bfr
  push hndl
  call WriteFile

  push hndl
  call CloseHandle

  skip:
    ret

Review my code, guys! Thanks in advance.

Pentagon
  • 58
  • 9
  • 2
    Use a debugger and a syscall tracer. Add error handling to your program. Also, comment your code especially if you want others to help. Give more details than "having a trouble", describe what happens. That said, `push WriteFile` is very suspicious, didn't you mean `call WriteFile`? – Jester Jun 08 '17 at 12:47
  • I didn't notice that. Thanks. However the code still doesn't work. – Pentagon Jun 08 '17 at 12:49
  • Also, your `jg skip` is suspicious, it skips everything including writing out the file. Hard to tell whether you want that or not, with the limited information you provided. "doesn't work" is still not better than "having a trouble". See also [ask]. – Jester Jun 08 '17 at 12:52
  • I'm sure it does "work", it just probably does not do what you did want or expect. Similar code usually doesn't stop CPU from executing it and it will do **something**. Often by using debugger you can overview what is that "something". – Ped7g Jun 08 '17 at 12:55
  • _"having a hard time identifying what went wrong"_ Check every return value, and call `GetLastError` if a WinApi function returns something other than "success". – Michael Jun 08 '17 at 14:30
  • 2
    `CreateFile` can fail for tons of reasons, like wrong file name, wrong path (extremely common problem), wrong permissions, already open, etc., etc. And you don't check for any of those! – Bo Persson Jun 08 '17 at 14:43

1 Answers1

1
mov al, [esi]
cmp al, 7fh
jg skip

This is a useless test! Nothing can ever be greater (signed byte) than 127.
Perhaps you meant to test for the above (unsigned byte) condition?

cmp byte [esi], 7Fh
ja  skip             ;Skip if from 128 to 255

Just a thought. Maybe this test must be repeated with each iteration? We can't possibly know these details of your task.

Fifoernik
  • 9,779
  • 1
  • 21
  • 27