-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (50 loc) · 1.71 KB
/
main.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
62
63
64
65
66
67
import math
from typing import MutableSet
def createTable(alphabet):
alphabetSize = len(alphabet)
rowSize = int(math.sqrt(nearestPerfectSquare(alphabetSize)))
table = ()
numberOfRows = alphabetSize // rowSize
if alphabetSize % rowSize != 0:
numberOfRows += 1
for i in range(numberOfRows):
table += (alphabet[i*rowSize : min((i+1)*rowSize, alphabetSize)],)
return table
def nearestPerfectSquare(number):
ceiling = number
floor = number
while math.sqrt(ceiling) % 1 != 0:
ceiling += 1
while math.sqrt(floor) % 1 != 0:
floor -= 1
if abs(ceiling - number) > abs(floor - number):
return floor
return ceiling
def getCoordinates(character, table):
for ln in range(len(table)):
for cl in range(len(table[ln])):
if character == table[ln][cl]:
return str(ln) + str(cl)
return "XX"
def encrypt(message, table):
encrypted = ""
for character in message:
encrypted += getCoordinates(character, table)
return encrypted
def getCharacter(coordinates, table):
if coordinates == "XX":
return "?"
return table[eval(coordinates[0])][eval(coordinates[1])]
def decrypt(encrypted, table):
decrypted = ""
for i in range(0, len(encrypted), 2):
decrypted += getCharacter(encrypted[i:i+2], table)
return decrypted
alphabet = ('A','B','C','D','E','F','G','H','I','J',' ','L','M','N','O','P','Q','R','S','T','U','V','X','Z','.')
table = createTable(alphabet)
message = "NOT A LOT GOING ON AT THE MOMENT"
print(f"message: {message}")
encrypted = encrypt(message, table)
print(f"encrypted: {encrypted}")
decrypted = decrypt(encrypted, table)
print(f"decrypted: {decrypted}")