2025-10-26 11:40:29 -04:00
|
|
|
import random
|
2025-09-04 12:51:05 -04:00
|
|
|
import sys
|
2025-09-04 12:55:04 -04:00
|
|
|
import time
|
2025-09-04 12:51:05 -04:00
|
|
|
|
2025-10-26 13:34:05 -04:00
|
|
|
# define the full range of characters used by this program
|
|
|
|
|
alpha_base: str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,. !="
|
2025-09-04 12:51:05 -04:00
|
|
|
|
2025-10-26 13:34:05 -04:00
|
|
|
|
|
|
|
|
# MAIN
|
2025-09-04 12:51:05 -04:00
|
|
|
def main():
|
2025-10-26 11:40:29 -04:00
|
|
|
### Argument validation ###
|
2025-10-26 13:34:05 -04:00
|
|
|
if len(sys.argv) < 4:
|
|
|
|
|
print("Usage: uv run main.py \"[message]\" e|d [int]")
|
2025-10-26 11:40:29 -04:00
|
|
|
sys.exit(1)
|
2025-09-04 12:51:05 -04:00
|
|
|
|
2025-10-26 13:34:05 -04:00
|
|
|
base_max: int = len(alpha_base)
|
2025-10-26 11:40:29 -04:00
|
|
|
|
2025-10-26 13:34:05 -04:00
|
|
|
if not is_number(sys.argv[3]):
|
2025-10-26 11:40:29 -04:00
|
|
|
print("Invalid iterations value.")
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
2025-10-26 13:34:05 -04:00
|
|
|
if int(sys.argv[3]) == 0:
|
2025-10-26 11:40:29 -04:00
|
|
|
iterations = 3
|
|
|
|
|
else:
|
2025-10-26 13:34:05 -04:00
|
|
|
iterations = int(sys.argv[3])
|
2025-10-26 11:40:29 -04:00
|
|
|
|
|
|
|
|
### Processing ###
|
2025-10-26 13:34:05 -04:00
|
|
|
seed_values: list[int] = []
|
2025-10-26 12:29:37 -04:00
|
|
|
|
2025-10-26 13:34:05 -04:00
|
|
|
if sys.argv[2] == "e":
|
2025-10-26 13:03:03 -04:00
|
|
|
with open("seeds.txt", "w") as f:
|
|
|
|
|
for iteration in range(0, iterations):
|
|
|
|
|
random.seed(time.time())
|
|
|
|
|
seed_values.append(random.randint(1, base_max))
|
|
|
|
|
f.write(str(seed_values[iteration]) + "\n")
|
|
|
|
|
f.close()
|
2025-10-26 12:29:37 -04:00
|
|
|
|
2025-10-26 13:34:05 -04:00
|
|
|
if sys.argv[2] == "d":
|
2025-10-26 13:03:03 -04:00
|
|
|
with open("seeds.txt", "r") as f:
|
|
|
|
|
for iteration in range(0, iterations):
|
|
|
|
|
seed = f.readline()
|
|
|
|
|
seed_values.append(int(seed))
|
|
|
|
|
f.close()
|
2025-10-26 12:14:17 -04:00
|
|
|
|
2025-10-26 13:34:05 -04:00
|
|
|
iteration_result: str = ""
|
2025-10-26 12:14:17 -04:00
|
|
|
|
2025-10-26 13:34:05 -04:00
|
|
|
if sys.argv[2] == "e":
|
2025-10-26 13:03:03 -04:00
|
|
|
print("Encoding!")
|
2025-10-26 11:40:29 -04:00
|
|
|
for iteration in range(0, iterations):
|
2025-10-27 09:03:24 -04:00
|
|
|
iteration_result = excode(True, seed_values[iteration], sys.argv[1])
|
2025-09-04 12:51:05 -04:00
|
|
|
|
2025-10-26 13:03:03 -04:00
|
|
|
print("Result:", "\"" + iteration_result + "\"")
|
2025-10-26 13:34:05 -04:00
|
|
|
elif sys.argv[2] == "d":
|
2025-10-26 13:03:03 -04:00
|
|
|
print("Decoding!")
|
2025-10-26 12:14:17 -04:00
|
|
|
for iteration in range(0, iterations):
|
2025-10-27 09:03:24 -04:00
|
|
|
iteration_result = excode(False, seed_values[iteration], sys.argv[1])
|
2025-09-04 12:51:05 -04:00
|
|
|
|
2025-10-26 13:03:03 -04:00
|
|
|
print("Result:", "\"" + iteration_result + "\"")
|
2025-10-26 11:40:29 -04:00
|
|
|
else:
|
|
|
|
|
print("Invalid option!")
|
|
|
|
|
sys.exit(1)
|
2025-09-04 12:51:05 -04:00
|
|
|
|
|
|
|
|
|
2025-10-27 09:03:24 -04:00
|
|
|
def excode(encoding: bool, offset: int, message: str) -> str:
|
2025-10-26 13:34:05 -04:00
|
|
|
result_message: str = ""
|
2025-10-26 13:03:03 -04:00
|
|
|
|
|
|
|
|
for index_a in range(len(message)):
|
|
|
|
|
for index_b in range(len(alpha_base)):
|
|
|
|
|
if message[index_a] == alpha_base[index_b]:
|
2025-10-27 09:03:24 -04:00
|
|
|
if encoding:
|
|
|
|
|
location = index_b + offset
|
|
|
|
|
if location > (len(alpha_base) - 1):
|
|
|
|
|
location = location - (len(alpha_base) - 1)
|
|
|
|
|
else:
|
|
|
|
|
location = index_b - offset
|
|
|
|
|
if location < 0:
|
|
|
|
|
location = location + (len(alpha_base) - 1)
|
2025-10-26 13:03:03 -04:00
|
|
|
result_message = result_message + alpha_base[location]
|
2025-09-04 12:51:05 -04:00
|
|
|
break
|
|
|
|
|
|
2025-10-26 13:03:03 -04:00
|
|
|
return result_message
|
2025-09-04 12:51:05 -04:00
|
|
|
|
|
|
|
|
|
2025-10-26 13:34:05 -04:00
|
|
|
def is_number(i) -> bool:
|
2025-10-26 11:40:29 -04:00
|
|
|
try:
|
|
|
|
|
int(i)
|
|
|
|
|
return True
|
|
|
|
|
except ValueError:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2025-09-04 12:51:05 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|