-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayfair_cipher_decrypt.py
99 lines (78 loc) · 2.03 KB
/
playfair_cipher_decrypt.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
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
# source : https://www.geeksforgeeks.org/playfair-cipher-with-examples/
def toLowerCase(plain):
# Convert all the characters of a string to lowercase
return plain.lower()
def removeSpaces(plain):
# Remove all spaces in a string
# can be extended to remove punctuation
return ''.join(plain.split())
def generateKeyTable(key):
# generates the 5x5 key square
keyT = [['' for i in range(5)] for j in range(5)]
dicty = {chr(i + 97): 0 for i in range(26)}
for i in range(len(key)):
if key[i] != 'j':
dicty[key[i]] = 2
dicty['j'] = 1
i, j, k = 0, 0, 0
while k < len(key):
if dicty[key[k]] == 2:
dicty[key[k]] -= 1
keyT[i][j] = key[k]
j += 1
if j == 5:
i += 1
j = 0
k += 1
for k in dicty.keys():
if dicty[k] == 0:
keyT[i][j] = k
j += 1
if j == 5:
i += 1
j = 0
return keyT
def search(keyT, a, b):
# Search for the characters of a digraph in the key square and return their position
arr = [0, 0, 0, 0]
if a == 'j':
a = 'i'
elif b == 'j':
b = 'i'
for i in range(5):
for j in range(5):
if keyT[i][j] == a:
arr[0], arr[1] = i, j
elif keyT[i][j] == b:
arr[2], arr[3] = i, j
return arr
def mod5(a):
# Function to find the modulus with 5
if a < 0:
a += 5
return a % 5
def decrypt(str, keyT):
# Function to decrypt
ps = len(str)
i = 0
while i < ps:
a = search(keyT, str[i], str[i+1])
if a[0] == a[2]:
str = str[:i] + keyT[a[0]
][mod5(a[1]-1)] + keyT[a[0]][mod5(a[3]-1)] + str[i+2:]
elif a[1] == a[3]:
str = str[:i] + keyT[mod5(a[0]-1)][a[1]] + \
keyT[mod5(a[2]-1)][a[1]] + str[i+2:]
else:
str = str[:i] + keyT[a[0]][a[3]] + keyT[a[2]][a[1]] + str[i+2:]
i += 2
return str
def decryptByPlayfairCipher(str, key):
# Function to call decrypt
ks = len(key)
key = removeSpaces(toLowerCase(key))
str = removeSpaces(toLowerCase(str))
keyT = generateKeyTable(key)
return decrypt(str, keyT)
def final(key, text):
return decryptByPlayfairCipher(text, key)