In: Computer Science
Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and a parameter of type int. The first parameter is the plaintext message, and the second parameter is the encryption key. The method strictly does the following tasks:
a. Convert the string into a list (let us refer to it as lista). An element in the generated list is the position of the corresponding letter in the parameter string in the English alphabet. Example: ‘C’ or ‘c’ in the parameter string will be converted to the number
b in lista. Assume that the parameter string contains only alphabetic characters. 2. Encrypt the generated list (lista) using Caesar cipher using the provided encryption key. You do not need to create a new list, update the elements of the lista list.
c. Convert lista into a string again by converting the positional element in the list to a character in the ciphertext string. Assume that the plaintext is ‘WelcomeToCryptography’ and the shift key is 5.
Step Output 1
lista = [22, 4, 11, 2, 14, 12, 4, 19, 14, 2, 17, 24, 15, 19, 14, 6, 17, 0, 15, 7, 24] 2
lista = [1, 9, 16, 7, 19, 17, 9, 24, 19, 7, 22, 3, 20, 24, 19, 11, 22, 5, 20, 12, 3] 3
ciphertext = ‘BJQHTRJYTHWDUYTLWFUMD’
The method’s header is as follows: def casesarencryption(s,
key):
Program:
#defining the function, where s is of type string
#and key of type int
def casesarencryption(s, key):
#defining the list lista
lista = []
#converting all the characters in upper case
#then we would not need to worry about
#upper case or lower case character
s = s.upper()
#traversing plain text, s
for i in s:
#converting character to ASCII value
#and subtracting 64 as ASCII value of 'A' is 65
temp = ord(i)-65
lista.append(temp)
#printing Output 1
print("Output 1")
print("lista =",str(lista))
#adding key to each element of the lista
for i in range(len(lista)):
lista[i] = (lista[i]+key)%26
#printing Output 2
print("\nOutput 2")
print("lista =",str(lista))
#converting the lista into cipher text
cipher_text = ""
for i in lista:
#adding 65 as it is ASCII value of 'A'
cipher_text += chr(i+65)
#printing Output 3
print("\nOutput 3")
print("Cipher text ='{}'".format(cipher_text))
Output:
Leave a comment if face any doubt!! Happy Coding!!