Yeah, seems to work. I take it that using a key larger than a byte was your instructor's idea?
; nasm -f elf32 myfile.asm
; ld -o myfile myfile.o -melf_i386
global _start
; comment does not match code!
KEY equ 326 ; any value between 1-255
BUFMAX equ 128 ; maximum buffer size
section .data
sPrompt db "Enter the plain text: ",0
sEncrypt db "Cipher text: ",0
sDecrypt db "Decrypted: ",0
; should probably be local to routine
newline db 10, 0
section .bss
buffer resb BUFMAX+1
bufSize resd 1
section .text
_start:
call InputTheString ; input the plain text
call TranslateBuffer ; encrypt the buffer
mov edx, sEncrypt ; display encrypted message
call DisplayMessage
call TranslateBuffer ; decrypt the buffer
mov edx, sDecrypt ; display decrypted message
call DisplayMessage
mov eax, 1
xor ebx, ebx
int 80h
;-----------------------------------------------------
InputTheString:
;
; Prompts user for a plaintext string. Saves the string
; and its length.
; Receives: nothing
; Returns: nothing
;-----------------------------------------------------
pushad
mov edx, sPrompt ; display a prompt
call WriteString
mov ecx, BUFMAX ; maximum character count
mov edx, buffer ; point to the buffer
call ReadString ; input the string
mov [bufSize], eax ; save the length
call Crlf
popad
ret
;-----------------------------------------------------
DisplayMessage:
;
; Displays the encrypted or decrypted message.
; Receives: EDX points to the message
; Returns: nothing
;-----------------------------------------------------
pushad
call WriteString
mov edx, buffer ; display the buffer
call WriteString
call Crlf
call Crlf
popad
ret
;-----------------------------------------------------
TranslateBuffer:
;
; Translates the string by exclusive-ORing each
; byte with the encryption key byte.
; Receives: nothing
; Returns: nothing
;-----------------------------------------------------
pushad
mov ecx, [bufSize] ; loop counter
mov esi,0 ; index 0 in buffer
L1:
; this is shonky - should be byte!
xor word [buffer + esi],KEY ; translate a byte
inc esi ; point to next byte
loop L1
popad
ret
;------------------------
;-------------------------
WriteString:
; expects: buffer in edx, zero-terminated
; returns: bytes written in eax
mov ecx, edx
xor edx, edx
.getlen:
cmp byte [ecx + edx], 0
jz .gotlen
inc edx
jmp .getlen
.gotlen:
mov ebx, 1
mov eax, 4
int 80h
ret
;-----------------------------
;-------------------------
ReadString:
; expects: buffer in edx, ecx = max
; returns: bytes written in eax
xchg ecx, edx
mov ebx, 1
mov eax, 3
int 80h
mov byte [ecx + eax + 1], 0
ret
;-----------------------------
;----------------------------
Crlf:
push edx
mov edx, newline
call WriteString
pop edx
ret
;--------------------------