-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.py
283 lines (233 loc) · 8.27 KB
/
project.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import math
def encryptMessage(msg,key):
cipher = ""
# track key indices
index = 0
msg_len = float(len(msg))
msg_list = list(msg)
key_list = sorted(list(key))
#matrix is of dimension row x col
col = len(key)
row = int(math.ceil(msg_len / col))
# add the padding character '_' for empty cells
padding = int((row * col) - msg_len)
msg_list.extend('_' * padding)
#print(msg_list)
#create the matrix
matrix = [msg_list[i: i + col]
for i in range(0, len(msg_list), col)]
# print("Matrix Construction...\n")
# print(matrix)
# read matrix column-wise using key
for _ in range(col):
curr_idx = key.index(key_list[index])
cipher += ''.join([row[curr_idx]
for row in matrix])
#print(cipher)
index += 1
return cipher
# Decryption
def decryptMessage(cipher,key, flag = 0):
msg = ""
# track key indices
index = 0
# track msg indices
msg_indx = 0
msg_len = float(len(cipher))
msg_list = list(cipher)
# calculate column of the matrix
col = len(key)
row = int(math.ceil(msg_len / col))
# convert key into list and sort
# alphabetically so we can access
# each character by its alphabetical position.
key_list = sorted(list(key))
# create an empty matrix to
# store deciphered message
deciphered = []
for _ in range(row):
deciphered += [[None] * col]
# Arrange the matrix column wise according
# to permutation order by adding into new matrix
for _ in range(col):
curr_idx = key.index(key_list[index])
for j in range(row):
deciphered[j][curr_idx] = msg_list[msg_indx]
msg_indx += 1
index += 1
#print(deciphered)
# convert decrypted msg matrix into a string
try:
msg = ''.join(sum(deciphered, []))
except TypeError:
raise TypeError("This program cannot",
"handle repeating words.")
if flag == 1:
return msg
else:
null_count = msg.count('_')
if null_count > 0:
return msg[: -null_count]
return msg
def encryptRailFence(text, key):
# create the matrix to cipher
# plain text key = rows ,
# length(text) = columns
# filling the rail matrix
# to distinguish filled
# spaces from blank ones
rail = [['\n' for i in range(len(text))]
for j in range(key)]
# to find the direction
dir_down = False
row, col = 0, 0
for i in range(len(text)):
# check the direction of flow
# reverse the direction if we've just
# filled the top or bottom rail
if (row == 0) or (row == key - 1):
dir_down = not dir_down
# fill the corresponding alphabet
rail[row][col] = text[i]
col += 1
# find the next row using
# direction flag
if dir_down:
row += 1
else:
row -= 1
# now we can construct the cipher
# using the rail matrix
result = []
for i in range(key):
for j in range(len(text)):
if rail[i][j] != '\n':
result.append(rail[i][j])
return("" . join(result))
# This function receives cipher-text
# and key and returns the original
# text after decryption
def decryptRailFence(cipher, key):
# create the matrix to cipher
# plain text key = rows ,
# length(text) = columns
# filling the rail matrix to
# distinguish filled spaces
# from blank ones
rail = [['\n' for i in range(len(cipher))]
for j in range(key)]
# to find the direction
dir_down = None
row, col = 0, 0
# mark the places with '*'
for i in range(len(cipher)):
if row == 0:
dir_down = True
if row == key - 1:
dir_down = False
# place the marker
rail[row][col] = '*'
col += 1
# find the next row
# using direction flag
if dir_down:
row += 1
else:
row -= 1
# now we can construct the
# fill the rail matrix
index = 0
for i in range(key):
for j in range(len(cipher)):
if ((rail[i][j] == '*') and
(index < len(cipher))):
rail[i][j] = cipher[index]
index += 1
# now read the matrix in
# zig-zag manner to construct
# the resultant text
result = []
row, col = 0, 0
for i in range(len(cipher)):
# check the direction of flow
if row == 0:
dir_down = True
if row == key-1:
dir_down = False
# place the marker
if (rail[row][col] != '*'):
result.append(rail[row][col])
col += 1
# find the next row using
# direction flag
if dir_down:
row += 1
else:
row -= 1
return("".join(result))
# Driver Code
if __name__ == '__main__':
ans=True
while ans:
print("\t\t\t\t\t\t==========================================================")
print("\t\t\t\t\t\t\t\tTRANSPOSITION CIPHERS")
print("\t\t\t\t\t\t==========================================================")
print ("""
1.Columnar Transposition
2.Double Columnar Transposition
3.Rail Fence Cipher
4.Exit/Quit
""")
ans=input("Please choose one of the above encryption methods: \n >> ")
if ans!="4":
choice=input("Choose 1. Encryption 2. Decryption \n >> ")
msg=input("Please enter the message you wish to encrypt/decrypt below:\n >> ")
if ans=="1":
print("\nYou have picked Columnar Transposition!")
n=input("Please enter the key: \n >> ")
if choice=="1":
cipher = encryptMessage(msg,n)
else:
cipher = decryptMessage(msg,n)
elif ans=="2":
print("\nDouble Columnar Transposition")
#output = "1"
#flag = 1
#while (output == "1" or output == "2") and flag == 1:
'''print(" Would you like to use the same key for both transpositions?")
output = input(" 1 for YES or 2 for NO :")
if output == "1":
key1 = key2 = input(" Enter the common key:")
flag = 0
elif output == "2":
key1 = input(" Enter the first key:")
key2 = input(" Enter the second key:")
flag = 0
else:
print("Please enter a valid choice")'''
key = input("Please enter the key: \n >> ")
if choice=="1":
cipher1 = encryptMessage(msg,key)
cipher = encryptMessage(cipher1,key)
else:
cipher1 = decryptMessage(msg,key,1)
cipher = decryptMessage(cipher1,key)
elif ans=="3":
print("\n Rail Fence Cipher")
n=int(input("Please enter the key: \n >> "))
if choice=="1":
cipher = encryptRailFence(msg, n)
else:
cipher = decryptRailFence(msg, n)
if choice == "1":
print("Encrypted Message: {}".
format(cipher))
else:
print("Decrypted Message: {}".
format(cipher))
elif ans=="4":
print("\nGoodbye!")
exit()
else:
print("\nNot Valid Choice Try again")
exit()