-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCypher.py
256 lines (200 loc) · 6.54 KB
/
TestCypher.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
# File: TestCipher.py
# Description:
# Student's Name:
# Student's UT EID:
# Partner's Name:
# Partner's UT EID:
# Course Name: CS 313E
# Unique Number:
# Date Created:
# Date Last Modified:
import sys
def make_railroad (strng, key):
railroad = []
key = int(key)
for i in range(key):
row = []
for col in range(len(strng) - 1):
row.append('-')
railroad.append(row)
return railroad
# Input: strng is a string of characters and key is a positive
# integer 2 or greater and strictly less than the length
# of strng
# Output: function returns a single string that is encoded with
# rail fence algorithm
def rail_fence_encode ( strng, key ):
railroad = make_railroad(strng,key)
x = 0
y = 0
move = 1
key = int(key)
for char in strng:
railroad[x][y] = char
y += 1
x += move
if y >= len(strng) - 1:
break
elif x > key - 1:
move = -1
x -= 2
elif x < 0:
move = 1
x += 2
final = ""
for row in range(len(railroad)):
count = 0
for i in range(len(railroad[0])):
if railroad[row][count] != '-':
final = final + railroad[row][count]
count += 1
return final
# Input: strng is a string of characters and key is a positive
# integer 2 or greater and strictly less than the length
# of strng
# Output: function returns a single string that is decoded with
# rail fence algorithm
def rail_fence_decode ( strng, key ):
railroad = make_railroad(strng,key)
x = 0
y = 0
move = 1
key = int(key)
for char in strng:
railroad[x][y] = '*'
y += 1
x += move
if y >= len(strng) - 1:
break
elif x > key - 1:
move = -1
x -= 2
elif x < 0:
move = 1
x += 2
strngCount = 0
for row in range(len(railroad)):
count = 0
for i in range(len(railroad[0])):
if railroad[row][count] == '*':
railroad[row][count] = strng[strngCount]
strngCount += 1
count += 1
final = ""
x = 0
y = 0
move = 1
for char in strng:
if railroad[x][y] != '-':
final = final + railroad[x][y]
y += 1
x += move
if y >= len(strng) - 1:
break
elif x > key - 1:
move = -1
x -= 2
elif x < 0:
move = 1
x += 2
return final
# Input: strng is a string of characters
# Output: function converts all characters to lower case and then
# removes all digits, punctuation marks, and spaces. It
# returns a single string with only lower case characters
def filter_string ( strng ):
strng = strng.lower()
new = ""
for char in strng:
if 97 <= ord(char) <= 122:
new = new + char
return new # placeholder for the actual return statement
def long_phrase(strng,passphrase):
count = 0
miniCount = 0
phrase = ""
while count < len(strng)+1:
if count + len(passphrase) <= len(strng):
phrase = phrase + passphrase
count += len(passphrase)
else:
phrase = phrase + passphrase[miniCount]
miniCount += 1
count += 1
return phrase
# Input: p is a character in the pass phrase and s is a character
# in the plain text
# Output: function returns a single character encoded using the
# Vigenere algorithm. You may not use a 2-D list
def encode_character (p, s):
LETTERS = 'abcdefghijklmnopqrstuvwxyz'
keyNum = ord(s) - 97
for i in range(0, keyNum):
LETTERS = LETTERS + LETTERS[i]
for j in range(0, keyNum):
LETTERS = LETTERS[1:]
keyNum2 = ord(p) - 97
finalLetter = LETTERS[keyNum2]
return finalLetter # placeholder for actual return statement
# Input: p is a character in the pass phrase and s is a character
# in the plain text
# Output: function returns a single character decoded using the
# Vigenere algorithm. You may not use a 2-D list
def decode_character (p, s):
LETTERS = 'abcdefghijklmnopqrstuvwxyz'
keyNum = ord(p) - 97
while True:
LETTERS = LETTERS + LETTERS[0]
LETTERS = LETTERS[1:]
if LETTERS[keyNum] == s:
break
return LETTERS[0]
# Input: strng is a string of characters and phrase is a pass phrase
# Output: function returns a single string that is encoded with
# Vigenere algorithm
def vigenere_encode ( strng, phrase ):
long_phrase1 = long_phrase(strng, phrase)
finalstrng = ""
for i in range(len(strng)-1):
finalstrng = finalstrng + encode_character(strng[i],long_phrase1[i])
return finalstrng
# Input: strng is a string of characters and phrase is a pass phrase
# Output: function returns a single string that is decoded with
# Vigenere algorithm
def vigenere_decode ( strng, phrase ):
long_phrase1 = long_phrase(strng, phrase)
finalstrng = ""
for i in range(len(strng)-1):
finalstrng = finalstrng + decode_character(long_phrase1[i],strng[i])
return finalstrng
def main():
# read the plain text from stdin
railstrng = sys.stdin.readline()
# read the key from stdin
key1 = sys.stdin.readline()
# encrypt and print the encoded text using rail fence cipher
print(rail_fence_encode(railstrng,key1))
# read encoded text from stdin
railstrng2 = sys.stdin.readline()
# read the key from stdin
key2 = sys.stdin.readline()
# decrypt and print the plain text using rail fence cipher
print(rail_fence_decode(railstrng2,key2))
# read the plain text from stdin
vstrng = sys.stdin.readline()
# read the pass phrase from stdin
passtrng = sys.stdin.readline()
passtrng = passtrng.strip()
print(long_phrase(vstrng,passtrng))
# encrypt and print the encoded text using Vigenere cipher
print(vigenere_encode ( vstrng, passtrng ))
# read the encoded text from stdin
vstrng2 = sys.stdin.readline()
# read the pass phrase from stdin
passtrng2 = sys.stdin.readline()
# decrypt and print the plain text using Vigenere cipher
print(vigenere_decode ( vstrng2, passtrng2 ))
# The line above main is for grading purposes only.
# DO NOT REMOVE THE LINE ABOVE MAIN
if __name__ == "__main__":
main()