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 :)
.. 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 function pretty_print, which takes one parameter that can be any type of namedtuple. It...
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It "pretty prints" the contents of the namedtuple, including both the names of its fields and their values. This is subject to a few rules. Each field and its value are displayed on one line, with the name of the field appearing first, followed by a colon and a space, followed by the value of the field converted to a string. The fields should be...
Write a subroutine that takes an int (.long) parameter and squares it. The parameter should be...
Write a subroutine that takes an int (.long) parameter and squares it. The parameter should be passed in on the stack. The answer should be returned in eax. The subroutine should not disturb any registers except eax, ecx, and edx. (Save any registers on the stack and restore them before exiting the subroutine.) Upon entry to the subroutine, push ebp, etc., to access the parameter.
In the caeser cipher encryption and decryption program below, what do the two lines if(ch >...
In the caeser cipher encryption and decryption program below, what do the two lines if(ch > 'z'){ ch = ch - 'z' + 'a' - 1; } if(ch < 'a'){ ch = ch + 'z' - 'a' + 1; } mean??? I understand that it has something to do with ASCII characters and makes sure that if the encryption/decryption character is more than "z", then it would loop back to "a" instead of outputting a charcter like "{" . I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT