-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayfair.py
More file actions
122 lines (90 loc) · 3.34 KB
/
Copy pathplayfair.py
File metadata and controls
122 lines (90 loc) · 3.34 KB
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
from pprint import pprint
def matrix(key):
matrix = []
# add unique key elements
for e in key:
if e.upper() not in matrix:
matrix.append(e.upper())
letters = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
# add remaining alphabets
for e in letters:
if e.upper() not in matrix:
matrix.append(e.upper())
# split 25 elements into 5x5 matrix
return [matrix[i:i + 5] for i in range(0, 25, 5)]
def messages(user_message):
# convert string to list with all capital letters
message = list(map(str.upper, user_message))
# remove spaces
message = list(filter(lambda x: x != ' ', message))
# add 'X' between duplicates if first one is at odd index
for i in range(len(message)):
if message[i-1] == message[i] and i % 2:
message.insert(i, 'X')
# append 'X' if list has odd number of elements to make valid pairs
if len(message) % 2 == 1:
message.append('X')
# make pairs from even number of elements
return [message[i:i+2] for i in range(0, len(message), 2)]
def position(matrixk, letter):
for i in range(5):
for j in range(5):
if matrixk[i][j] == letter:
return i, j
def encrypt(user_message, key):
message = messages(user_message)
matrixk = matrix(key)
cipher = []
# get indices of pairs
for a, b in message:
x1, y1 = position(matrixk, a)
x2, y2 = position(matrixk, b)
# Elements in same row
if x1 == x2:
# if y reached one end (4), %5 will make (4+1)%5 = 0
cipher.append(matrixk[x1][(y1 + 1) % 5])
cipher.append(matrixk[x1][(y2 + 1) % 5])
# Elements in same column
elif y1 == y2:
# if x reached one end (4), %5 will make (4+1)%5 = 0
cipher.append(matrixk[(x1 + 1) % 5][y1])
cipher.append(matrixk[(x2 + 1) % 5][y2])
# Elements make a box so use remaining corners
else:
cipher.append(matrixk[x1][y2])
cipher.append(matrixk[x2][y1])
# convert list to string
return ''.join(cipher)
def decrypt(user_cipher, key):
# generate pairs from cipher
cipher = [user_cipher[i:i+2] for i in range(0, len(user_cipher), 2)]
matrixk = matrix(key)
plaintext = []
for a, b in cipher:
x1, y1 = position(matrixk, a)
x2, y2 = position(matrixk, b)
# Elements in same row
if x1 == x2:
# if y reached one end (0), %5 will make (0-1)%5 = 4
plaintext.append(matrixk[x1][(y1 - 1) % 5])
plaintext.append(matrixk[x1][(y2 - 1) % 5])
# Elements in same column
elif y1 == y2:
# if x reached one end (0), %5 will make (0-1)%5 = 4
plaintext.append(matrixk[(x1 - 1) % 5][y1])
plaintext.append(matrixk[(x2 - 1) % 5][y2])
# Elements make a box so use remaining corners
else:
plaintext.append(matrixk[x1][y2])
plaintext.append(matrixk[x2][y1])
# convert list to string
return ''.join(plaintext)
key = input('Please input the key: ')
plaintext = input('Please input the message: ')
cipher = encrypt(plaintext, key)
decipher = decrypt(cipher, key)
print("Plaintext Matrix: ", messages(plaintext))
print("Key Matrix:")
pprint(matrix(key))
print("Encrypted text: ", cipher)
print("Decrypted text: ", decipher)