-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtermProj.py
More file actions
297 lines (220 loc) · 7.29 KB
/
termProj.py
File metadata and controls
297 lines (220 loc) · 7.29 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
# Kevin Rodriguez
# CSCI 4400 D01 Dr. Yi Gu
# Network Security Project Option 2
# Symmetric Encryption/Decryption (2 parts)
# Part 1: Encryption/Decryption using Polyalphabetic Cipher
# Part 2: Encryption/Decryption using Rail Fence Cipher
#----------------------
def main():
print("")
print("Welcome to the Symmetric Encryption/Decryption Program!\n")
#filename = input()
filename = "plaintext.txt"
#Read the plaintext from the file
infile = open(filename, 'r')
plaintext = infile.read()
print("Main")
print("----------\n")
print("Original plaintext: ", plaintext,"\n")
#call part 1
print("Calling Part 1")
print("----------\n")
part1(plaintext)
#call part 2
print("Calling Part 2")
print("----------\n")
part2(plaintext)
infile.close()
#-----------------------
#-----------------------
def part1(plaintext):
'''Part 1:
Input: A given text file for plaintext (assume only 26 letters, no special character, numbers, nor punctuations)
3 substitution ciphers, M1, M2, M3
M1 - right shift 8 letters
M2 - fixed mapping
M3 - left shift 12 letters
Cycling pattern:
n=4: M2, M3, M1, M3; M2, M3, M1, M3; M2, M3, M1, M3; ...
Output: Encrypted ciphertext and decrypted plaintext
'''
#create dictionaries for ciphers
M1Cipher = shiftCipher(8)
M2Cipher = fixedCipher()
M3Cipher = shiftCipher(-12)
#cycle pattern
cyclePattern = [M2Cipher, M3Cipher, M1Cipher, M3Cipher]
print("Welcome to Part 1: Encryption/Decryption using Polyalphabetic Ciphers\n")
# call encrypt func and assign to cipher text
print("**Encrypting**\n")
ciphertext = encryptPoly(plaintext, cyclePattern)
print("Original plaintext: ", plaintext,"\n")
print("Ciphertext: ", ciphertext,"\n")
#write to file
p1Cipher = open('p1Cipher.txt', 'w')
p1Cipher.write(ciphertext)
p1Cipher.close()
print("**Decrypting**\n")
#create dictionaries to decrypt
M1Plain = unshiftCipher(M1Cipher)
M2Plain = unshiftCipher(M2Cipher)
M3Plain = unshiftCipher(M3Cipher)
#cycle pattern
decryptOrder = [M2Plain, M3Plain, M1Plain, M3Plain]
#call decrypt func and assign
decryptedPlainText = decryptPoly(ciphertext, decryptOrder)
print("Decrypted ciphertext: ", decryptedPlainText, "\n")
#write to file
p1Decrypted = open('p1Decrypted.txt', 'w')
p1Decrypted.write(decryptedPlainText)
p1Decrypted.close()
#-----------------------
#-----------------------
def shiftCipher(toShift):
alphabet = "abcdefghijklmnopqrstuvwxyz"
# [toShift:] slices from toShift to end
# [:toShift] slices from beginning to toShift
# and concatenates to create new shifted alphabet
shiftedAlphabet = alphabet[toShift:] + alphabet[:toShift]
# zip plain and shifted alphabets into tuple
alphaTuple = zip(alphabet, shiftedAlphabet)
# create dictionary
cipherDict = {
plain : cipher
for plain,cipher in alphaTuple
}
return cipherDict
#----------------------
#----------------------
def unshiftCipher(dict):
## swaps dictionary letters
plainDict = {
cipher : plain
for plain, cipher in dict.items()
}
return plainDict
#----------------------
#----------------------
def fixedCipher():
#create alphabets and make it lowercase
alphabet = "abcdefghijklmnopqrstuvwxyz"
fixedAlphabet= "DKVQFGBXWPESCJHTMYAUOLRIZN".lower()
#create tuple
alphaTuple = zip(alphabet, fixedAlphabet)
#creates cipher dictionary
cipherDict = {
plain : cipher
for plain,cipher in alphaTuple
}
return cipherDict
#-----------------------
#-----------------------
def encryptPoly(plaintext, cyclePattern):
#empty list and cycle length used for mod op
ciphertext = []
cycleLength = len(cyclePattern)
index = 0
for letter in plaintext.lower():
if letter.isalpha():
#decide what step of polyalphabetic cipher to use by the index mod cyclelength
cipher = cyclePattern[index % cycleLength]
ciphertext.append(cipher[letter])
index += 1
else:
ciphertext.append(letter)
return "".join(ciphertext)
#-----------------------
#----------------------
def decryptPoly(ciphertext, decryptOrder):
decrypted = []
cycleLength = len(decryptOrder)
index = 0
for letter in ciphertext.lower():
if letter.isalpha():
reverseCipher = decryptOrder[index % cycleLength]
decrypted.append(reverseCipher[letter])
index += 1
else:
decrypted.append(letter)
return "".join(decrypted)
#----------------------
#----------------------
def part2(plaintext):
'''Part 2:
Input: A given text file for plaintext
User input depth of rail fence
Output:Encrypted ciphertext and decrypted plaintext
'''
print("Welcome to Part 2:Encryption/Decryption using Rail Fence Cipher\n")
#ask user for rail depth and convert to int
depth = input("Please enter 'n' the depth of the Rail Fence Cipher: ")
depth = int(depth)
print("")
print("**Encrypting**")
#to display each rail
ciphertext, rails = railFenceEncrypt(plaintext, depth)
print("Original plaintext: ", plaintext, "\n")
print("Ciphertext: ", ciphertext, "\n")
#write to file
p2Cipher = open('p2Cipher.txt', 'w')
p2Cipher.write(ciphertext)
p2Cipher.close()
#print rails and ciphertext
for i, row in enumerate(rails):
print(f"Row{i}: {' '* i}{row}")
print()
print("**Decrypting**")
decryptedPlainText = railFenceDecrypt(ciphertext, depth)
print("Decrypted ciphertext: ", decryptedPlainText, "\n")
#write to file
p2Decrypted = open('p2Decrypted.txt', 'w')
p2Decrypted.write(decryptedPlainText)
p2Decrypted.close()
#----------------------
#----------------------
def railFenceEncrypt(plaintext, depth):
if depth < 2:
return plaintext
# list of empty strings using depth for each rail
rails = [""] * depth
row, direction = 0, 1
#to climb up and down rails and return when reached
for letter in plaintext:
rails[row] += letter
row += direction
if row == 0 or row == depth - 1:
direction *= -1
ciphertext = "".join(rails)
return ciphertext, rails
#----------------------
#----------------------
def railFenceDecrypt(ciphertext, depth):
if depth < 2:
return ciphertext
#identify how long each rail is
count = [0] * depth
row, direction = 0, 1
for _ in ciphertext:
count[row] += 1
row += direction
if row == 0 or row == depth - 1:
direction *= -1
#slice the ciphertext into segments for each rail
rail, index = [], 0
for n in count:
rail.append(ciphertext[index:index + n ])
index += n
#climb up and down rail and append to plaintext, return when top or bottom is reached
railPtr = [0] * depth
row, direction = 0, 1
plaintext = []
for _ in ciphertext:
plaintext.append(rail[row][railPtr[row]])
railPtr[row] += 1
row += direction
if row == 0 or row == depth - 1:
direction *= -1
return "".join(plaintext)
#----------------------
if __name__ == "__main__":
main()