0% found this document useful (0 votes)
6 views1 page

Vigenere Cipher

The document provides Python code for implementing the Vigenere cipher, which includes functions for both encryption and decryption. It takes user input for plain text and a key, encrypts the text, and then decrypts it back to verify the process. The code handles both uppercase and lowercase letters and maintains non-alphabetic characters unchanged.

Uploaded by

nimshashetty3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Vigenere Cipher

The document provides Python code for implementing the Vigenere cipher, which includes functions for both encryption and decryption. It takes user input for plain text and a key, encrypts the text, and then decrypts it back to verify the process. The code handles both uppercase and lowercase letters and maintains non-alphabetic characters unchanged.

Uploaded by

nimshashetty3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

NAME:NEHASHETTY

USN:4NM21IS094

Q. IMPLEMENT VIGERE CIPHER


def encrypt_vigenere(plain_text, key):
encrypted_text = ''
key_index = 0
for char in plain_text:
if char.isalpha():
key_char = key[key_index % len(key)]
shift = ord(key_char.upper()) - 65
if char.isupper():
encrypted_text += chr((ord(char) + shift - 65) % 26 + 65)
else:
encrypted_text += chr((ord(char) + shift - 97) % 26 + 97)
key_index += 1
else:
encrypted_text += char
return encrypted_text

def decrypt_vigenere(encrypted_text, key):


decrypted_text = ''
key_index = 0
for char in encrypted_text:
if char.isalpha():
key_char = key[key_index % len(key)]
shift = ord(key_char.upper()) - 65
if char.isupper():
decrypted_text += chr((ord(char) - shift - 65) % 26 + 65)
else:
decrypted_text += chr((ord(char) - shift - 97) % 26 + 97)
key_index += 1
else:
decrypted_text += char
return decrypted_text

# Taking input from the user


plain_text = input("Enter the plain text: ")
key = input("Enter the key: ")

# Encrypting the input text


encrypted_text = encrypt_vigenere(plain_text, key)
print("Encrypted:", encrypted_text)

# Decrypting the encrypted text


decrypted_text = decrypt_vigenere(encrypted_text, key)
print("Decrypted:", decrypted_text)

OUTPUT:

You might also like