Skip to content

Commit d067fe6

Browse files
authoredNov 13, 2022
Add files via upload
1 parent d44441c commit d067fe6

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
 

‎vignere_cipher.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''Firstly, a key is generated with the help of a keyword if the length of
2+
the message is not equal to the keyword.'''
3+
def generateKey(string, key):
4+
key = list(key)
5+
if len(string) == len(key):
6+
return (key)
7+
else:
8+
for i in range(len(string) - len(key)):
9+
key.append(key[i % len(key)])
10+
'''The join() method takes all items in an iterable and joins them into one string.'''
11+
return ("".join(key))
12+
13+
'''encryption() to encrypt the message which takes two arguments one is the message that needs to be encrypted and
14+
the second argument is the key that returns the encrypted text.'''
15+
def encryption(string, key):
16+
encrypt_text = []
17+
for i in range(len(string)):
18+
'''In the encryption function the
19+
message and key are added modulo 26'''
20+
x = (ord(string[i]) + ord(key[i])) % 26
21+
x += ord('A')
22+
encrypt_text.append(chr(x))
23+
return ("".join(encrypt_text))
24+
25+
'''decryption function to decrypt the encrypted message. That takes two
26+
arguments one is the encrypted text and the second one is the key that used for encryption.'''
27+
def decryption(encrypt_text, key):
28+
orig_text = []
29+
for i in range(len(encrypt_text)):
30+
'''In the decryption function encryption text
31+
and key are subtracted, then added 26 modulo 26.'''
32+
x = (ord(encrypt_text[i]) - ord(key[i]) + 26) % 26
33+
x += ord('A')
34+
orig_text.append(chr(x))
35+
return ("".join(orig_text))
36+
37+
38+
if __name__ == "__main__":
39+
string = input("Enter the message: ")
40+
keyword = input("Enter the keyword: ")
41+
key = generateKey(string, keyword)
42+
encrypt_text = encryption(string, key)
43+
print("Encrypted message:", encrypt_text)
44+
print("Decrypted message:", decryption(encrypt_text, key))
45+

0 commit comments

Comments
 (0)
Please sign in to comment.