forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vigenere_cipher.py
45 lines (36 loc) · 1.01 KB
/
vigenere_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
# Vigenere Cipher python code.
def encrypt(msg, key):
cipher = ""
n = len(key)
i = 0
for l in msg:
# finding the index of the character
p = int(ord(l) + ord(key[i % n])) % 26
# to perform the shift with the key
cipher += chr(p + ord('A'))
i += 1
return cipher
def decrypt(cipher, key):
msg = ""
n = len(key)
j = 0
for l in cipher:
p = int(ord(l) - ord(key[j % n]) + 26) % 26
msg += chr(p + ord('A'))
j += 1
return msg
msg = input("Enter the plain All uppercase text: ")
key = input("Enter the key All uppercase text: ")
# encrypting the message
cipher = encrypt(msg, key)
print("The encrypted text is: ", cipher)
# decrypting the ciphar text
original = decrypt(cipher, key)
print("The decrypted(original) text is: ", msg)
'''
output:
Enter the plain All uppercase text: VIGENERECIPHER
Enter the key All uppercase text: SECRET
The encrypted text is: NMIVRXJIEZTAWV
The decrypted(original) text is: VIGENERECIPHER
'''