-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxor.py
More file actions
29 lines (23 loc) · 919 Bytes
/
xor.py
File metadata and controls
29 lines (23 loc) · 919 Bytes
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
def xor_data(data: bytes, key: bytes) -> bytes:
"""XOR each byte of data with repeating key bytes."""
return bytes([b ^ key[i % len(key)] for i, b in enumerate(data)])
# Hide file_a inside file_b
def hide_file(file_a, file_b, key, out_file):
with open(file_a, "rb") as f1, open(file_b, "rb") as f2:
data_a = f1.read()
data_b = f2.read()
key_bytes = key.encode()
hidden = xor_data(data_a, key_bytes)
# Combine: original B + XORed A
with open(out_file, "wb") as out:
out.write(data_b + b"\n---HIDDEN---\n" + hidden)
# Extract hidden A
def extract_file(out_file, key, extracted_file):
with open(out_file, "rb") as f:
combined = f.read()
# Split on marker
data_b, hidden = combined.split(b"\n---HIDDEN---\n")
key_bytes = key.encode()
data_a = xor_data(hidden, key_bytes)
with open(extracted_file, "wb") as f:
f.write(data_a)