-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoute_Cipher.py
39 lines (30 loc) · 954 Bytes
/
Route_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
def route_cipher(message, rows, cols):
message+= ' ' * (rows * cols- len(message))
grid = [[' ' for _ in range(cols)] for _ in range(rows)]
for r in range(rows):
for c in range(cols):
grid[r][c] = message[r*cols+c]
ciphertext = ''
for c in range(cols):
for r in range(rows):
ciphertext += grid[r][c]
return ciphertext
def route_cipher_decrypt(ciphertext, rows, cols):
grid = [[' ' for _ in range(cols)] for _ in range(rows)]
for r in range(rows):
for c in range(cols):
grid[r][c] = ''
i=0
for c in range(cols):
for r in range(rows):
grid[r][c] = ciphertext[i]
i = i+1
plaintext = ''
for r in range(rows):
for c in range(cols):
plaintext += grid[r][c]
return plaintext.strip()
message = input("Enter the message: ")
ciphertext = route_cipher(message, 3, 4)
print("Cipher text is: ", ciphertext)
print("Plain text is: ", route_cipher_decrypt(ciphertext,3,4))