forked from VoxelPixel/CiphersInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColTransCipher.py
More file actions
163 lines (132 loc) · 3.71 KB
/
ColTransCipher.py
File metadata and controls
163 lines (132 loc) · 3.71 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
def cipher_encryption():
msg = input("Enter Plain Text: ").replace(" ", "").upper()
# print(msg)
key = input("Enter keyword: ").upper()
# assigning numbers to keywords
kywrd_num_list = keyword_num_assign(key)
# printing key
for i in range(len(key)):
print(key[i], end=" ", flush=True)
# for
print()
for i in range(len(key)):
print(str(kywrd_num_list[i]), end=" ", flush=True)
# for
print()
print("-------------------------")
# in case characters don't fit the entire grid perfectly.
extra_letters = len(msg) % len(key)
# print(extraLetters)
dummy_characters = len(key) - extra_letters
# print(dummyCharacters)
if extra_letters != 0:
for i in range(dummy_characters):
msg += "."
# if
# print(msg)
num_of_rows = int(len(msg) / len(key))
# Converting message into a grid
arr = [[0] * len(key) for i in range(num_of_rows)]
z = 0
for i in range(num_of_rows):
for j in range(len(key)):
arr[i][j] = msg[z]
z += 1
# for
# for
for i in range(num_of_rows):
for j in range(len(key)):
print(arr[i][j], end=" ", flush=True)
print()
# for
# getting locations of numbers
num_loc = get_number_location(key, kywrd_num_list)
print(num_loc)
# cipher
cipher_text = ""
k = 0
for i in range(num_of_rows):
if k == len(key):
break
else:
d = int(num_loc[k])
# if
for j in range(num_of_rows):
cipher_text += arr[j][d]
# for
k += 1
# for
print("Cipher Text: {}".format(cipher_text))
def get_number_location(key, kywrd_num_list):
num_loc = ""
for i in range(len(key) + 1):
for j in range(len(key)):
if kywrd_num_list[j] == i:
num_loc += str(j)
# if
# for
# for
return num_loc
def keyword_num_assign(key):
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
kywrd_num_list = list(range(len(key)))
# print(kywrdNumList)
init = 0
for i in range(len(alpha)):
for j in range(len(key)):
if alpha[i] == key[j]:
init += 1
kywrd_num_list[j] = init
# if
# inner for
# for
return kywrd_num_list
def cipher_decryption():
msg = input("Enter Cipher Text: ").replace(" ", "").upper()
# print(msg)
key = input("Enter keyword: ").upper()
# assigning numbers to keywords
kywrd_num_list = keyword_num_assign(key)
num_of_rows = int(len(msg) / len(key))
# getting locations of numbers
num_loc = get_number_location(key, kywrd_num_list)
# Converting message into a grid
arr = [[0] * len(key) for i in range(num_of_rows)]
# decipher
plain_text = ""
k = 0
itr = 0
# print(arr[6][4])
# itr = len(msg)
for i in range(len(msg)):
d = 0
if k == len(key):
k = 0
else:
d: int = int(num_loc[k])
for j in range(num_of_rows):
arr[j][d] = msg[itr]
# print("j: {} d: {} m: {} l: {} ". format(j, d, msg[l], l))
itr += 1
if itr == len(msg):
break
k += 1
print()
for i in range(num_of_rows):
for j in range(len(key)):
plain_text += str(arr[i][j])
# for
# for
print("Plain Text: " + plain_text)
def main():
choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): "))
if choice == 1:
print("Encryption")
cipher_encryption()
elif choice == 2:
print("Decryption")
cipher_decryption()
else:
print("Invalid Choice")
if __name__ == "__main__":
main()