-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.py
63 lines (54 loc) · 2.09 KB
/
decrypt.py
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
from base64 import urlsafe_b64encode
from hashlib import md5
from cryptography.fernet import Fernet
from utils import bin2str
class Decryptor:
"""Responsible for decrypting steganography of many images using a given key"""
def __init__(self, password: str):
_hash = md5(password.encode()).hexdigest()
cipher_key = urlsafe_b64encode(_hash.encode())
self.__encryptor = Fernet(cipher_key)
@staticmethod
def __extract_data_from_image(im, data_length, header_length=32) -> str:
data = ""
height, width, _ = im.shape
try:
for i in range(height):
for j in range(width):
pixel = im[i, j]
for k in range(3):
# Skip the header
if header_length:
header_length -= 1
continue
# Get the lsb
data += str(pixel[k] & 1)
data_length -= 1
if data_length == 0:
raise StopIteration
except StopIteration:
pass
return bin2str(data)
@staticmethod
def __extract_data_length_from_image(im, header_length=32) -> int:
data_length = ""
height, width, _ = im.shape
try:
for i in range(height):
for j in range(width):
pixel = im[i, j]
for k in range(3):
# Get the lsb
data_length += str(pixel[k] & 1)
header_length -= 1
if header_length == 0:
raise StopIteration
except StopIteration:
pass
return int(data_length, 2)
def decrypt(self, im) -> str:
"""Decrypts the hidden message in `im`."""
data_length = self.__extract_data_length_from_image(im)
data = self.__extract_data_from_image(im, data_length)
message = self.__encryptor.decrypt(data.encode()).decode()
return message