# Vigenere Cipher Using Python

In this article, we will learn Vigenere Cipher using Python. First, let’s discuss a few points vigenere cipher.

## What is Vigenere Cipher

Vigenere Cipher is a technique for encrypting alphabetic content. It utilizes a basic type of polyalphabetic replacement. A polyalphabetic cipher is any cipher dependent on replacement, utilizing numerous replacement alphabets.

The table comprises of the letters in order worked out multiple times in various lines, every letter set moved consistently to one side contrasted with the past letters in order, comparing to the 26 potential Caesar Ciphers.

At various focuses in the encryption cycle, the cipher utilizes alternate letters in order from one of the lines. The letters in order utilized at each point rely upon a rehashing catchphrase.

Encryption – (message+key) % 26

Decryption – (encrytption text-key+26)%26

## Vigenere Cipher in Python

1. Firstly, a key is generated with the help of a keyword if the length of the message is not equal to the keyword.

2. Use generateKey function to generate the key. The keyword is appended to itself until the length of the message is equal to the length of the key.

3. Once the key generated use encryption() to encrypt the message which takes two arguments one is the message that needs to be encrypted and the second argument is the key that returns the encrypted text.

4. In the encryption function the message and key are added modulo 26

5. Use the decryption function to decrypt the encrypted message. That takes two arguments one is the encrypted text and the second one is the key that used for encryption.

6. In the decryption function encryption text and key are subtracted, then added 26 modulo 26.

7. Finally, return the encrypted and decrypted messages.

```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 encryption(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 decryption(encrypt_text, key):
orig_text = []
for i in range(len(encrypt_text)):
x = (ord(encrypt_text[i]) -ord(key[i]) + 26) % 26
x += ord('A')
orig_text.append(chr(x))
return("" . join(orig_text))

if __name__ == "__main__":
string = input("Enter the message: ")
keyword = input("Enter the keyword: ")
key = generateKey(string, keyword)
encrypt_text = encryption(string,key)
print("Encrypted message:", encrypt_text)
print("Decrypted message:", decryption(encrypt_text, key))

```

Output

```Enter the message: CODESPEEDY
Enter the keyword: TIME
Encrypted message: BCVORDWOCM
Decrypted message: CODESPEEDY```