-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA_02_d_PlayfairCipher.py
180 lines (139 loc) · 4.26 KB
/
A_02_d_PlayfairCipher.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Author: Devashish Roy, Roll No: 2024PGCSIS08
# Assignment No: 02
# Implementation of Classical Substitution Ciphers
# a) Additive (Ceasar) Cipher
# b) Multiplicative Cipher
# c) Affine Cipher
# d) Playfair Cipher
# e) Hill Cipher
# Playfair Cipher
def generateKeySquare(key):
size = 5
keySquare = [[0] * size for i in range(size)]
alphaSet = set(())
row = 0
col = 0
for alpha in key:
if(alpha not in alphaSet):
if(alpha == 'J'):
continue
alphaSet.add(alpha)
# Add to the matrix
if(col >= size):
row += 1
col = 0
keySquare[row][col] = alpha
col += 1
for i in range(65, 91):
if(chr(i) not in alphaSet):
if(i == 74):
continue
alphaSet.add(chr(i))
if(col >= size):
row += 1
col = 0
keySquare[row][col] = chr(i)
col += 1
return keySquare
def generateDigraph(sampletext):
digraphArray = []
i = 0
while i < len(sampletext):
if(i+1 >= len(sampletext)):
digraphArray.append([sampletext[i], 'z'])
i += 1
elif(sampletext[i] == sampletext[i+1]):
digraphArray.append([sampletext[i], 'x'])
i += 1
else:
digraphArray.append([sampletext[i], sampletext[i+1]])
i += 2
return digraphArray
def search(keySquare, element):
for i in range(5):
for j in range(5):
if(keySquare[i][j] == element.upper()):
return i, j
def encryptionMethod(digraph, keySquare):
row_1, col_1 = search(keySquare, digraph[0])
row_2, col_2 = search(keySquare, digraph[1])
if(col_1 == col_2):
row_1 += 1
row_2 += 1
if(row_1 == 5):
row_1 = 0
if(row_2 == 5):
row_2 = 0
elif(row_1 == row_2):
col_1 += 1
col_2 += 1
if(col_1 == 5):
col_1 = 0
if(col_2 == 5):
col_2 = 0
else:
temp = col_1
col_1 = col_2
col_2 = temp
return keySquare[row_1][col_1], keySquare[row_2][col_2]
def decryptionMethod(digraph, keySquare):
row_1, col_1 = search(keySquare, digraph[0])
row_2, col_2 = search(keySquare, digraph[1])
if(col_1 == col_2):
row_1 -= 1
row_2 -= 1
if(row_1 == -1):
row_1 = 4
if(row_2 == -1):
row_2 = 4
elif(row_1 == row_2):
col_1 -= 1
col_2 -= 1
if(col_1 == -1):
col_1 = 4
if(col_2 == -1):
col_2 = 4
else:
temp = col_1
col_1 = col_2
col_2 = temp
return keySquare[row_1][col_1], keySquare[row_2][col_2]
# def encryption(plaintext, key):
# ciphertext = ""
# keySquare = generateKeySquare(key)
# digraphArray = generateDigraph(plaintext)
# for eachDigraph in digraphArray:
# x, y = encryptionMethod(eachDigraph, keySquare)
# ciphertext = ciphertext + x + y
# return ciphertext
# def decryption(ciphertext, key):
# plaintext = ""
# keySquare = generateKeySquare(key)
# digraphArray = generateDigraph(ciphertext)
# for eachDigraph in digraphArray:
# x, y = decryptionMethod(eachDigraph, keySquare)
# plaintext = plaintext + x + y
# return plaintext.lower()
def encryptNdecrypt(inputtext, key, method):
outputtext = ""
keySquare = generateKeySquare(key)
digraphArray = generateDigraph(inputtext)
for eachDigraph in digraphArray:
if(method == "encryption"):
x, y = encryptionMethod(eachDigraph, keySquare)
elif(method == "decryption"):
x, y = decryptionMethod(eachDigraph, keySquare)
outputtext = outputtext + x + y
if(method == "encryption"):
return outputtext.upper()
elif(method == "decryption"):
return outputtext.lower()
def main():
sampletext = "instruments"
key = "MONARCHY"
ciphertext = encryptNdecrypt(sampletext, key, "encryption")
print(ciphertext)
plaintext = encryptNdecrypt(ciphertext, key, "decryption")
print(plaintext)
if __name__ == "__main__":
main()