-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.asm
More file actions
85 lines (71 loc) · 1.9 KB
/
Copy pathencryption.asm
File metadata and controls
85 lines (71 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
section .data
outputFile db 'output.enc', 0
key db 0xAA ; Simple XOR key
section .bss
buffer resb 256 ; Buffer to hold file content
filename resb 256 ; Buffer to hold filename
section .text
extern fopen, fread, fwrite, fclose
global _start
_start:
; Read the filename from command line arguments
; Arguments are passed on the stack.
mov eax, [esp + 4] ; First argument (filename)
; Check the number of bytes read
cmp eax, 256 ; compare with buffer size
jge buffer_overflow ; If bytes read >= 256, jump to overflow handler
mov [filename], eax ; Store filename in buffer
; Open the input file
push outputFile
push eax
call fopen
add esp, 8
mov ebx, eax ; Save file handle to ebx
; Read from the input file
push 256
push buffer
push 1
push ebx
call fread
add esp, 16
; Encrypt/Decrypt using XOR
mov ecx, eax ; Number of bytes read
xor_loop:
test ecx, ecx
jz write_file ; If no bytes left, go to write file
dec ecx
mov al, [buffer + ecx]
xor al, [key]
mov [buffer + ecx], al
jmp xor_loop
; Close input file
push ebx
call fclose
add esp, 4
; Open the output file
push outputFile
push 0 ; No mode means create or overwrite
call fopen
add esp, 8
mov ebx, eax ; Save output file handle to ebx
; Write to the output file
push 256
push buffer
push 1
push ebx
call fwrite
add esp, 16
; Close output file
push ebx
call fclose
add esp, 4
; Exit program
mov eax, 1 ; sys_exit
xor ebx, ebx
int 0x80 ; Call kernel
buffer_overflow:
; Handle buffer overflow case
; For demonstration, just exit with an error code
mov eax, 1 ; sys_exit
mov ebx, 1 ; return code 1 (indicating error)
int 0x80 ; Call kernel