Description

In this assignment you can implement a simple encoder-decoder program that uses a Caesar cipher. A Caesar cipher is based on shi"ing a code for each letter by some number. For example, let's choose a shi" that equals to 3 and a le#er from the English alphabet that is A, then the encoded symbol is a le#er that follows A and is located at the position that is shifted from A by 3, so it is D. D is the fourth letter in the alphabet, whereas A is the first le#er, and the distance between them is 3. In this encoding we use the right shift, so A becomes D, B becomes E, C becomes F, and so on. It is also easy to decode the cipher because we can use the letter shift to reverse the encoding, so F becomes C, E becomes B, and D becomes A. The only problem is how to encode the last le#ers in the alphabet because they are not followed by any other le#ers. This problem can be easily solved if we assume that the letters in the alphabet are arranged in a circle, so Z is followed by A. Then using the right shift 3, letter Z becomes C, le#er Y becomes B, and letter X becomes A.

Here are the snippets of the program output, note that your program output should match this format exactly. In the beginning your program should ask the user to choose from the menu one of the following opera!ons: encode, decode, or quit in the following way:

Would you like to encode or decode the message?
Type E to encode, D to decode, or Q to quit:

If the user types 'e' or d, then the program asks the user to enter a filename for reading:

Please enter a file for reading:

Assume that the user has already created a file named message.txt that has the following content "Hello! How are you?", and the file is located in the same directory as the main program cipher.py. When the user types the filename message.txt and hits 'Enter', the program asks the user to enter a new filename for wri!ng:

Please enter a file for writing:

Assume that the user enters the filename code.txt. When the user enters the filename, the program produces the following output:

Plaintext:
HELLO! HOW ARE YOU?

Ciphertext:
KHOOR! KRZ DUH BRX?

Would you like to encode or decode the message?
Type E to encode, D to decode, or Q to quit:

Please note that the program reprints the same prompt after wri!ng plaintext and ciphertext. Also, it writes the ciphertext to the file previously chosen by the user. You should check the content of the selected file code.txt.

If the user previously selected 'd' and entered files for reading (code.txt) and wri!ng (plain.txt), then the output is slightly different, ciphertext is printed before plaintext:

Ciphertext:
KHOOR! KRZ DUH BRX?

Plaintext:
HELLO! HOW ARE YOU?

Would you like to encode or decode the message?
Type E to encode, D to decode, or Q to quit:

The program also writes the plaintext to the file previously chosen by the user. You should check the content of the selected file plain.txt. If the user enters 'q' from the menu, then the program prints the following message and terminates:

Goodbye!

If the user does not enter a valid input the program could reprint the prompts or write a message that notifies about the wrong input (This implementation is optional, your program will not be checked for invalid input).

Remember that first you have to create a text file (for example, message.txt) that will be used for reading. The file should be located in the same directory as the main program cipher.py. A file that will be used for writing (for example, code.txt or plain.txt) does not have to be created in advance, it can be created when you open it for writing.

Programming Approaches

Please, read the following code carefully. You can copy it to the Python IDLE or other editor and add the missing functions and the main program. Some functions are already implemented. As always write a header in the beginning of the program:

# read text from a file and return text as a string
# the function should contain an input statement, open file statement,
# and try-except statement
def readfile():
write your code here
return message

# write a string (message) to a file
# the function should contain an input statement, open file statement,
# and try-except statement
def writefile(message):
write your code here
# make a list (tuple) of letters in the English alphabet
def make_alphabet():
alphabet = ()
for i in range(26):
char = i + 65
alphabet += (chr(char),)
#print (alphabet)
return alphabet

# encode text letter by letter using a Caesar cipher
# return a list of encoded symbols
def encode(plaintext):
plaintext = plaintext.upper()
shift = 3
ciphertext = []
alphabet = make_alphabet()
length = len(alphabet)
for char in plaintext:
found = False
for i in range(length):
if char == alphabet[i]:
letter = alphabet[(i + shift) % leng
th]
ciphertext.append(letter)
found = True
break
if not found:
ciphertext.append(char)
return ciphertext

# decode text letter by letter using a Caesar cipher
# return a list of decoded symbols
# check how the function encode() is implemented
# your implementation of the function decode() can be very similar
# to the implementation of the function encode()
def decode(text):
shift = -3
plaintext = []
write your code here
return plaintext

# convert a list into a string
# for example, the list ["A", "B", "C"] to the string "ABC" or
# the list ["H", "O", "W", " ", "A", "R", "E", " ", "Y", "O", "U", "?"] to the string "HOW ARE YOU?"
def to_string(text):
s = ""
write your code here
return s

# main program
write your code here

Afer you implement your cipher.py you can decode the following message:

ZRXOG BRX WHOO PH, SOHDVH, ZKLFK ZDB L RXJKW WR JR IURP KHUH?""WKDW GHSHQGV D JRG GHDO RQ ZKHUH BRX ZDQW WR JHW WR," VDLG WKH FDW."L GRQ'W PXFK FDUH ZKHUH -" VDLG DOLFH."WKHQ LW GRHVQ'W PDWWHU ZKLFK ZDB BRX JR," VDLG WKH FDW."- VR ORQJ DV L JHW VRPHZKHUH," DOLFH DGGHG DV DQ HASODQDWLRQ."RK, BRX'UH VXUH WR GR WKDW," VDLG WKH FDW, "LI BRX RQOB ZDON ORQJ HQRXJK.

This message is written in the file secret.txt.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.