-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayfair_cipher.py
More file actions
203 lines (184 loc) · 6.4 KB
/
playfair_cipher.py
File metadata and controls
203 lines (184 loc) · 6.4 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/python
# -*- coding: utf-8 -*-
#############################################################################
### ###
### Description: PlayFair Cipher Algorithm, takes a 25 character (5x5) ###
### key that is generated with a phrase. Then encrypts ###
### / decrypts the message using the rules and the key ###
#############################################################################
__author__ = "Tyler Hall"
__copyright__ = "Copyright 2017"
###################
# IMPORTS #
###################
###################
# GLOBAL #
###################
ALPHABET = 'ABCDEFGHIKLMNOPQRSTUVWXYZ'
"""
Please note there is no J as it is translated as an I
"""
###################
# FUNCTIONS #
###################
# Method : Generates the table from the key phrase
# Param : key: string (phrase) used to create table
# Returns : Array table
def gen_key(key):
# Variables
x = 0
y = 0
cipher_table = {}
# Use key
for char in key.upper():
if char == 'J':
char = 'I'
if char not in cipher_table.keys():
cipher_table[char] = (x,y)
if y == 4:
y = 0
x += 1
else:
y += 1
#Fill in the rest of the table
if x < 5:
for char in ALPHABET:
if char not in cipher_table.keys():
cipher_table[char] = (x,y)
if y == 4:
y = 0
x += 1
else:
y += 1
if x == 5:
break
return cipher_table
# Method : Encryption Method
# Param : message: plaintext
# dicKey: Key used to encrypt/decrypt the text
# Returns : returns cipherText
def encrypt(message, dictKey):
cipherText = ''
diagramList = breakMessage(message)
print "diagramList : ", diagramList
for coupling in diagramList:
cipherText += encrypt_rules(coupling,dictKey)
return cipherText
# Method : Decryption Method
# Param : cipherText: encrypted message
# dicKey: Key used to encrypt/decrypt the text
# Returns : returns plain text
def decrypt(cipherText, dictKey):
plainText = ''
diagramList = breakMessage(cipherText)
for coupling in diagramList:
plainText += decrypt_rules(coupling,dictKey)
return plainText
# Method : Breaks the plain text into diagrams (couples of two)
# Param : plainText: OG message
# Returns : returns sets of the grouped characters
def breakMessage(plainText):
diagram = []
plainText = ''.join(plainText.split())
plainText = plainText.upper()
for x in range(0,len(plainText),2):
if (x+1) != len(plainText):
if (plainText[x]!=plainText[x+1]):
diagram.append(plainText[x]+plainText[x+1])
else:
diagram.append(plainText[x]+"X")
else:
diagram.append(plainText[x]+"X")
return diagram
# Method : Rules for encrypting the coupling of the characters
# Param : coupling: Two characters that where put together
# dicKey: Key used to encrypt/decrypt the text
# Returns : returns sets of the encrypted coupling
def encrypt_rules(coupling, dictKey):
rowArray = []
colArray = []
row,col = -1, -1
finalValue = ''
for char in coupling:
if char != 'J':
row, col = dictKey[char]
rowArray.append(row)
colArray.append(col)
else:
char = 'I'
row, col = dictKey[char]
rowArray.append(row)
colArray.append(col)
if(rowArray[0] == rowArray[1]):
for index in range(len(rowArray)):
if (colArray[index]+1 <= 3):
currentValue = (rowArray[index],colArray[index]+1)
else:
currentValue = (rowArray[index],0)
for key, value in dictKey.items():
if currentValue == value:
finalValue += key
return finalValue
elif(colArray[0] == colArray[1]):
for index in range(len(rowArray)):
if (rowArray[index]+1 <= 3):
currentValue = (rowArray[index]+1,colArray[index])
else:
currentValue = (0,colArray[index])
for key, value in dictKey.items():
if currentValue == value:
finalValue += key
return finalValue
else:
for index in range(len(rowArray)):
if (index == 0):
currentValue = (rowArray[index],colArray[1])
else:
currentValue = (rowArray[index],colArray[0])
for key, value in dictKey.items():
if currentValue == value:
finalValue += key
return finalValue
# Method : Rules for decrypting the coupling of the characters
# Param : coupling: Two characters that where put together
# dicKey: Key used to encrypt/decrypt the text
# Returns : returns sets of the decrypted coupling
def decrypt_rules(coupling, dictKey):
rowArray = []
colArray = []
row,col = -1, -1
finalValue = ''
for char in coupling:
row, col = dictKey[char]
rowArray.append(row)
colArray.append(col)
if(rowArray[0] == rowArray[1]):
for index in range(len(rowArray)):
if (colArray[index]-1 >= 0):
currentValue = (rowArray[index],colArray[index]-1)
else:
currentValue = (rowArray[index],3)
for key, value in dictKey.items():
if currentValue == value:
finalValue += key
return finalValue
elif(colArray[0] == colArray[1]):
for index in range(len(rowArray)):
if (rowArray[index]-1 >= 0):
currentValue = (rowArray[index]-1,colArray[index])
else:
currentValue = (3,colArray[index])
for key, value in dictKey.items():
if currentValue == value:
finalValue += key
return finalValue
else:
for index in range(len(rowArray)):
if (index == 0):
currentValue = (rowArray[index],colArray[1])
else:
currentValue = (rowArray[index],colArray[0])
for key, value in dictKey.items():
if currentValue == value:
finalValue += key
return finalValue