In: Computer Science
Cryptography and Applications
1. Write Python program to implement Caesar’s Cipher. Take user input to get plain text and key.
2. Write a Python program to implement Vignere Cipher. Take user input to get plain text and key.
TRY TO MAKE IT AS EASY AS YOU CAN.
PART 1 -
CODE -
# Taking plain text and key as input from the user
plain_text = input("Enter Plain Text: ")
key = int(input("Enter key: "))
# Initializing cipher text with empty string
cipher_text = ""
# Iterating over each character of plain text
for i in range(len(plain_text)):
letter = plain_text[i]
# Encrypting uppercase characters
if letter.isupper():
cipher_text += chr((ord(letter) - 65 + key) % 26 + 65)
# Encrypting lowercase characters
else:
cipher_text += chr((ord(letter) - 97 + key) % 26 + 97)
# Displaying plain text and encrypted text
print("\n\nPlain Text: ", plain_text)
print("\nEncrypted Text: ", cipher_text)
SCREENSHOT -
PART 2 -
CODE -
# Taking plain text and key as input from the user
plain_text = input("Enter Plain Text: ")
key = input("Enter key: ")
# Initializing generated key with key
gen_key = key
# Checking if length of key is equal to length of plain text or
not
if len(gen_key) != len(plain_text):
# Generating key in a cyclic manner until its length is equal to
length of plain text
for i in range(len(plain_text) - len(key)):
gen_key += key[i % len(key)]
# Initializing cipher text with empty string
cipher_text = ""
# Iterating over each character of plain text
for i in range(len(plain_text)):
# Converting into letter number between 0-25
letter_num = (ord(plain_text[i]) + ord(gen_key[i])) % 26
# Converting into ASCII characters
if plain_text[i].islower():
cipher_text += chr(ord('A') + letter_num).lower()
else:
cipher_text += chr(ord('A') + letter_num)
# Displaying plain text and encrypted text
print("\n\nPlain Text: ", plain_text)
print("\nEncrypted Text: ", cipher_text)
SCREENSHOT -
If you have any doubt regarding the solution, then do comment.
Do upvote.