-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.txt
106 lines (96 loc) · 4.28 KB
/
test.txt
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
import re
class Playfair():
"""The Playfair Cipher enciphers pairs of characters, the key consists of
a keysquare 25 characters in length.
More information about the algorithm can be
found at http://www.practicalcryptography.com/ciphers/playfair-cipher/.
:param key: The keysquare, as a 25 character string.
:param newkey: The key to make an unique keysquare
"""
def __init__(self, newkey, key='ABCDEFGHIKLMNOPQRSTUVWXYZ'):
newkey = newkey.replace(' ', '').upper().replace('J', 'I')
newkey = ''.join([newkey, key])
self.newkey = (sorted(set(newkey), key=newkey.index))
def remove_punctuation(self, text, filter='[^A-Z]'):
return re.sub(filter, '', text.upper())
def encipher_pair(self, a, b):
if a == b:
b = 'X'
arow, acol = int(self.newkey.index(a) / 5), self.newkey.index(a) % 5
brow, bcol = int(self.newkey.index(b) / 5), self.newkey.index(b) % 5
if arow == brow:
return self.newkey[arow * 5 +
(acol + 1) % 5] + self.newkey[brow * 5 +
(bcol + 1) % 5]
elif acol == bcol:
return self.newkey[((arow + 1) % 5) * 5 +
acol] + self.newkey[((brow + 1) % 5) * 5 + bcol]
else:
return self.newkey[arow * 5 + bcol] + self.newkey[brow * 5 + acol]
def decipher_pair(self, a, b):
assert a != b, ('two of the same letters occurred together, '
'illegal in playfair')
arow, acol = int(self.newkey.index(a) / 5), self.newkey.index(a) % 5
brow, bcol = int(self.newkey.index(b) / 5), self.newkey.index(b) % 5
if arow == brow:
return self.newkey[arow * 5 +
(acol - 1) % 5] + self.newkey[brow * 5 +
(bcol - 1) % 5]
elif acol == bcol:
return self.newkey[((arow - 1) % 5) * 5 +
acol] + self.newkey[((brow - 1) % 5) * 5 + bcol]
else:
return self.newkey[arow * 5 + bcol] + self.newkey[brow * 5 + acol]
def encipher(self, string):
"""Encipher string using Playfair cipher according to initialised key.
Punctuation and whitespace
are removed from the input. If the input plaintext is not an even
number of characters, an 'X' will be appended.
:param string: The string to encipher.
:returns: The enciphered string.
"""
string = self.remove_punctuation(string)
string = re.sub(r'[J]', 'I', string)
if len(string) % 2 == 1:
string += 'X'
ret = ''
for c in range(0, len(string), 2):
ret += self.encipher_pair(string[c], string[c + 1])
return ret
def decipher(self, string):
"""Decipher string using Playfair cipher according to initialised key.
Punctuation and whitespace
are removed from the input. The ciphertext should be an even number of
characters. If the input ciphertext is not an even number of
characters, an 'X' will be appended.
:param string: The string to decipher.
:returns: The deciphered string.
"""
string = self.remove_punctuation(string)
if len(string) % 2 == 1:
string += 'X'
ret = ''
for c in range(0, len(string), 2):
ret += self.decipher_pair(string[c], string[c + 1])
return ret
if __name__ == '__main__':
while True:
print("Would you like to encode or decode")
print("press [E] to encode, [D] to decode")
inputselect = input("Pick an option between E and D:")
if inputselect.lower() not in ('e', 'd'):
print("Not an appropriate choice.")
else:
break
if inputselect == 'e':
print("")
keyword = input('Enter a keyword: ')
message = input('Enter string to encode: ')
playfair_object = Playfair(keyword)
print(playfair_object.encipher(message))
if inputselect == 'd':
print("")
keyword = input('Enter a keyword: ')
message = input('Enter string to decode: ')
playfair_object = Playfair(keyword)
print(playfair_object.decipher(message))