-
Notifications
You must be signed in to change notification settings - Fork 22
/
playfair_encryption.py
178 lines (150 loc) · 5.65 KB
/
playfair_encryption.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
import string
def key_generation(key):
# initializing all and generating key_matrix
main=string.ascii_lowercase.replace('j','.')
# convert all alphabets to lower
key=key.lower()
key_matrix=['' for i in range(5)]
# if we have spaces in key, those are ignored automatically
i=0;j=0
for c in key:
if c in main:
# putting into matrix
key_matrix[i]+=c
# to make sure repeated characters in key
# doesnt include in the key_matrix, we replace the
# alphabet into . in the main, whenever comes in iteration
main=main.replace(c,'.')
# counting column change
j+=1
# if column count exceeds 5
if(j>4):
# row count is increased
i+=1
# column count is set again to zero
j=0
# to place other alphabets in the key_matrix
# the i and j values returned from the previous loop
# are again used in this loop, continuing the values in them
for c in main:
if c!='.':
key_matrix[i]+=c
j+=1
if j>4:
i+=1
j=0
return(key_matrix)
# Now plaintext is to be converted into cipher text
def conversion(plain_text):
# seggrigating the maeesage into pairs
plain_text_pairs=[]
# replacing repeated characters in pair with other letter, x
cipher_text_pairs=[]
# remove spaces
plain_text=plain_text.replace(" ","")
# convert to lower case
plain_text=plain_text.lower()
# RULE1: if both letters in the pair are same or one letter is left at last,
# replace second letter with x or add x, else continue with normal pairing
i=0
# let plain_text be abhi
while i<len(plain_text):
# i=0,1,2,3
a=plain_text[i]
b=''
if((i+1)==len(plain_text)):
# if the chosen letter is last and doesnt have pair
# then the pai will be x
b='x'
else:
# else the next letter will be pair with the previous letter
b=plain_text[i+1]
if(a!=b):
plain_text_pairs.append(a+b)
# if not equal then leave the next letter,
# as it became pair with previous alphabet
i+=2
else:
plain_text_pairs.append(a+'x')
# else dont leave the next letter and put x
# in place of repeated letter and conitnue with the next letter
# which is repeated (according to algo)
i+=1
print("plain text pairs: ",plain_text_pairs)
for pair in plain_text_pairs:
# RULE2: if the letters are in the same row, replace them with
# letters to their immediate right respectively
flag=False
for row in key_matrix:
if(pair[0] in row and pair[1] in row):
# find will return index of a letter in string
j0=row.find(pair[0])
j1=row.find(pair[1])
cipher_text_pair=row[(j0+1)%5]+row[(j1+1)%5]
cipher_text_pairs.append(cipher_text_pair)
flag=True
if flag:
continue
# RULE3: if the letters are in the same column, replace them with
# letters to their immediate below respectively
for j in range(5):
col="".join([key_matrix[i][j] for i in range(5)])
if(pair[0] in col and pair[1] in col):
# find will return index of a letter in string
i0=col.find(pair[0])
i1=col.find(pair[1])
cipher_text_pair=col[(i0+1)%5]+col[(i1+1)%5]
cipher_text_pairs.append(cipher_text_pair)
flag=True
if flag:
continue
#RULE:4 if letters are not on the same row or column,
# replace with the letters on the same row respectively but
# at the other pair of corners of rectangle,
# which is defined by the original pair
i0=0
i1=0
j0=0
j1=0
for i in range(5):
row=key_matrix[i]
if(pair[0] in row):
i0=i
j0=row.find(pair[0])
if(pair[1] in row):
i1=i
j1=row.find(pair[1])
cipher_text_pair=key_matrix[i0][j1]+key_matrix[i1][j0]
cipher_text_pairs.append(cipher_text_pair)
print("cipher text pairs: ",cipher_text_pairs)
# final statements
print('plain text: ',plain_text)
print('cipher text: ',"".join(cipher_text_pairs))
key=input("Enter the key: ")
# calling first function
key_matrix=key_generation(key)
print("Key Matrix for encryption:")
print(key_matrix)
plain_text=input("Enter the message: ")
# calling second function
conversion(plain_text)
'''
----------OUTPUT----------
Enter the key: Make my day beautiful
Key Matrix for encryption:
['makey', 'dbuti', 'flcgh', 'nopqr', 'svwxz']
Enter the message: hi there my name is abhiram
plain text pairs: ['hi', 'th', 'er', 'em', 'yn', 'am', 'ei', 'sa', 'bh', 'ir', 'am']
cipher text pairs: ['rh', 'ig', 'yq', 'ya', 'mr', 'ka', 'yt', 'vm', 'il', 'hz', 'ka']
plain text: hitheremynameisabhiram
cipher text: rhigyqyamrkaytvmilhzka
>>>
'''
'''
Took help from:
1. https://www.youtube.com/watch?v=U_J2xnhblPg
2. https://www.youtube.com/watch?v=O8MxWNfrzho&t=4s
3. https://www.youtube.com/watch?v=66K1tplwYqg&t=5s
4. https://www.youtube.com/watch?v=2PUInSjhxNs
Thanks for the help !!!
'''