1# ํ์ผ ์ํธํ ์์ (File Encryption Examples)
2
3XOR ๊ธฐ๋ฐ ํ์ผ ์ํธํ ๋๊ตฌ ๊ตฌํ ์์
4
5## ํ์ผ ๋ชฉ๋ก
6
7### 1. `simple_xor.c`
8XOR ์ํธํ์ ๊ธฐ๋ณธ ์๋ฆฌ๋ฅผ ๋ณด์ฌ์ฃผ๋ ๊ฐ๋จํ ๋ฐ๋ชจ
9
10**ํ์ต ๋ด์ฉ:**
11- XOR ์ฐ์ฐ์ ๊ฐ์ญ์ฑ (A ^ B ^ B = A)
12- ๋นํธ ์ฐ์ฐ ๊ธฐ์ด
13- 16์ง์ ์ถ๋ ฅ
14- ๋ฐ์ด๋๋ฆฌ ํจํด ์ถ๋ ฅ
15
16**์ปดํ์ผ ๋ฐ ์คํ:**
17```bash
18gcc -Wall -Wextra -std=c11 simple_xor.c -o simple_xor
19./simple_xor
20```
21
22**์ถ๋ ฅ ์์:**
23```
24=== XOR ์ํธํ ๋ฐ๋ชจ ===
25
26์๋ณธ ๋ฉ์์ง: Hello, World!
27์๋ณธ (hex): 48 65 6C 6C 6F 2C 20 57 6F 72 6C 64 21
28
29์ฒซ ๊ธ์ 'H' XOR 'K' ์ฐ์ฐ ๊ณผ์ :
30 'H' = 72 (0b01001000)
31 'K' = 75 (0b01001011)
32 XOR = 3 (0b00000011)
33
34์ํธํ ์๋ฃ!
35๋ณตํธํ ๊ฒฐ๊ณผ: Hello, World!
36```
37
38---
39
40### 2. `file_encrypt.c`
41์ค์ฉ์ ์ธ ํ์ผ ์ํธํ ๋๊ตฌ (๊ธฐ๋ณธ ๋ฒ์ )
42
43**ํ์ต ๋ด์ฉ:**
44- ๋ฐ์ดํธ ๋จ์ ํ์ผ I/O (`fread`, `fwrite`)
45- ๋ช
๋ น์ค ์ธ์ ํ์ฑ (`argc`, `argv`)
46- ๋ฒํผ๋ง๋ ํ์ผ ์ฒ๋ฆฌ (4KB ๋ฒํผ)
47- ์๋ฌ ์ฒ๋ฆฌ (`perror`)
48- ํค ์ํ ์ ์ฉ (modulo ์ฐ์ฐ)
49
50**์ปดํ์ผ:**
51```bash
52gcc -Wall -Wextra -std=c11 file_encrypt.c -o file_encrypt
53```
54
55**์ฌ์ฉ๋ฒ:**
56```bash
57# ์ํธํ
58./file_encrypt -e input.txt output.enc mypassword
59
60# ๋ณตํธํ
61./file_encrypt -d output.enc decrypted.txt mypassword
62```
63
64**ํน์ง:**
65- ๊ฐ๋จํ๊ณ ์ง๊ด์ ์ธ ์ธํฐํ์ด์ค
66- XOR ํน์ฑ์ ์ํธํ/๋ณตํธํ ๋์ผ ์ฐ์ฐ
67- ๋ชจ๋ ํ์ผ ํ์
์ง์ (ํ
์คํธ, ๋ฐ์ด๋๋ฆฌ)
68
69---
70
71### 3. `file_encrypt_v2.c`
72ํฅ์๋ ํ์ผ ์ํธํ ๋๊ตฌ (ํค๋ + ๊ฒ์ฆ)
73
74**ํ์ต ๋ด์ฉ:**
75- ๊ตฌ์กฐ์ฒด ํ์ฉ (`FileHeader`)
76- ํ์ผ ๋งค์ง ๋๋ฒ ๊ฒ์ฆ
77- ํด์ ํจ์ ๊ตฌํ (djb2 ์๊ณ ๋ฆฌ์ฆ)
78- ํค ๊ฒ์ฆ (๋น๋ฐ๋ฒํธ ํ์ธ)
79- ํ์ผ ๋ฉํ๋ฐ์ดํฐ ์ ์ฅ
80- ์งํ๋ฅ ํ์
81- ๊ณ ์ ํฌ๊ธฐ ์ ์ ํ์
(`uint8_t`, `uint32_t`, `uint64_t`)
82
83**์ปดํ์ผ:**
84```bash
85gcc -Wall -Wextra -std=c11 file_encrypt_v2.c -o file_encrypt_v2
86```
87
88**์ฌ์ฉ๋ฒ:**
89```bash
90# ์ํธํ
91./file_encrypt_v2 encrypt secret.txt secret.enc mypassword
92
93# ํ์ผ ์ ๋ณด ํ์ธ
94./file_encrypt_v2 info secret.enc
95
96# ๋ณตํธํ
97./file_encrypt_v2 decrypt secret.enc decrypted.txt mypassword
98```
99
100**ํ์ผ ํค๋ ๊ตฌ์กฐ:**
101```
102โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
103โ Magic Number (4 bytes): "XENC" โ
104โ Version (1 byte): 1 โ
105โ Key Hash (4 bytes): ๊ฒ์ฆ์ฉ โ
106โ Original Size (8 bytes): ์๋ณธ ํฌ๊ธฐ โ
107โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
108โ ์ํธํ๋ ๋ฐ์ดํฐ โ
109โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
110```
111
112**์ถ๋ ฅ ์์:**
113```bash
114$ ./file_encrypt_v2 encrypt test.txt test.enc mypass
115์ํธํ ์ค...
116.
117์ํธํ ์๋ฃ: test.txt -> test.enc
118์๋ณธ ํฌ๊ธฐ: 38 ๋ฐ์ดํธ
119ํค ํด์: 0x6CBFD0D9
120
121$ ./file_encrypt_v2 info test.enc
122=== ์ํธํ ํ์ผ ์ ๋ณด ===
123๋งค์ง ๋๋ฒ: XENC
124๋ฒ์ : 1
125ํค ํด์: 0x6CBFD0D9
126์๋ณธ ํฌ๊ธฐ: 38 ๋ฐ์ดํธ
127ํ์ผ ํฌ๊ธฐ: 62 ๋ฐ์ดํธ
128ํค๋ ํฌ๊ธฐ: 24 ๋ฐ์ดํธ
129์ํธํ ๋ฐ์ดํฐ: 38 ๋ฐ์ดํธ
130
131$ ./file_encrypt_v2 decrypt test.enc out.txt wrongpass
132์ค๋ฅ: ์๋ชป๋ ๋น๋ฐ๋ฒํธ
133๊ธฐ๋ ํด์: 0x6CBFD0D9, ์
๋ ฅ ํด์: 0x289A5245
134```
135
136---
137
138## ํต์ฌ ๊ฐ๋
139
140### XOR ์ํธํ ์๋ฆฌ
141```
142์๋ฆฌ: A ^ K = C, C ^ K = A
143
144์์:
145 ์๋ณธ: 'H' (72) = 0b01001000
146 ํค: 'K' (75) = 0b01001011
147 ์ํธ: XOR = 0b00000011 (3)
148 ๋ณตํธ: 3 ^ 75 = 0b01001000 (72 = 'H')
149```
150
151### djb2 ํด์ ์๊ณ ๋ฆฌ์ฆ
152```c
153uint32_t hash = 5381;
154while ((c = *str++)) {
155 hash = hash * 33 + c;
156}
157```
158- ๋น ๋ฅด๊ณ ํจ๊ณผ์ ์ธ ๋ฌธ์์ด ํด์
159- ์ถฉ๋ ํ๋ฅ ๋ฎ์
160- ํค ๊ฒ์ฆ์ฉ์ผ๋ก ์ ํฉ
161
162---
163
164## ๋ณด์ ์ฃผ์์ฌํญ
165
166โ ๏ธ **๊ต์ก ๋ชฉ์ ์ ์ฉ**
167
168์ด ์์ ๋ ํ์ต ๋ชฉ์ ์ผ๋ก ์ ์๋์์ต๋๋ค. ์ค์ ๋ณด์์ด ํ์ํ ๊ฒฝ์ฐ:
169
1701. **XOR ์ํธํ์ ์ทจ์ฝ์ :**
171 - ํค ๋ฐ๋ณต ์ฌ์ฉ ์ ํจํด ๋
ธ์ถ
172 - ์๋ ค์ง ํ๋ฌธ ๊ณต๊ฒฉ(Known-plaintext attack)์ ์ทจ์ฝ
173 - ํค ๊ธธ์ด๊ฐ ์งง์ผ๋ฉด ๋ธ๋ฃจํธํฌ์ค ๊ณต๊ฒฉ ๊ฐ๋ฅ
174
1752. **์ค์ ์ฌ์ฉ ๊ถ์ฅ์ฌํญ:**
176 - AES-256 (๋์นญ ์ํธํ)
177 - RSA (๋น๋์นญ ์ํธํ)
178 - OpenSSL ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ
179 - ํค ์คํธ๋ ์นญ (PBKDF2, bcrypt)
180 - ์ํธ(Salt) ์ถ๊ฐ
181
1823. **์ด ๊ตฌํ์ ํ๊ณ:**
183 - ํค ์คํธ๋ ์นญ ์์
184 - ์ํธ ๋ฏธ์ฌ์ฉ
185 - ๋ฌด๊ฒฐ์ฑ ๊ฒ์ฆ(MAC) ์์
186 - ์ฌ์ ๊ณต๊ฒฉ ๋ฐฉ์ง ์์
187
188---
189
190## ํ
์คํธ ์์
191
192```bash
193# 1. ๊ธฐ๋ณธ ํ
์คํธ
194echo "Hello, World!" > test.txt
195./file_encrypt -e test.txt test.enc mykey
196./file_encrypt -d test.enc out.txt mykey
197diff test.txt out.txt # ๋์ผํด์ผ ํจ
198
199# 2. ๋ฐ์ด๋๋ฆฌ ํ์ผ ํ
์คํธ
200./file_encrypt_v2 encrypt /bin/ls ls.enc strongpass
201./file_encrypt_v2 decrypt ls.enc ls.dec strongpass
202diff /bin/ls ls.dec # ๋์ผํด์ผ ํจ
203
204# 3. ์๋ชป๋ ํค ํ
์คํธ
205./file_encrypt_v2 decrypt test.enc wrong.txt wrongkey
206# ์ถ๋ ฅ: ์ค๋ฅ: ์๋ชป๋ ๋น๋ฐ๋ฒํธ
207
208# 4. ๋์ฉ๋ ํ์ผ ํ
์คํธ (10MB)
209dd if=/dev/urandom of=large.bin bs=1M count=10
210time ./file_encrypt_v2 encrypt large.bin large.enc mypass
211./file_encrypt_v2 info large.enc
212```
213
214---
215
216## ์ปดํ์ผ ์ต์
์ค๋ช
217
218```bash
219gcc -Wall -Wextra -std=c11 file.c -o program
220```
221
222- `-Wall`: ๊ธฐ๋ณธ ๊ฒฝ๊ณ ํ์ฑํ
223- `-Wextra`: ์ถ๊ฐ ๊ฒฝ๊ณ ํ์ฑํ
224- `-std=c11`: C11 ํ์ค ์ฌ์ฉ
225- `-o program`: ์ถ๋ ฅ ํ์ผ๋ช
์ง์
226
227---
228
229## ํ์ต ์ฒดํฌ๋ฆฌ์คํธ
230
231- [ ] XOR ์ฐ์ฐ์ ๊ฐ์ญ์ฑ ์ดํด
232- [ ] ๋นํธ ์ฐ์ฐ์ ์ฌ์ฉ๋ฒ (`^`, `&`, `|`, `~`, `<<`, `>>`)
233- [ ] ๋ฐ์ดํธ ๋จ์ ํ์ผ I/O (`fread`, `fwrite`)
234- [ ] ๋ช
๋ น์ค ์ธ์ ํ์ฑ (`argc`, `argv`)
235- [ ] ๊ตฌ์กฐ์ฒด๋ฅผ ํ์ฉํ ํ์ผ ํค๋ ์ค๊ณ
236- [ ] ํด์ ํจ์ ๊ตฌํ (djb2)
237- [ ] ์๋ฌ ์ฒ๋ฆฌ ๋ฐ ๊ฒ์ฆ ๋ก์ง
238- [ ] ๋ฒํผ๋ง์ ํตํ ํจ์จ์ ์ธ ํ์ผ ์ฒ๋ฆฌ
239
240---
241
242## ์ฐธ๊ณ ์๋ฃ
243
244- [C11 Standard](https://en.cppreference.com/w/c/11)
245- [XOR Cipher - Wikipedia](https://en.wikipedia.org/wiki/XOR_cipher)
246- [djb2 Hash Function](http://www.cse.yorku.ca/~oz/hash.html)
247- [OpenSSL Documentation](https://www.openssl.org/docs/)
248
249---
250
251## ๊ด๋ จ ํ์ต ์๋ฃ
252
253- `/opt/projects/01_Personal/03_Study/content/ko/C_Programming/08_Project_File_Encryption.md`
254- `/opt/projects/01_Personal/03_Study/content/ko/C_Programming/14_Bit_Operations.md`