-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcolumnar_transposition_decryption.py
62 lines (52 loc) · 1.66 KB
/
columnar_transposition_decryption.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
import math
def row(s,key):
# to remove repeated alphabets in key
temp=[]
for i in key:
if i not in temp:
temp.append(i)
k=""
for i in temp:
k+=i
print("The key used for encryption is: ",k)
arr=[['' for i in range(len(k))]
for j in range(int(len(s)/len(k)))]
# To get indices as the key numbers instead of alphabets in the key, according
# to algorithm, for appending the elementsof matrix formed earlier, column wise.
kk=sorted(k)
d=0
# arranging the cipher message into matrix
# to get the same matrix as in encryption
for i in kk:
h=k.index(i)
for j in range(len(k)):
arr[j][h]=s[d]
d+=1
print("The message matrix is: ")
for i in arr:
print(i)
# the plain text
plain_text=""
for i in arr:
for j in i:
plain_text+=j
print("The plain text is: ",plain_text)
msg=input("Enter the message to be decrypted: ")
key=input("Enter the key in alphabets: ")
row(msg,key)
'''
----------OUTPUT----------
Enter the message to be decrypted: Mu b___mid____crnm___ ew ___o ee___ps ____ytoy___
Enter the key in alphabets: expensive
The key used for encryption is: expnsiv
The message matrix is:
['M', 'y', ' ', 'c', 'o', 'm', 'p']
['u', 't', 'e', 'r', ' ', 'i', 's']
[' ', 'o', 'w', 'n', 'e', 'd', ' ']
['b', 'y', ' ', 'm', 'e', '_', '_']
['_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_']
The plain text is: My computer is owned by me_______________________
>>>
'''