-
Notifications
You must be signed in to change notification settings - Fork 0
/
playfair.py
73 lines (58 loc) · 1.37 KB
/
playfair.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
# implement playfair cipher
letters = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z']
def get_key():
key = raw_input("Enter the text : ").upper()
check_repeated_alpha(key)
def check_repeated_alpha(key):
newWord = ""
for sym in key:
if sym.isalpha() and sym not in newWord:
newWord += sym
gen_matrix(newWord)
def gen_matrix(newWord):
matrix = []
for char in newWord:
if char in letters and char not in matrix:
matrix.append(char)
for char in letters:
if char not in matrix :
matrix.append(char)
for char in matrix:
if char is "I" or char is "J":
if char not in newWord:
matrix.remove(char)
print matrix
def get_message():
message = []
message = raw_input("Enter message : ")
#print "\n" + message
sub = []
pair = []
space_li = ""
for char in message:
if ord(char) >= 97 and ord(char) <= 122:
space_li += char
#print space_li
if len(space_li) % 2 == 0:
pair = space_li[:2]
new = space_li[2:]
sub.append(pair)
while(len(new) != 0):
pair = new[:2]
new = new[2:]
sub.append(pair)
print "\n" + str(sub)
else:
space_li += 'x'
print space_li
pair = space_li[:2]
new = space_li[2:]
sub.append(pair)
while(len(new) != 0):
pair = new[:2]
new = new[2:]
sub.append(pair)
print "\n" + str(sub)
get_key()
get_message()