Question

In: Computer Science

Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and...

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):

Solutions

Expert Solution

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!!


Related Solutions

Problem 2: Caesar Cipher Decryption] Write a python method that takes two parameters: A parameter of...
Problem 2: Caesar Cipher Decryption] Write a python 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. Reverse the operations performed by the encryption method to obtain the plaintext message. The method’s header is as follows: def casesardecryption(s, key):
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
The mathematical expression of the encryption and decryption process of a Caesar cipher algorithm is expressed...
The mathematical expression of the encryption and decryption process of a Caesar cipher algorithm is expressed respectively as: c=Ep, k=p+k%26                                                                                                                         (1) p=Dc,k=c-k%26                                                                                                                         (2) Please do the following: Write at least two paragraphs to explain the principle of operation of the algorithm. For a full credit, your explanation must show the architectural diagram of the encryption and decryption process. Write a program to implement the Caesar algorithm Code must have two functions; encryption and decryption Test your codes with p as...
In cryptography, Caesar cipher is one of the simplest encryption techniques. The key idea of this...
In cryptography, Caesar cipher is one of the simplest encryption techniques. The key idea of this method is to replace each plaintext letter with one fixed number of places down the alphabet. Below is an example with a shift of three: Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz Cipher: DEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABC To cipher a string, ‘A’ is replaced by ‘D’, ‘B’ is substituted by ‘E’, and so on. To decode a string, ‘A’ is replaced by ‘x’, etc. By using python with vs code: Write a...
.. Write a method called findNums that takes a two-dimension array of integers as a parameter...
.. Write a method called findNums that takes a two-dimension array of integers as a parameter and returns the number of times a two-digit number appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 The value returned would be 5 (there are 5 two-digit numbers in the array) public class Question2 {    public static void main(String args[]){      int arr[][] = {{10, 45,...
Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
In an application write a method filterStack that takes a stack of integers as a parameter...
In an application write a method filterStack that takes a stack of integers as a parameter and filters its elements (in a new Stack) in a way that places the even elements at the bottom and the odd ones at the top. The original stack should remain unchanged. You should use a queue (only one queue) as a temporary storage. Use stack and queue operations only to solve this problem. No need to write the main method. For example, if...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT