-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathcolumnar_transposition_cipher.py
54 lines (53 loc) · 1.69 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
import math
def encrypt(key, message):
ciphertext = []
for col in range(key):
position = col
while position < len(message):
ciphertext.append(message[position])
position += key
return ''.join(ciphertext)
def decrypt(key, message):
numOfRows = math.ceil(len(message) / key)
plaintext = []
cipher = [*message]
remainder = len(message)%key
count=0
if(remainder != 0):
for i in range(0,len(message),numOfRows):
count += 1
if(count > remainder):
cipher.insert(i+numOfRows-1,'#')
cipher.append('#')
for row in range(numOfRows):
position = row
while(position < len(cipher)):
plaintext.append(cipher[position])
position += numOfRows
plaintext = [ele for ele in plaintext if ele != '#']
else:
for row in range(numOfRows):
position = row
while(position < len(message)):
plaintext.append(message[position])
position += numOfRows
return ''.join(plaintext)
def main():
while(True):
print("1. Encrypt")
print("2. Decrypt")
print("3. Exit")
option = int(input("Enter the option: "))
if(option == 1):
text = input("Enter the plain text: ")
k = int(input("Enter the key: "))
cipher = encrypt(k,text)
print("Ciphertext: ", cipher)
if(option == 2):
text = input("Enter the ciphertext: ")
k = int(input("Enter the key: "))
plain = decrypt(k, text)
print("Plaintext: ", plain)
if(option == 3):
break
main()