-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayfairTable.py
More file actions
66 lines (55 loc) · 1.82 KB
/
PlayfairTable.py
File metadata and controls
66 lines (55 loc) · 1.82 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
'''
@author William Ray Johnson
9/3/2017
'''
from collections import OrderedDict
class PlayfairTable:
alphabet = "abcdefghiklmnopqrstuvwxyz"
def __init__(self, keyword):
self.keyword = keyword.lower().replace(" ", "").replace("j", "i")
self.createTable()
'''
@author William Ray Johnson
Creates a table for a playfair cipher
'''
def createTable(self):
tableValues = "".join(OrderedDict.fromkeys(self.keyword + self.alphabet))
table = [['-','-','-','-','-'],
['-','-','-','-','-'],
['-','-','-','-','-'],
['-','-','-','-','-'],
['-','-','-','-','-']]
for row in range(0,5):
for column in range(0,5):
table[row][column] = tableValues[0]
tableValues = tableValues[1:]
self.table = table
def getTable(self):
return self.table
def getIndex(self, value):
localValue = value
if localValue == "j":
localValue = "i"
valueFound = False
index = None
row = 0
column = 0
while not(valueFound) and row <= 4:
while not(valueFound) and column <= 4:
if self.table[row][column] == localValue:
valueFound = True
else:
column += 1
if not(valueFound):
column = 0
row += 1
if valueFound:
return (row, column)
else:
raise ValueError('Value not found in table')
def getValue(self, index):
if index[0] == 5:
index = (0, index[1])
if index[1] == 5:
index = (index[0], 0)
return self.table[index[0]][index[1]]