OneCompiler

Encryption and decryption. Asm

1722

section .data
prompt db 'Enter 1 to encrypt or 2 to decrypt:', 0
input db 'HELLO', 0 ; Null-terminated string
key db 0x3F ; XOR key
output db 80 dup(0) ; Space for output, same length as input

section .bss
choice resb 1 ; Space for user's choice

section .text
global _start

_start:
; Print prompt
mov edx, len prompt
mov ecx, prompt
mov ebx, 1
mov eax, 4
int 0x80

; Read choice
mov eax, 3
mov ebx, 0
mov ecx, choice
mov edx, 1
int 0x80

; Check choice
cmp byte [choice], '1'
je encrypt
cmp byte [choice], '2'
je decrypt
jmp done

encrypt:
mov esi, input ; Source index points to input
mov edi, output ; Destination index points to output

encrypt_loop:
lodsb ; Load byte at [esi] into AL, and increment ESI
test al, al ; Test if end of string (null byte)
jz done ; If zero, end of input string

xor al, [key]            ; XOR AL with key
stosb                    ; Store result in output buffer
jmp encrypt_loop         ; Repeat the loop

decrypt:
mov esi, input ; Source index points to input
mov edi, output ; Destination index points to output

decrypt_loop:
lodsb ; Load byte at [esi] into AL, and increment ESI
test al, al ; Test if end of string (null byte)
jz done ; If zero, end of input string

xor al, [key]            ; XOR AL with key
stosb                    ; Store result in output buffer
jmp decrypt_loop         ; Repeat the loop

done:
mov eax, 1 ; Syscall number for sys_exit
xor ebx, ebx ; Exit code 0
int 0x80 ; Call kernel

len equ $ - prompt