Please use python \"\"\" You must complete six (6) functions in this lab. You mu
ID: 3870566 • Letter: P
Question
Please use python
"""
You must complete six (6) functions in this lab. You must write your code in
each of these functions so that they perform as required. The details of these
functions are provided as comments in the definitions. READ THE INSTRUCTIONS
VERY CAREFULLY!!
Each function has a set of dummy return values that are there so the program
can run without any actual code written. You must replace these return values
with the appropriate ones from your code. The dummy values are also an
indication of the data types to be returned.
You are NOT allowed to import any other modules.
"""
def read_input():
"""
Read input should prompt the user for a message and read and input.
The function should then return three (3) variables:
1. A string/character for mode
2. A string with the key
3. A string with the message
Multiple variables can be returned by separating the variables with a
comma as demonstrated by the dummy return values.
"""
return "E", "175", "MESSAGE TO BE ENCRYPTED"
def caesarEncrypt(word, shift):
"""
Function should take a word and a shift and return the encrypted message
produced using the Caesar Cipher with the word and shift.
Parameter: word - a string of alphabet characters
Parameter: shift - the shift distance
Return: the encrypted string
"""
return "ENCRYPTED"
def caesarDecrypt(word, shift):
"""
Function should take a word and a shift and return the decrypted message
produced using the Caesar Cipher with the word and shift.
Parameter: word - a string of alphabet characters
Parameter: shift - the shift distance
Return: the decrypted string
"""
return "DECRYPTED"
def encrypt(message, key):
"""
This function will take a whole message and a key and encrypt the message
using the key.
Parameter: message - a string, the whole message to be encrypted
Parameter: key - a string, the encryption key
Return: the encrypted message
"""
return "ENCRYPTED MESSAGE"
def decrypt(message, key):
"""
This function will take a whole message and a key and decrypt the message
using the key.
Parameter: message - a string, the whole message to be decrypted
Parameter: key - a string, the encryption key
Return: the decrypted message
"""
return "DECRYPTED MESSAGE"
if __name__ == "__main__":
mode, key, message = read_input()
if mode == "E" or mode == "e":
print(encrypt(message, key))
elif mode == 'D' or mode == 'd':
print(decrypt(message, key))
else:
print("Invalid mode entered.")
Explanation / Answer
program.py------
def read_input():
mode = input(" Mode of operation : ")
key = input(" Key to be used : ")
message = input(" Message to be encrypt or decrypt : ")
return mode, key, message
def caesarEncrypt(word, shift):
"""
It is assumed that word can contain any character having ascii value between
0 and 255 and that's why mod operation is performed with 256
"""
string = ""
for i in range(len(word)):
string += chr((ord(word[i]) + int(shift)) % 256)
return string
def caesarDecrypt(word, shift):
"""
It is assumed that word can contain any character having ascii value between
0 and 255 and that's why mod operation is performed with 256
"""
string = ""
for i in word:
x = ord(i) - int(shift)
if x < 0:
x += 256
string += str(chr(x % 256))
return string
def encrypt(message, key):
return caesarEncrypt(message, key)
def decrypt(message, key):
return caesarDecrypt(message, key)
if __name__ == "__main__":
mode, key, message = read_input()
if mode == "E" or mode == "e":
print(encrypt(message, key))
elif mode == 'D' or mode == 'd':
print(decrypt(message, key))
else:
print("Invalid mode entered.")