-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnihilist.py
More file actions
98 lines (85 loc) · 3.11 KB
/
nihilist.py
File metadata and controls
98 lines (85 loc) · 3.11 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
from string import ascii_uppercase
alphabet = list(ascii_uppercase)
# Searches for a letter in a Polybius square and returns its index
def search_polybius(letter, polybius):
for r, row in enumerate(polybius):
for c, item in enumerate(row):
if (letter in item): # We use "in" because of the special case of I/J
return (r + 1)*10 + (c + 1)
return -1 # If the letter isn't found for some reason
# Prompt the user for the first key
print("Please input the first key to be used.")
key_1 = input().upper()
# Create the Polybius square
polybius = [["" for x in range(5)] for y in range(5)]
r = 0
c = 0
used_letters = []
# First, fill the square with letters from the key
for char in key_1:
if (char.isalpha() and char not in used_letters):
# Special case of I/J
if (char == "I" or char == "J"):
polybius[r][c] = "I/J"
used_letters.append("I")
used_letters.append("J")
else:
polybius[r][c] = char
used_letters.append(char)
c = (c + 1) % 5
if (c == 0):
r += 1
if (r > 4):
break
# Then, fill in the rest of the letters
if (r <= 4):
for char in alphabet:
if (char not in used_letters):
# Special case of I/J
if (char == "I" or char == "J"):
polybius[r][c] = "I/J"
used_letters.append("I")
used_letters.append("J")
else:
polybius[r][c] = char
c = (c + 1) % 5
if (c == 0):
r += 1
# Print the Polybius square in a readable format
print("The Polybius square generated by this key is:")
# Print the first line w/ column numbers
print(" " + " ".join([str(i) for i in range(1,6)]))
# Print the individual rows
for row_num, row in enumerate(polybius):
row_output = str(row_num + 1) + " "
for letter in row:
if (letter == "I/J"):
row_output += letter
else:
row_output += " {} ".format(letter)
print(row_output)
# Prompt the user for the second input
print("Please input the second key to be used. This must contain at least one letter.")
valid = False
key_2 = ""
while (not valid):
key_2 = input().upper()
key_2 = "".join([x for x in key_2 if x.isalpha()])
if (key_2 == ""):
print("This does not contain at least one letter. Please try again.")
else:
valid = True
key_2_nums = [search_polybius(c, polybius) for c in key_2]
# Prompt the user for the message to encrypt
print("Please enter the message you want to encrypt. All characters that are not letters will be dropped.")
message = input().upper()
message = "".join([x for x in message if x.isalpha()])
# Get the cipher text
cipher_nums = []
for i, char in enumerate(message):
key_num = key_2_nums[i % len(key_2_nums)]
plain_num = search_polybius(char, polybius)
cipher_nums.append(str(key_num + plain_num))
# Print the cipher text
print("The encrypted message is:")
print(" ".join(cipher_nums))