-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
61 lines (45 loc) · 2.07 KB
/
app.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
from cryptography import SubstitutionCipher
from cryptography import TranspositionCiper
from collections import Counter
def execute_substitution_cipher():
print("Substitution Cipher...")
text = input("Input the text: ")
k = input("Input the dislocation k (integer): ")
while not k.isdigit():
k = input("Invalid input. Input the dislocation k (integer): ")
operation = input("Do you want to encode (e) or decode (d)? ")
while (operation != "e") and (operation != "d"):
operation = input("Invalid input. Input e or d:")
cipher = SubstitutionCipher(int(k))
if operation == "d":
result = cipher.decode(text)
else:
result = cipher.encode(text)
print("The result is: " + result)
def execute_transposition_cipher():
print("Transposition Cipher...")
text = input("Input the text: ")
key = input("The key must have no repeating characters and it's lenght should be divisible by the text lenght.\nInput the key: ")
repeating_char_list = [i for i,j in Counter(key).items() if j>1]
while repeating_char_list or (len(text) % len(key) != 0):
key = input("Invalid key. Input the key: ")
repeating_char_list = [i for i,j in Counter(key).items() if j>1]
operation = input("Do you want to encode (e) or decode (d)? ")
while (operation != "e") and (operation != "d"):
operation = input("Invalid input. Input e or d:")
cipher = TranspositionCiper(key)
if operation == "d":
result = cipher.decode(text)
else:
result = cipher.encode(text)
print("The result is: " + result)
def main():
choice = input("Select your cipher:\n1 - Substitution Cipher\n2 - Transposition Cipher\n")
while (choice != "1") and (choice != "2"):
choice = input("Select your cipher:\n1 - Substitution Cipher\n2 - Transposition Cipher\n")
if choice == "1":
execute_substitution_cipher()
elif choice == "2":
execute_transposition_cipher()
if __name__ == "__main__":
main()