-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumnar-transposition-cipher.py
54 lines (44 loc) · 1.44 KB
/
columnar-transposition-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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Function to remove same character from keyword
def getKeyword(keyword):
keyword = keyword.upper().replace(" ", "")
listKeyword = []
for character in keyword:
if (character not in listKeyword):
listKeyword.append(character)
return listKeyword
# Function to generate matrix for encyption process using keyword and plaintext
def getMatrix(keyword, plaintext):
plaintext = plaintext.upper().replace(" ", "")
listPlaintext = []
for character in plaintext:
listPlaintext.append(character)
matrix = [listPlaintext[i:i+len(keyword)] for i in range(0, len(listPlaintext), len(keyword))]
if (matrix[len(matrix)-1] != len(keyword)):
for i in range(len(keyword) - len(matrix[len(matrix)-1])):
matrix[len(matrix)-1].append('X')
return matrix
# Function to encrypt plaintext
def encrypt(keyword, plaintext):
keyword = getKeyword(keyword)
matrix = getMatrix(keyword, plaintext)
listCipherChar = []
column = 0
while column < len(keyword):
cipherChar = ''
for i in range(len(matrix)):
cipherChar += matrix[i][column]
column += 1
listCipherChar.append(cipherChar)
dictionaryCipherChar = dict()
for i in range(len(keyword)):
dictionaryCipherChar[keyword[i]] = listCipherChar[i]
keyword.sort()
ciphertext = ''
for i in range(len(keyword)):
ciphertext += dictionaryCipherChar[keyword[i]]
return ciphertext
# Main program
keyword = 'wordkey'
plaintext = 'this is plaintext'
ciphertext = encrypt(keyword, plaintext)
print(ciphertext)