In: Computer Science
Using Python, write a simple application that takes user input of plaintext and key, and encrypt the plaintext with Vigenere Cipher. The application should then print out the plaintext and the ciphertext.
Note: The program is properly commented, if face any doubt leave a comment.
Program:
#Note: Use only capital letter
#Function to perform Vignere cipher conversion
def vignere_cipher(plain_text, key):
#defining cipher_text variable to store variable
cipher_text = ""
#storing length of the key to l
l = len(key)
#we'll iterate through each character of the plain text
for i in range(len(plain_text)):
#Formula: Ci = (Pi + Ki) mod 26
#Ci is the cipher text character's ASCII value
#Pi is the plain text character's ASCII value
#Ki is the corresponding key character's ASCII value
x = (ord(plain_text[i]) + ord(key[i%l])) % 26
x += ord('A')
cipher_text += chr(x)
#returning cipher text
return cipher_text
#Calling the main program
plain_text = input('Enter plain text: ')
key = input('Enter key: ')
cipher_text = vignere_cipher(plain_text, key)
print('\n\nCipher text: ', cipher_text)
Output:
Leave a comment if face any doubt!! Happy coding!!