-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransposition encryption.py
86 lines (61 loc) · 2.09 KB
/
transposition encryption.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#this program is an transposition Encrption
import math
#the main part of program
def main():
print("welcome to jsk's encryption ")
print("""\t 1. encrypt
\t 2. decrypt""")
choose = int(input("select encrypt or decrypt the message : "))
while (choose < 1) or (choose > 2):
choose = int(input("enter either 1 or 2 to select option : "))
if choose == 1:
encrypt()
else:
decrypt()
#this is encryption part of the program
def encrypt():
plaintext = input("enter the message you want to encrypt : ")
key = int(input("enter the key to encrypt the message : "))
#it take key and message to enrypt
cipherText = encryption(key, plaintext)
print(cipherText + '|')
def encryption(key, message):
#multiply the list length with key
cipher = [''] * key
#create column with range key
for column in range(key):
currentIndex = column
while currentIndex < len(message):
cipher[column] += message[currentIndex]
currentIndex += key
return ''.join(cipher)
#decryption part of the program
def decrypt():
ciphertext = input("Enter the message to decrypt : ")
key = int(input("Enter the key to decrypt the message : "))
plaintext = decryption(key, ciphertext)
print(plaintext)
def decryption(key, message):
numofRows = key
numofColumn = int(math.ceil(len(message)/ float(key)))
numofShaded = (numofColumn * numofRows) - len(message)
plaintext = [''] * numofColumn
column = 0
row = 0
for symbol in message:
plaintext[column] += symbol
column += 1
if (column == numofColumn) or (column == numofColumn -1 and
row >= numofRows - numofShaded):
column = 0
row += 1
return ''.join(plaintext)
start= True
while start:
main()
again = input("if you want to encrypt or decrypt another message, enter y : ")
if again == 'y':
start = True
else:
start = False
print("bye")