32-bit assembly language creates output file problem

After some time, working with this program, it continues to stop after creating the output file. I am using Visual Basic 2010 and still new to this. Home question:

Description (Symmetric Encryption):

  • Encoding

    • Ask user to enter text
    • Ask the user to enter the private key in this range [1-255]. Perform range validation.
    • Encrypt the input text with the provided private key, put the encrypted text in a file named by the user.
  • Decoding

    • Ask the user to specify the file to decode.
    • Download the encrypted text from this file and try to decrypt it, not assuming that the private key is used in encoding.
    • Put all test results in a separate file named by the user.
    • e. Find out what is the most reasonable result (or original text).

I can understand how to encrypt the text, but I cannot create the output file with the libraries located at this address of the tutorial: http://www.kipirvine.com/asm/examples/index.htm

I will include my code below and show through the commented material how many attempts I had on this. The book does not explain me very well, so if I could see what she was trying to say, it would be very useful!

Thanks in advance!

INCLUDE Irvine32.inc BUFMAX = 128 ; maximum buffer size KEYMAX = 128 ; maximum buffer size BUFFER_SIZE = 5000 .data sPrompt BYTE "Enter some text message: ", 0 keyPrompt BYTE "Enter a private key [1-255]: ", 0 cFile BYTE "Enter a filename for cypher text: ", 0 sEncrypt BYTE "Cypher text ", 0 sDecrypt BYTE "Decrypted: ", 0 error BYTE "The key must be within 1 - 255! ", 0 buffer BYTE BUFMAX + 1 DUP(0) bufSize DWORD ? keyStr BYTE KEYMAX + 1 DUP(0) keySize DWORD ? key DWORD ? filename BYTE "newfile.txt ", 0 fileHdl DWORD ? bufFile BYTE BUFFER_SIZE DUP (?) .code main PROC call InputTheString ; input the plain text call InputTheKey ; input the security key call CypherFile ; input a cypher filename ;call TranslateBuffer ; encrypt the buffer ;mov edx, OFFSET sEncrypt ; display encrypted message ;call DisplayMessage ;call TranslateBuffer ; decrypt the buffer ;mov edx, OFFSET sDecrypt ; display decrypted message ;call DisplayMessage exit main ENDP InputTheKey PROC pushad ; save 32-bit registers LK: mov edx, OFFSET keyPrompt ; display a prompt call WriteString ; Enter a private key [1-255] call Crlf ; start a new line call ReadInt ; read int into system mov key, eax ; store int into keyStr cmp eax, 255 ; compare newly read int ja LC ; jump if above 255 to LC cmp eax, 1 ; compare newly read int jb LC ; jump if below 1 to LC jmp LR ; if between range jump to LR LC: mov edx, OFFSET error ; The key must be within 1 - 255! call WriteString ; Display the error call Crlf ; start a new line loop LK ; loop back to enter the security key LR: popad ; restore the registers ret InputTheKey ENDP CypherFile PROC pushad mov edx, OFFSET cFile ; "Enter a filename for cypher text call WriteString ; Enter a name for encrypted file call Crlf call ReadString ; Store the filename in eax ;mov filename, eax mov edx, OFFSET filename ;push eax ;mov eax, fileHdl ;mov edx, OFFSET bufFile ;mov ecx, BUFFER_SIZE ;mov edx, "C:\outputtext.txt" call CreateOutputFile ;mov edx, OFFSET filename ;mov ecx, SIZEOF filename ;push eax ;mov eax, bufSize call WriteToFile pop eax ;call CloseFile ret CypherFile ENDP InputTheString PROC pushad ; save 32-bit registers mov edx, OFFSET sPrompt ; display a prompt call WriteString ; "Enter some text message" call Crlf ; start a new line mov ecx, BUFMAX ; maximum character count mov edx, OFFSET buffer ; point to the buffer call ReadString ; input the string mov bufSize, eax ; save the length popad ret InputTheString ENDP COMMENT ! DisplayMessage PROC pushad call WriteString mov edx, OFFSET buffer ; display the buffer call WriteString call Crlf call Crlf popad ret DisplayMessage ENDP TranslateBuffer PROC pushad mov ecx, bufSize ; loop counter mov esi, 0 ; index 0 in buffer mov edi, 0 ; index 0 in the key L1: mov al, keyStr[edi] ; get a character from encryption key xor buffer[esi], al ; translate a byte inc esi ; point to next byte inc edi ; go to next position in key cmp edi, keySize ; compare if equal to size of the key jb L2 mov edi, 0 ; reset to beginning of the key L2: loop L1 popad ret TranslateBuffer ENDP ! END main 
+4
source share
1 answer

Where to begin?

Are you missing a few options for ReadString? Perhaps a pointer to where to store the entered file name? Buffer size to get file name?

CypherFile you CypherFile all the registers pushad stack with pushad , but in the end you just pop eax . The big problem is there what should be popad

At its core, it does not write anything to the output file, since the WriteToFile parameters are commented out.

EDIT - Against my "simple code" you should tell ReadString where to save the entered file name and buffer size. Then pass this to CreateOutputFile - In CyperFile proc -

 mov edx, offset buffer ; you are missing a buffer for filename mov ecx, BUFMAX ; buffer size call ReadString mov edx, offset buffer ; Pass this to CreateOutputFile call CreateOutputFile 

Now, reading the source code for CreateOutputFile , he says that he returns a successful file, saving it. You use this with CloseFile when you finish writing to a file. If the file is not created successfully, it will return INVALID_HANDLE_VALUE

+2
source

Source: https://habr.com/ru/post/1448043/


All Articles