-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.py
More file actions
174 lines (152 loc) · 7.44 KB
/
encryption.py
File metadata and controls
174 lines (152 loc) · 7.44 KB
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
from enum import Enum
import random
import time
import json
import data_tables
class Algorithms(Enum):
ENCRYPT = 'encrypt'
DECRYPT = 'decrypt'
def encrypt(message: str, key: str, content:list[str], mapping_depth:int = 0) -> str:
message:str = shuffle_content(key, message, Algorithms.ENCRYPT, content)
message = algorithm(key, message, Algorithms.ENCRYPT, content)
for i in range(max(min(settings.get('random_mapping_repeat', 5), 1), 100)):
message = mapping_algorithm(message, Algorithms.ENCRYPT, provide_random_mapping_dict(key, content))
for i in range(mapping_depth):
message = mapping_algorithm(message, Algorithms.ENCRYPT, provide_most_used_mapping_dict(content, i, key, message))
return message
def decrypt(message: str, key: str, content:list[str], mapping_depth:int = 0) -> str:
for i in range(mapping_depth):
message = mapping_algorithm(message, Algorithms.DECRYPT, provide_most_used_mapping_dict(content, mapping_depth-i-1, key, message))
for i in range(max(min(settings.get('random_mapping_repeat', 5), 1), 100)):
message = mapping_algorithm(message, Algorithms.DECRYPT, provide_random_mapping_dict(key, content))
message:str = algorithm(key, message, Algorithms.DECRYPT, content)
message = shuffle_content(key, message, Algorithms.DECRYPT, content)
return message
def algorithm(key: str, message: str, mode:Algorithms, content:list[str]) -> str:
result:str = ""
for i in range(len(message)):
if message[i] in content and key[i % len(key)] in content:
char_index:int = content.index(message[i])
key_char_index:int = content.index(key[i % len(key)])
first_key_index:int = content.index(key[0])
shift:int = key_char_index * first_key_index + 1
if mode == Algorithms.ENCRYPT:
new_index:int = char_index + shift
else:
new_index:int = char_index - shift
while new_index > len(content) - 1:
new_index -= len(content)
while new_index < 0:
new_index += len(content)
new_index = new_index % len(content)
result += content[new_index]
else:
result += message[i]
return result
def shuffle_content(key: str, message:str, mode:Algorithms, content:list[str]) -> str:
reversed_key = key[::-1]
result:str = ""
for i in range(len(message)):
if message[i] in content and reversed_key[i % len(reversed_key)] in content and key[i % len(key)] in content:
char_index:int = content.index(message[i])
shift:int = content.index(key[i % len(key)]) + content.index(reversed_key[i % len(reversed_key)])
if mode == Algorithms.ENCRYPT:
new_index:int = char_index + shift
else:
new_index:int = char_index - shift
new_index = new_index % len(content)
result += content[new_index]
else:
result += message[i]
return result
def provide_random_mapping_dict(key: str, content:list[str]) -> dict:
mapping_dict:dict = {}
for i in range(len(content)):
mapping_dict[content[i]] = content[i]
temp = list(mapping_dict.values())
random.seed(algorithm(key, key, Algorithms.ENCRYPT, content))
random.shuffle(temp)
mapping_dict = dict(zip(mapping_dict, temp))
return mapping_dict
def provide_most_used_mapping_dict(content:list[str], mapping_depth:int = 0, key:str="", message:str="") -> dict:
most_used_letters_in_encryption:dict = {}
usage_percentage:float = min(max(settings.get('test_data_usage_percentage'), 1.0), 0.0)
for i in range(round(len(data_tables.testing_keys) * usage_percentage)):
for j in range(round(len(data_tables.testing_messages) * usage_percentage)):
#print(f"Encrypting message {data_tables.testing_messages[j]} with key {data_tables.testing_keys[i]}...")
encrypted_message = encrypt(data_tables.testing_messages[j], data_tables.testing_keys[i], content, mapping_depth)
# print('TEST DATA ENC: ', encrypted_message)
for char in encrypted_message:
most_used_letters_in_encryption[char] = most_used_letters_in_encryption.get(char, 0) + 1
# print(most_used_letters_in_encryption)
mapping_dict:dict = {}
if not len(most_used_letters_in_encryption) == len(data_tables.most_used_letters_english):
for char in data_tables.most_used_letters_english:
most_used_letters_in_encryption[char] = 0
if not len(most_used_letters_in_encryption) == len(data_tables.most_used_letters_english):
print("Error: The number of unique characters in the encrypted messages does not match the expected number.")
for i in range(len(data_tables.most_used_letters_english)):
mapping_dict[list(most_used_letters_in_encryption.keys())[i]] = data_tables.most_used_letters_english[i]
#print('MAPPED DICT: ', mapping_dict)
return mapping_dict
def mapping_algorithm(message: str, mode:Algorithms, mapping_dict:dict) -> str:
result:str = ""
for char in message:
if mode == Algorithms.ENCRYPT:
if char in mapping_dict.keys():
result += mapping_dict[char]
else:
result += char
elif mode == Algorithms.DECRYPT:
if char in mapping_dict.values():
result += list(mapping_dict.keys())[list(mapping_dict.values()).index(char)]
else:
result += char
return result
def main() -> None:
print('-' * 40)
print('Welcome to the Aco Ecryption/Decryption Tool V' + version)
time.sleep(1.0)
algorithm_choice:str = settings.get('algorithm_choice', 'c').strip().lower()
while algorithm_choice not in ['s', 'c']:
print("Algorithm Choice could not be defined. Please choose 's' for simple or 'c' for complex (suggested).")
algorithm_choice = input("Type 's' for simple or 'c' for complex (suggested): ").strip().lower()
if algorithm_choice == 's':
content:list[str] = data_tables.symbols_simple
else:
content:list[str] = data_tables.symbols_complex
mapping_depth:int = settings.get('mapping_depth', 1)
key:str = ""
while not key:
key = input("Enter a key: ")
message:str = ""
while not message:
message = input("Enter a message: ")
operation:str = ""
while not operation:
operation = input("Enter a operation (Type 'e' for encrypting, 'd' for decrypting or 'b' for both): ").strip().lower()
random.seed(key)
random.shuffle(content)
if operation == 'e' or operation == 'b':
encrypted:str = encrypt(message, key, content, mapping_depth)
if encrypted.endswith(' '):
encrypted += '★'
print('Encrypted:', encrypted)
if operation == 'd':
if message.endswith('★'):
message = message[:-1]
decrypted:str = decrypt(message, key, content, mapping_depth)
print('Decrypted:', decrypted)
if operation == 'b':
if encrypted.endswith('★'):
encrypted = encrypted[:-1]
decrypted:str = decrypt(encrypted, key, content, mapping_depth)
print('Decrypted:', decrypted)
if decrypted == message:
print("ACO_DEBUG: Encryption/Decryption successful!")
else:
print("ACO_DEBUG: Fail: Decryption doesn't match original message")
version:str = '1.0'
with open('algorithm_settings.json', 'r') as f:
settings = json.load(f)
main()