-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransposition_dec.py
57 lines (45 loc) · 1.53 KB
/
transposition_dec.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
# Input the cipher text from the transposition_enc.py and key used
cipherText = input("Enter cipher text: ")
transKey = input("Enter transposition key: ")
plaintextlist = []
cipherTextList = []
# Same symbols sets we used in transposition_enc.py
symbols1 = "$?@!%)&*"
symbols2 = "#(-=+></"
transKeyLength = len(transKey)
cipherTextLength = len(cipherText)
matrixWidth = cipherTextLength // transKeyLength
transKeySeq = []
# Convert the key to numrical format if it is not
for j in range(transKeyLength):
seq = 1
for k in transKey:
if transKey[j] > k:
seq = seq + 1
transKeySeq.append(str(seq))
# We'll creat matrix but invert the dimensions.
si = 0
ei = matrixWidth
for s in range(transKeyLength):
try:
cipherTextList.append(cipherText[si:ei])
si = ei
ei = ei + matrixWidth
except IndexError:
cipherTextList.append(cipherText[si:])
# Decrypt the cipher text
for i in range(matrixWidth):
for j in range(1, transKeyLength + 1):
charIndex = transKeySeq.index(str(j))
plaintextlist.append(cipherTextList[charIndex][i])
plainText = "".join(plaintextlist)
# Replace the symbols from symbol set 1 to space
for c in plainText:
if c in symbols1:
plainText = plainText.replace(c, ' ')
# Remove the symbols from symbol set 2
for c in plainText:
if c in symbols2:
plainText = plainText.replace(c, '')
# We have our plain text back! (Wohooo)
print("Plaintext is: ", plainText)