Skip to content

Commit ae5e101

Browse files
committed
retrieval code
1 parent 45edff6 commit ae5e101

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from cryptography.fernet import Fernet
2+
from PIL import Image
3+
import numpy as np
4+
import sys
5+
6+
if len(sys.argv) < 3:
7+
print("Provide appropriate commandline arguments.")
8+
sys.exit()
9+
10+
11+
image_path = sys.argv[1]
12+
key_path = sys.argv[2]
13+
14+
15+
def decrypt_message(enc_message, key):
16+
dec_message = Fernet(key).decrypt(enc_message)
17+
return dec_message
18+
19+
20+
def retrieve_info(image, key):
21+
22+
binary_data = ""
23+
image = np.array(image)
24+
25+
for values in image:
26+
for pixel in values:
27+
r, g, b = [format(i, "08b") for i in pixel]
28+
binary_data += r[-1]
29+
binary_data += g[-1]
30+
binary_data += b[-1]
31+
32+
all_bytes = [binary_data[i: i+8] for i in range(0, len(binary_data), 8)]
33+
34+
decoded_data = ""
35+
for byte in all_bytes:
36+
decoded_data += chr(int(byte, 2))
37+
if decoded_data[-5:] == "#####".encode():
38+
break
39+
40+
message = decrypt_message(decoded_data[:-5].encode(), key)
41+
42+
return decoded_data[:-5]
43+
44+
45+
steg_image = Image.open(image_path, 'r')
46+
with open(key_path, "rb") as f:
47+
key = f.read()
48+
49+
print(f"Secret Message:{retrieve_info(steg_image, key)}")

0 commit comments

Comments
 (0)