-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathController.py
97 lines (84 loc) · 3.51 KB
/
Controller.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import zlib
from functools import partial
from ImageType import ImageType
from OneTimePad import OneTimePad
from typing import Callable
from VideoType import VideoType
from WavType import WavType
"""
[1, 9]
The higher the number the smaller the resulted message
(At the cost of being more computationally intensive)
"""
COMPRESSION_LEVEL = 9
class Controller:
def handle_encode(
self,
filepath: str,
secret_message: str,
nr_lsb_used: int,
apply_encryption: bool,
select_output_path: Callable
):
exception = None
mask = None
try:
# Compress and encrypt the secret message if requested
compressed_message = zlib.compress(bytes(secret_message,'utf-8'), COMPRESSION_LEVEL)
if apply_encryption:
otp = OneTimePad(len(compressed_message))
final_message = otp.encrypt(compressed_message)
mask = otp.get_hexmask()
else:
final_message = compressed_message
# Determine the carrier's extension and call the appropriate encoding function
extension = (filepath.split('.'))[1]
select_output_path = partial(select_output_path, extension)
match extension:
case "png":
ImageType.encode(filepath, final_message, nr_lsb_used, select_output_path)
case "jpg":
ImageType.encode(filepath, final_message, nr_lsb_used, select_output_path)
case "wav":
WavType.encode(filepath, final_message, nr_lsb_used, select_output_path)
case "mp4":
VideoType.encode(filepath, final_message, nr_lsb_used, select_output_path)
case default:
raise ValueError(f"Unable to support encoding for {extension} files!")
except Exception as e:
exception = e
return mask, exception
def handle_decode(
self,
filepath: str,
nr_lsb_used: int,
mask: bytes
):
exception = None
decompressed_message = None
try:
# Determine the carrier's extension and call the appropriate decoding function
extension = (filepath.split('.'))[1]
match extension:
case "png":
secret_message = ImageType.decode(filepath, nr_lsb_used)
case 'jpg':
secret_message = ImageType.decode(filepath, nr_lsb_used)
case "wav":
secret_message = WavType.decode(filepath, nr_lsb_used)
case "mp4":
secret_message = VideoType.decode(filepath, nr_lsb_used)
case default:
raise ValueError(f"Unable to support decoding for {extension} files!")
# Decrypt the message if a valid mask is given
if len(mask):
if len(mask) != len(secret_message):
raise ValueError(f"The length of the mask({len(mask)}) doesn't match with the length of the message ({len(secret_message)})!")
decrypted_message = OneTimePad.decrypt(secret_message, mask)
else:
decrypted_message = secret_message
# Decompress the message
decompressed_message = zlib.decompress(decrypted_message)
except Exception as e:
exception = e
return decompressed_message, exception