forked from theutpal01/HacktoberFest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvignere.py
31 lines (29 loc) · 970 Bytes
/
vignere.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
def generateKey(string, key):
key = list(key)
if len(string) == len(key):
return(key)
else:
for i in range(len(string) -len(key)):
key.append(key[i % len(key)])
return("" . join(key))
def encrypt(string, key):
encrypt_text = []
for i in range(len(string)):
x = (ord(string[i]) +ord(key[i])) % 26
x += ord('A')
encrypt_text.append(chr(x))
return("" . join(encrypt_text))
def decrypt(encrypt_text, key):
original = []
for i in range(len(encrypt_text)):
x = (ord(encrypt_text[i]) -ord(key[i]) + 26) % 26
x += ord('A')
original.append(chr(x))
return("" . join(original))
if __name__ == "__main__":
string = input("Enter the message: ")
keyword = input("Enter the key: ")
key = generateKey(string, keyword)
encrypt_text = encrypt(string,key)
print("Encrypted message is:", encrypt_text)
print("Decrypted message is:", decrypt(encrypt_text, key))