In: Computer Science
Finish one part before you proceed to the next part. Turn in your source code, the content of each file (including the text files) and the snapshot of the execution results for each part.
Part One: Write a Python program, called myEncryption.py, which takes three inputs: (1) the name of a text file, called plain.txt, (2) a user input integer k in the range of (1-25) and (3) the name of an output file, called encrypted.txt. The program encrypts the file plain.txt using the Caesar cipher algorithm and outputs one encrypted file, called encypted.txt. The plain.txt is given.
Contents of plain.txt file:
usingatexteditorsuchasnotepad ortestedityoucancreateviewandsavedatain atxtfilesoyoushouldmakesuretochange thistoplaintextinyoureditorpreferencesifthatisthe caseyour pythonprogamshoudoutputdata toatextfileand readcodefromit done
Hint: The myEncryption.py program may have the following sketch of code. First, it defeins variables to take user input and open files (for read and write). Then it read the plain.txt file content line by line using the function readline(). You may need to use a while loop to control the number of loops to read the file content line by line. Your program also opens an output file for write. For each input character in a line read, it performs the encrypting operations and then writes the encrypted line characters to the output file.
"""Take user inputs. Open files, i.e., outFile and inputFile. """
cipherCode = "" #initialize the decrypted code
line = inputFile.readline().strip() # read the first line from the input file
while line!= "": # The While loop controls to read until the end of the input file
for ch in line:
""" Block of Python code to encrypt the character ch """
cipherCode += chr(cipherValue) # Append the cipher character for ch to cipherCode
outFile.write(cipherCode + "\n")
line = inputFile.readline().strip() # read next line from the input file
cipherCode = ""
""" Finally, close all the files. """
Part Two: Write a Python program, called myDecryption.py, which takes two inputs: (1) the encrypted file encrypted.txt from Part One, and (2) the same user input integer k in the range of (1-25) and decrypts the file encrypted.txt using the Caesar cipher algorithm and outputs one decrypted file, called producedPlain.txt.
Part Three: Compare the plain.txt (from Part One) and producedPlain.txt (from Part Two). Are those two text files identical? ______________________
Part One:
Program:
"""Take user inputs. Open files, i.e., outFile and inputFile. """ # taking 3 inputs nameTextFile = input("Enter name of text file: ") cipherValue = int(input("Enter integer in range(1-25): ")) outputFileName = input("Enter name of output file: ") # opening files inputFile = open(nameTextFile) outFile = open(outputFileName, 'w') cipherCode = "" # initialize the decrypted code line = inputFile.readline().strip() # read the first line from the input file while line != "": # The While loop controls to read until the end of the input file for ch in line: """ Block of Python code to encrypt the character ch """ cipherCode += chr(ord(ch)+cipherValue) # Append the cipher character for ch to cipherCode outFile.write(cipherCode + "\n") line = inputFile.readline().strip() # read next line from the input file cipherCode = "" """ Finally, close all the files. """ inputFile.close() outFile.close()
Code Snippet:
Output Snippet:
plain.txt:
encrypted.txt:
Part Two:
Program:
# taking inputs nameTextFile = input("Enter name of text file: ") cipherValue = int(input("Enter integer in range(1-25): ")) # opening encrypted input file inputFile = open(nameTextFile) # opening producedPlain.txt file outFile = open('producedPlain.txt', 'w') decryptedCode = "" # initialize the decrypted code line = inputFile.readline().strip() # read the first line from the input file while line != "": # The While loop controls to read until the end of the input file for ch in line: """ Block of Python code to decrypt the character ch """ decryptedCode += chr(ord(ch) - cipherValue) # Append the decrypted character outFile.write(decryptedCode + "\n") line = inputFile.readline().strip() # read next line from the input file decryptedCode = "" """ Finally, close all the files. """ inputFile.close() outFile.close()
Code Snippet:
Output Snippet:
plain.txt:
producedPlain.txt:
Part Three:
Ans: Yes. Those two text files are identical.
I hope you got the provided solution.
Thank You.