-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
273 lines (217 loc) · 9.97 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""
`python main.py -h` for help message
Uses python random module and XOR operation to encrypt/decrypt
The script is modular and each function can be used independently
"""
from argparse import ArgumentParser
from random import Random
defaults = {
"seed": 0,
"size_multiplier": 2,
"char_range": 128,
"garbage_seed": None,
"hex": False, # When hex is True, encrypted message is outputted longer than multiplier because multiple hex characters correspond to a single character
}
help_messages = {
"arg": "The text to be encrypted, or the path of the file if -f flag is set.",
"file": "[default:False] Indicate that the given argument is a file path insead of string.",
"decrypt": "[default:False] Indicate that the message is to be decrypted, not encrypted.",
"output": "[default:stdout] Specify a text file where the output code/values will be saved.",
"hex": "[default:False] Indicates that the encrypted message is to be hex encoded. (or the message is to be decoded from hex, if -d is set)",
"seed": "[default:random] The seed for encrypting/decrypting. Passed to random library as seed.",
"size": "[default:2] Approximately equal to len(encrypted) / len(original).",
"char range": "[default:128] Unicode character range of the encrypted message (Does not limit the original message).",
"garbage seed": "[default:random] Seed for garbage values which randomize encrypted text.",
"values only": "[default:False] Give key values and encrypted message only, not the entire code (Ignored if -d flag is set).",
}
def _load_defaults(params: dict, *args: str) -> None:
"""\
Load default values of given args into params
Args:
params (dict): The dictionary to load defaults into
args (str): The arguments to load defaults of
Returns:
None: makes changes in params itself
"""
for arg in args:
params[arg] = params.get(arg, defaults[arg])
def _validate(params: dict) -> None:
"""
Validate the values passed by the user (as params)
Args:
seed (Any): If value is None, the system fails
size_multiplier (int): Min value is 1
char_range (int): Min value is 1
Returns:
None: makes changes in params itself
"""
args = ("seed", "size_multiplier", "char_range")
_load_defaults(params, *args)
if params["seed"] is None:
params["seed"] = defaults["seed"]
if params["size_multiplier"] < 1:
params["size_multiplier"] = defaults["size_multiplier"]
if params["char_range"] < 1:
params["char_range"] = defaults["char_range"]
def encrypt(message: str, **params) -> str:
"""\
Encrypts the given message based on given parameters
Args:
message (str): The string to encrypt
seed (Any): Main key for encryption, passed to random library as seed
size_multiplier (int): Factor of how large the encrypted message should be
char_range (int): Unicode character range of the encrypted message, does not limit the input message
garbage_seed (Any): Seed for randomizing the output, to replicate an output, use the same value
hex (bool): If the output should be hex encoded
Returns:
str: The encrypted message
NOTE: Ensure to use the same 'seed', 'size_multiplier', and 'char_range' values as used for decoding
"""
args = ("seed", "size_multiplier", "char_range", "garbage_seed", "hex")
_load_defaults(params, *args)
size_multiplier = params["size_multiplier"]
char_range = params["char_range"]
temp = Random(params["garbage_seed"])
determined = Random(params["seed"])
result = ""
i = 0
n = len(message)
while i < n:
char = message[i]
if not determined.randrange(size_multiplier):
i += 1
result += chr(determined.randrange(char_range) ^ ord(char))
else:
result += chr(temp.randrange(char_range))
if params.get("hex"):
result = result.encode().hex().upper()
return result
def decrypt(encrypted_message: str, **params) -> str:
"""\
Decrypts the encrypted message based on the given parameters
Args:
encrypted_message (str): The encrypted message to be decrypted
seed (Any): Main key for decryption, passed to random library as seed
size_multiplier (int): Factor of how small the original message is
char_range (int): Unicode character range of the encrypted message
Returns:
str: The decrypted message
NOTE: Ensure to use the same 'seed', 'size_multiplier', and 'char_range' values as used while encoding.
"""
args = ("seed", "size_multiplier", "char_range")
_load_defaults(params, *args)
size_multiplier = params["size_multiplier"]
char_range = params["char_range"]
determined = Random(params["seed"])
message = ""
for char in encrypted_message:
if not determined.randrange(size_multiplier):
message += chr(determined.randrange(char_range) ^ char)
return message
def give_code(encrypted_message: str, **params) -> str:
"""\
Generates Python code to decrypt the message based on given parameters
Args:
encrypted_message (str): The encrypted message
seed (Any): Main key for decryption, passed to random library as seed
size_multiplier (int): Factor of how small the original message is
char_range (int): Unicode character range of the encrypted message
hex (bool): If the encrypted message is hex encoded
Returns:
str: 2 lines of Python code for decoding the message
NOTE: Ensure to use the same 'seed', 'size_multiplier', and 'char_range' values as used while encoding.
"""
args = ("seed", "size_multiplier", "char_range", "hex")
_load_defaults(params, *args)
m = "bytes.fromhex(m).decode()" if params["hex"] else "m"
return f"""from random import randrange, seed; seed({params["seed"]!r}); m = {encrypted_message!r}
print(''.join((chr(randrange({params.get("char_range")})^ord(c))for c in {m} if not randrange({params.get("size_multiplier")}))),end='')"""
def give_values(encrypted_message: str, **params) -> str:
"""\
Generates a formatted string containing encrypted message and key values for decryption
Args:
encrypted_message (str): The encrypted message
seed (Any): Main key for decryption, passed to random library as seed
size_multiplier (int): Factor of how small the original message is
char_range (int): Unicode character range of the encrypted message
hex (bool): Indicates weather the output message is hex encoded
Returns:
str: The formatted string containing key values
NOTE: Ensure to use the same 'seed', 'size_multiplier', and 'char_range' values as used while encoding.
"""
args = ("seed", "size_multiplier", "char_range", "hex")
_load_defaults(params, *args)
return f"message:\t{encrypted_message!r}\nseed:\t\t{params['seed']!r}\nsize seed:\t{params['size_multiplier']}\nchar range:\t{params['char_range']}\nhex encoded:\t{params['hex']}"
def get_input() -> dict:
"""\
Set up argument parser and return it
Returns:
dict: The parsed arguments in a dictionary form
"""
parser = ArgumentParser("Pseudo-Random Encryptor")
parser.add_argument("arg", type=str, help=help_messages["arg"])
parser.add_argument("--hex", "-x", action="store_true", default=False, help=help_messages["hex"])
parser.add_argument("--file", "-f", action="store_true", default=False, help=help_messages["file"])
parser.add_argument("--decrypt", "-d", action="store_true", default=False, help=help_messages["decrypt"])
parser.add_argument("--values-only", "-vo", action="store_true", default=False, help=help_messages["values only"])
parser.add_argument("--seed", "-s", default=f"{Random().random()}"[2:], help=help_messages["seed"])
parser.add_argument("--size", "-z", default=2, type=int, help=help_messages["size"])
parser.add_argument("--char-range", "-r", type=int, default="128", help=help_messages["char range"])
parser.add_argument("--output", "-o", type=str, default="", help=help_messages["output"])
parser.add_argument("--garbage", "-g", default=None, help=help_messages["garbage seed"])
args = parser.parse_args()
params = {k:v for k, v in args._get_kwargs()}
params["size_multiplier"] = args.size
del params["size"]
return params
def _extract_message(params: dict) -> str | int:
"""\
Extracts the original message to be encrypted/decrypted.
Reads the file if specified.
Args:
params (dict): a dictionary of arguments
Returns:
str|int: message (str) if it exists, otherwise -1 (int)
"""
if params["file"]:
try:
with open(params["arg"], "r") as f:
message = f.read()
except FileNotFoundError:
print("File does not exist")
return -1
else:
message = params["arg"]
return message if message else -1
def run(params: dict) -> None:
"""\
Main function
Args:
params (dict): all the arguments passed to the program
Returns:
None: Prints or writes output to the file
"""
message = _extract_message(params)
del params["arg"]
if message == -1: # file does not exist or empty file
print("No message to encrypt")
return
_validate(params)
if params["decrypt"]:
message = message.encode().decode("unicode-escape").encode()
if params["hex"]:
message = bytes.fromhex(message.decode())
output = decrypt(message, **params)
else:
params["encrypted_message"] = encrypt(message, **params)
output = give_values(**params) if params["values_only"] else give_code(**params)
if params["output"]:
try:
with open(params["output"], "w") as f:
f.write(output)
except Exception as err:
print(f"Error occured while writing to file: {err}")
else:
print(output)
if __name__ == "__main__":
run(get_input())