News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

tasm encoding file

Started by thornography, May 07, 2015, 08:36:01 PM

Previous topic - Next topic

thornography

hello brothers. ı have a code that make file I/O. for example, input.txt: "hello"

when we execute this code in cmd. (this takses 2 arguments)


Destkop>program.exe input.txt output.txt.


after, it creates output.txt file, output.txt:"hello"


ı have to change this code. ı need to write encoding file. each character XORing with a number. for example as a ASCI number

xor 64,48
= bla bla bla. as a result output.txt:"qed3lvq...." lıke this.

please. ı really tried but I could not find where to do changes in this code. ım waiting for your help.



.586                ; islemci tipi
.model flat         ; bellek modeli

BUFFER_SIZE    equ    32768 ; Dosya giris/cikis icin tampon boyu
tab    equ    9             ; Tab karakter kodu

FILE_ATTRIBUTE_NORMAL   equ 80h ; normal dosya ozellikleri

OPEN_EXISTING   equ 3           ; var olan dosya
GENERIC_READ    equ 80000000h   ; okuma modu

CREATE_ALWAYS   equ 2           ; yeni yaratilacak dosya
GENERIC_WRITE   equ 40000000h   ; yazma modu

INVALID_HANDLE_VALUE    equ -1  ; dosya yaratilamadi, donus degeri

STD_OUTPUT_HANDLE   equ -11     ; standart cikis tutmaci

.data               ; boyutu bilinen veri tanimlamalari
CmdLinePtr dd  0

SrcFileNamePtr dd 0 ; kaynak dosya adina gosterici
DstFileNamePtr dd 0 ; hedef dosya adina gosterici

SrcFileHandle dd 0  ; kaynak dosya tutmaci
DstFileHandle dd 0  ; hedef dosya tutmaci

BytesRead    dd 0   ; okunan byte sayisi, donus degeri
BytesWritten dd 0   ; yazilan byte sayisi

BadSourceMsg  db "Kaynak dosya acilamadi",13,10
BadSourceMsgLen equ $ - BadSourceMsg

BadDestMsg    db "Hedef dosya acilamadi",13,10
BadDestMsgLen equ $ - BadDestMsg

.data?              ; boyutu bilinmeyen veri tanimlamalari
TempBuffer  db BUFFER_SIZE dup(?)
CmdLineTmp  db 1024 dup(?)

extrn   GetCommandLineA:near
extrn   CreateFileA:near
extrn   ReadFile:near
extrn   WriteFile:near
extrn   GetStdHandle:near
extrn   CloseHandle:near
extrn   ExitProcess:near

public  _start
.code               ; kod kismi
_start:

call    GetCommandLineA         ; M[CmdLinePtr] <- Komut satirinin tumu
mov     [CmdLinePtr], eax

mov     esi, [CmdLinePtr]       ; islemler sirasinda CmdLineTmp ile calis
mov     edi, offset CmdLineTmp

call    ScanBlanks              ; bosluklari atla
call    ScanArg                 ; program adini (argv[0]) atla
call    ScanBlanks              ; bosluklari atla
mov     [SrcFileNamePtr], edi   ; M[SrcFileNamePtr] <- giris dosya adi
call    ScanArg                 ; giris dosya adini atla
call    ScanBlanks              ; bosluklari atla
mov     [DstFileNamePtr], edi   ; M[DstFileNamePtr] <- cikis dosya adi
call    ScanArg                 ; cikis dosya adini atla

push    large 0    ; giris dosyasini ac
push    large FILE_ATTRIBUTE_NORMAL
push    large OPEN_EXISTING
push    large 0    ; guvenlik oznitelikleri
push    large 0    ; paylasim modu
push    large GENERIC_READ
push    [SrcFileNamePtr]
call    CreateFileA
cmp     eax, INVALID_HANDLE_VALUE
je      BadSrc
mov     [SrcFileHandle], eax        ; M[SrcFileHandle] <- giris dosya tutmaci

push    large 0    ; cikis dosyasini ac
push    large FILE_ATTRIBUTE_NORMAL
push    large CREATE_ALWAYS
push    large 0    ; guvenlik oznitelikleri
push    large 0    ; paylasim modu
push    large GENERIC_WRITE
push    [DstFileNamePtr]
call    CreateFileA
cmp     eax, INVALID_HANDLE_VALUE
je      BadDst
mov     [DstFileHandle], eax        ; M[DstFileHandle] <- cikis dosya tutmaci

CopyLoop:                   ; ReadFile geri donus degerlerini yigina it
push    0                   ; giris dosyasından okunacak tampona gosterici
push    offset BytesRead    ; okunan byte sayisi donus degeri
push    BUFFER_SIZE         ; okunabilecek en buyuk blok uzunlugu
push    offset TempBuffer   ; tampon
push    [SrcFileHandle]     ; giris dosyasi tutmaci
call    ReadFile            ; giris dosyasindan oku
cmp     [BytesRead], 0      ; dosya sonu ise
je      EndCopy             ; kopyalamayi bitir

push    0                   ; benzer sekilde WriteFile icin gerekenleri yigina
push    offset BytesWritten ; cikis dosyasina yazilan byte geri donus degeri
push    [BytesRead]         ; okunan tum bytelari yaz
push    offset TempBuffer   ; tampon
push    [DstFileHandle]     ; cikis dosyasi tutmaci
call    WriteFile           ; cikis dosyasina yaz
jmp     CopyLoop            ; giris dosyasinin tumu islenene kadar tekrarla

EndCopy:

push    [SrcFileHandle]     ; giris dosyasini kapat
call    CloseHandle

push    [DstFileHandle]     ; cikis dosyasini kapat
call    CloseHandle

