-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolybius-square-crypt.py
50 lines (40 loc) · 1.11 KB
/
polybius-square-crypt.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
#!/usr/bin/env python3
#Created by PepeBigotes
square = [['A', 'B', 'C', 'D', 'E', 'F'], ['G', 'H', 'I', 'J', 'K', 'L'], ['M', 'N', 'Ñ', 'O', 'P', 'Q'], ['R', 'S', 'T', 'U', 'V', 'W'], ['X', 'Y', 'Z']]
# ENCRYPT #
text = input("Text to encrypt (avoid special characters):\n> ").upper()
cipher = ""
for char in text:
if char == " ":
cipher += " "
continue
x = 0
y = 0
for i in square:
x += 1
y = 0
for j in i:
y += 1
if char == j:
cipher += str(x) + str(y) + " "
break
print('\n' + cipher)
# DECRYPT #
text = ""
for i, char in enumerate(cipher):
try: nextchar = cipher[i+1]
except IndexError: break
if char == " " and nextchar == " ":
text += " "
continue
if char == " ": continue
try:
x = int(char)
y = int(nextchar)
text += square[x-1][y-1]
except IndexError: pass
except ValueError: pass
for char in text:
if char == " ": print(" ", end='')
else: print(char + " ", end='')
print()