forked from manasijha/columnar-transposition-cipher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.py
63 lines (50 loc) · 1.37 KB
/
code.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
import math
def decrypt(message, keyword):
matrix = new1(ordering(keyword), message)
plaintext = "";
for r in range(len(matrix)):
for c in range (len(matrix[r])):
plaintext += matrix[r][c]
return plaintext
def new1(keySeq, message):
width = len(keySeq)
height = math.ceil(len(message) / width)
if height * width < len(message):
height += 1
matrix = new2(width, height, len(message))
pos = 0
for num in range(len(keySeq)):
column = keySeq.index(num+1)
r = 0
while (r < len(matrix)) and (len(matrix[r]) > column):
matrix[r][column] = message[pos]
r += 1
pos += 1
return matrix
def new2(width, height, length):
matrix = []
totalAdded = 0
for r in range(height):
matrix.append([])
for c in range(width):
if totalAdded >= length:
return matrix
matrix[r].append('')
totalAdded += 1
return matrix
def ordering(keyword):
sequence = []
for pos, ch in enumerate(keyword):
previousLetters = keyword[:pos]
newNumber = 1
for previousPos, previousCh in enumerate(previousLetters):
if previousCh > ch:
sequence[previousPos] += 1
else:
newNumber += 1
sequence.append(newNumber)
return sequence
cipher = input("Enter the CipherText to be Decrypted :")
key = input("Enter the Security Key for Decryption :")
dec = decrypt(cipher,key)
print(dec)