Author Topic: write encrypted string to file  (Read 1087 times)


  • Regular Member
  • *
  • Posts: 9
write encrypted string to file
« on: October 17, 2020, 06:55:05 AM »
I have a program to ask for user input and then encrypt that text using xor command. However, instead of displaying that encrypted text, I want to save that encrypt text into a file, how should I do it? I've no knowledge of reading/writing file in assembly language, so a solution on writing this string into a file would be helpful. Here's my program:

Code: [Select]
;Encryption Program               (Encrypt.asm)

; This program demonstrates simple symmetric
; encryption using the XOR instruction.

KEY = 239           ; any value between 1-255
BUFMAX = 128        ; maximum buffer size

sPrompt  BYTE  "Enter the plain text: ",0
sEncrypt BYTE  "Cipher text:          ",0
sDecrypt BYTE  "Decrypted:            ",0
buffer   BYTE   BUFMAX+1 DUP(0)
bufSize  DWORD  ?

main PROC
    call    InputTheString      ; input the plain text
    call    TranslateBuffer ; encrypt the buffer
    mov edx,OFFSET sEncrypt ; display encrypted message
    call    DisplayMessage

main ENDP

InputTheString PROC
; Prompts user for a plaintext string. Saves the string
; and its length.
; Receives: nothing
; Returns: nothing
    mov edx,OFFSET sPrompt  ; display a prompt
    call    WriteString
    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
    call    Crlf
InputTheString ENDP

DisplayMessage PROC
; Displays the encrypted or decrypted message.
; Receives: EDX points to the message
; Returns:  nothing
    call    WriteString
    mov edx,OFFSET buffer   ; display the buffer
    call    WriteString
    call    Crlf
    call    Crlf
DisplayMessage ENDP

TranslateBuffer PROC
; Translates the string by exclusive-ORing each
; byte with the encryption key byte.
; Receives: nothing
; Returns: nothing
    mov ecx,bufSize     ; loop counter
    mov esi,0           ; index 0 in buffer
    xor buffer[esi],KEY ; translate a byte
    inc esi             ; point to next byte
    loop    L1

TranslateBuffer ENDP
END main


  • Member
  • *****
  • Posts: 2583
Re: write encrypted string to file
« Reply #1 on: October 17, 2020, 07:12:43 AM »

Welcome to the Masm Forum.

The  write_disk_file function from masm32.lib can do the job :


write_disk_file proc lpName:DWORD,lpData:DWORD,fl:DWORD


write_disk_file writes the content of a memory buffer to a disk file


1. lpName The address of a zero terminated string that contains the file name.
2. lpData The address of the buffer that contains the data to write.
3. fl The number of bytes to write to the file.

Return Value

The return value is the number of bytes written if the procedure succeeds or zero if it fails.


This procedure is designed to write a complete disk file each time it is called. It will overwrite an existing file of the same name.


  • Member
  • ****
  • Posts: 725
Re: write encrypted string to file
« Reply #2 on: October 17, 2020, 09:15:31 AM »
Code: [Select]
    include \masm32\include\

    call main
    invoke ExitProcess,0

main proc uses esi edi ebx

LOCAL plc:DWORD ;cmd line pointer
LOCAL saida:DWORD ;output handle
LOCAL manipulador_arquivo:dword ;file handle
LOCAL tamanho:DWORD ;source file size
LOCAL mapeado:DWORD ;ptr source file
LOCAL mem_aloc:DWORD ;ptr mem alloc
LOCAL nada:dword
LOCAL arq_mape:DWORD ;mapped file handle

invoke GetStdHandle,STD_OUTPUT_HANDLE
mov saida,eax

invoke GetCommandLine
mov plc,eax

;search for 1st space char, file name supposed to be after
;to-do: deal better with this
dec eax
@@: inc eax
movzx ecx,byte ptr [eax]
test ecx,ecx
jz fim
cmp ecx," "
jnz @B
@@: inc eax

invoke CreateFile,eax,GENERIC_READ,0,0,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0 ;open source file
mov manipulador_arquivo,eax ;handle

invoke GetFileSize, manipulador_arquivo,NULL ;sizeof source file
mov tamanho,eax
invoke CreateFileMapping,manipulador_arquivo,NULL,PAGE_READONLY,0,0,0 ;mapped file handle
mov arq_mape,eax
invoke MapViewOfFile,eax,FILE_MAP_READ,0,0,0 ;map it
mov mapeado,eax
invoke CloseHandle,manipulador_arquivo ;don't need this anymore

invoke GlobalAlloc,GMEM_FIXED or GMEM_ZEROINIT,tamanho ;start a zero block memory
mov mem_aloc,eax

mov esi,mapeado
mov edi,mem_aloc
mov ecx,tamanho
mov edx,0
mov ebx,0

;search for ";" inside source file, if found, ignore it
;if not found, continue storing source file in memory buffer
.while ecx != 0
movzx eax,byte ptr [esi+edx]
inc edx
.if eax == ";"
mov byte ptr [edi+ebx],al
inc ebx
dec ecx
mov tamanho,ebx ;sizeof

invoke UnmapViewOfFile,mapeado
invoke WriteFile,saida,mem_aloc,tamanho,addr nada,0
invoke GlobalFree,mem_aloc

main endp

end start
C:\masm32\Bin\ML.EXE /c /coff /Cp /nologo /I"C:\masm32\Include" "cmd_line.asm"
C:\masm32\Bin\LINK.EXE /SUBSYSTEM:CONSOLE /RELEASE /VERSION:4.0 /LIBPATH:"C:\masm32\Lib" /OUT:"cmd_line.exe" "cmd_line.obj"
use it as: "cmd_line cmd_line.asm"
this snippet will open file cmd_line.asm and echo that context ignoring ";" when found.

Xor cipher have some proprieties, input data have same size as random data and/or output size. One possible attack is a xor search; if you know target language you can try search for some comon words.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything