Skip to content

Commit 187233f

Browse files
Issue #67: Added new encryption algorithm
1 parent 7f12f2c commit 187233f

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import math
2+
def encrypt(key, message):
3+
ciphertext = []
4+
for col in range(key):
5+
position = col
6+
while position < len(message):
7+
ciphertext.append(message[position])
8+
position += key
9+
return ''.join(ciphertext)
10+
11+
def decrypt(key, message):
12+
numOfRows = math.ceil(len(message) / key)
13+
plaintext = []
14+
cipher = [*message]
15+
remainder = len(message)%key
16+
count=0
17+
if(remainder != 0):
18+
for i in range(0,len(message),numOfRows):
19+
count += 1
20+
if(count > remainder):
21+
cipher.insert(i+numOfRows-1,'#')
22+
cipher.append('#')
23+
for row in range(numOfRows):
24+
position = row
25+
while(position < len(cipher)):
26+
plaintext.append(cipher[position])
27+
position += numOfRows
28+
plaintext = [ele for ele in plaintext if ele != '#']
29+
else:
30+
for row in range(numOfRows):
31+
position = row
32+
while(position < len(message)):
33+
plaintext.append(message[position])
34+
position += numOfRows
35+
return ''.join(plaintext)
36+
def main():
37+
while(True):
38+
print("1. Encrypt")
39+
print("2. Decrypt")
40+
print("3. Exit")
41+
option = int(input("Enter the option: "))
42+
if(option == 1):
43+
text = input("Enter the plain text: ")
44+
k = int(input("Enter the key: "))
45+
cipher = encrypt(k,text)
46+
print("Ciphertext: ", cipher)
47+
if(option == 2):
48+
text = input("Enter the ciphertext: ")
49+
k = int(input("Enter the key: "))
50+
plain = decrypt(k, text)
51+
print("Plaintext: ", plain)
52+
if(option == 3):
53+
break
54+
main()

0 commit comments

Comments
 (0)