push    large 0             ; isletim sistemine geri don
call    ExitProcess

BadSrc:
mov     esi, offset BadSourceMsg    ; giris dosyasi acilamadi iletisi
mov     ecx, BadSourceMsgLen
jmp     ErrorExit                   ; standart hataya yaz

BadDst:
mov     esi, offset BadDestMsg      ; cikis dosyasi acilamadi iletisi
mov     ecx, BadDestMsgLen

ErrorExit:
push    large 0                     ; dosyadan okunacak tampona gosterici
push    offset BytesWritten         ; cikis dosyasina yazilan byte donus degeri
push    ecx                         ; byte sayisi
push    esi                         ; tampon
push    large STD_OUTPUT_HANDLE     ; stadart hata tutmaci
call    GetStdHandle                ; tutmaci al
push    eax                         ; yigina koy
call    WriteFile                   ; stadart hataya yaz
push    large 0                     ; isletim sistemine geri don
call    ExitProcess

ScanBlanks_1:               ; bosluklari atla
inc     esi
ScanBlanks:
mov     al, [esi]
cmp     al, ' '
je      ScanBlanks_1
cmp     al, tab
je      ScanBlanks_1
ret                         ; bosluk olmayan ilk karakter pozisyonu ESI de

ScanArg:                ; bosluk olmayanlari (argumanlari) atla
mov     al, [esi]
cmp     al, 0
je      ExitScanArg
cmp     al, '"'
je      ScanQuoted
ScanUnquoted:           ; bosluk karakteri icermeyen argumanlari isle
mov     [edi], al
inc     esi
inc     edi
mov     al, [esi]
cmp     al, 0
je      ExitScanArg
cmp     al, ' '
je      ExitScanArg
cmp     al, tab
je      ExitScanArg
cmp     al, '"'
je      ExitScanArg
jmp     ScanUnquoted
ScanQuoted:
inc     esi             ; bosluk karakterli argumanlardaki tirnaklari isle
mov     al, [esi]
cmp     al, 0
je      ExitScanArg
cmp     al, '"'
je      ExitQuoted
ScanQuoted_1:
mov     [edi], al
inc     esi
inc     edi
mov     al, [esi]
cmp     al, 0
je      ExitScanArg
cmp     al, '"'
je      ExitQuoted
jmp     ScanQuoted_1
ExitQuoted:
inc     esi             ; tirnaklari da atla
ExitScanArg:
mov     byte ptr [edi], 0    ; karakter dizisi sonuna 0 koy
inc     edi
ret                     ; argumandan sonraki ilk karakter ESI de

end _start




jj2007

Folks,

Check whether you are allowed to use libraries beyond the Windows API (CreateFile, ReadFile, ...). Commands like FileRead$() (or InputFile, the Masm32 equivalent) are very convenient, but your teacher might insist that you learn the basics first.

rrr314159

#3
This code of course is dealing with ascii text - written words - is that relevant? Dunno if you need to just do quoted strings, or just unquoted, or the contents of quoted strings but not the quotes, or what? Perhaps this is only an example to get you started: you're to encode every character regardless, and throw out all the extra logic about quotes, blanks. The code is primitive, do you want to upgrade to more modern methods (as jj2007 suggests)?

Anyway, the basic idea is, there are two places where the code says "mov [edi], al". Just before those (one of them, or both of them, dunno what your homework assignment is exactly) you need to xor the byte "al", to encode it, b4 sending it along to the destination.

Unless, of course, I've misunderstood something vitally important
I am NaN ;)

AssemblyChallenge

Well, the code looks a little bit "closed"  :lol:

The Read and Write to file procedures are outside the main program. My humble advice would be to insert the xorxing code just after reading the file and do the XOR on TempBuffer, and then, write to the file.

Regards. 

rrr314159

That would be sensible, and a lot more flexible, allowing more advanced crypto, not just per-byte. I'd try the quick-and-dirty approach first - even leave the text processing logic in place, useless as it may be - to see if it works, take it from there. Don't know how advanced OP is
I am NaN ;)

thornography

Quote from: rrr314159 on May 08, 2015, 01:34:56 AM
This code of course is dealing with ascii text - written words - is that relevant? Dunno if you need to just do quoted strings, or just unquoted, or the contents of quoted strings but not the quotes, or what? Perhaps this is only an example to get you started: you're to encode every character regardless, and throw out all the extra logic about quotes, blanks. The code is primitive, do you want to upgrade to more modern methods (as jj2007 suggests)?

Anyway, the basic idea is, there are two places where the code says "mov [edi], al". Just before those (one of them, or both of them, dunno what your homework assignment is exactly) you need to xor the byte "al", to encode it, b4 sending it along to the destination.

Unless, of course, I've misunderstood something vitally important

the problem is not tasm32 or masm32 taught us. we learned only a little simple assembly. this assignment like a research topics. nobody could this assignment yet. ı know. can be more simplified methods. but we need to write "mynumber.exe" after we write 2 arguments(ınput text_name and output text_name) also they were given this code. ı dont know how many lines changed or ı dont know where change.  also ı need to have key value. key value must be "myschool number mod 256" namely it is 108. also ı need to convert this to hexadecimal? ı really really dont know.

thornography

ımmm. ı add the line "xor tembuffer,114" after the reading in loop. one character changed my output text. ı think ım close. only one thing is missing

thornography

i convert the key value to hexadecimal number. when i execute this code the characters are converted japanese alphabet. am ı wrong? or right

rrr314159

good work, keep it up! You deserve that "A"
I am NaN ;)

thornography

it is OK. you have a good thing. especially rrr thank you all.  ı love you all..