-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCipher.py
131 lines (93 loc) · 2.79 KB
/
Cipher.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
# File: Cipher.py
# Description:
# Student Name: Nicholas Webb
# Student UT EID: nw6887
# Partner Name: EJ Porras
# Partner UT EID: ejp2488
# Course Name: CS 313E
# Unique Number: 51135
# Date Created: 2/8/2022
# Date Last Modified: 2/8/2022
import math
# Input: strng is a string of 100 or less of upper case, lower case,
# and digits
# Output: function returns an encrypted string
def encrypt(strng):
string_to_encyrpt = strng
size = math.sqrt(len(string_to_encyrpt))
# changes the length of string until it can fit into square matrix
while size ** 2 != len(string_to_encyrpt):
string_to_encyrpt += "*"
# inserts characters in the string to be encrypted into the square matrix
p1 = []
for i in range(size):
p2 = []
for j in range(size):
p2.append(string_to_encyrpt[0])
string_to_encyrpt = string_to_encyrpt[1:]
p1.append(p2)
# rotates square matrix clockwise by 90 degrees
for i in range(size // 2):
for j in range(i, size - i - 1):
temp = p1[i][j]
p1[i][j] = p1[size - j - 1][i]
p1[size - j - 1][i] = p1[size - i - 1][size - j - 1]
p1[size - i - 1][size - j - 1] = p1[j][size - i - 1]
p1[j][size - i - 1] = temp
encrypted_string = ""
# traverses through matrix to record the encrypted string
for i in range(size):
for j in range(size):
if p1[i][j] == "*":
continue
else:
encrypted_string += p1[i][j]
return encrypted_string
# Input: strng is a string of 100 or less of upper case, lower case,
# and digits
# Output: function returns an encrypted string
def decrypt ( strng ):
size = math.ceil(math.sqrt(len(strng)))
while len(strng) < size ** 2:
strng = strng + "*"
matrix = []
for i in range(len(strng)):
if i % size == 0:
ch = strng[i + size]
p1 = []
for j in ch:
p1.append(j)
matrix.append(p1)
for i in range(0, size/2):
for j in range(i, size - i - 1):
holder = matrix[i][j] #this is the current index
matrix[i][j] = matrix[j][size - i - 1] #moving from
matrix[j][size - i - 1] = matrix[size - i - 1][size - j - 1]
matrix[size - i - 1][size - j - 1] = matrix[size - j - 1][i]
matrix[size - j - 1][i] = holder
encrypted_string = ""
for i in range(size):
for j in range(size):
if matrix[i][j] == "*":
continue
else:
encrypted_string += p1[i][j]
return encrypted_string
def main():
# read the strings P and Q from standard input
p_str = input()
q_str = input()
try:
while True:
# encrypt the string P
printed_encryption = encrypt(p_str)
# decrypt the string Q
printed_decryption = decrypt(q_str)
# print the encrypted string of P
print(printed_encryption)
# and the decrypted string of Q
print(printed_decryption)
except EOFError:
pass
if __name__ == "__main__":
main()