Question

In: Computer Science

Finish one part before you proceed to the next part. Turn in your source code, the...

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? ______________________

Solutions

Expert Solution

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.


Related Solutions

* Make sure you turn in your code (take a screen shot of your code in...
* Make sure you turn in your code (take a screen shot of your code in R)and answers. Conduct the hypothesis and solve questions by using R. 2) A random sample of 12 graduates of a secretarial school averaged 73.2 words per minute with a standard deviation of 7.9 words per minute on a typing test. What can we conclude, at the .05 level, regarding the claim that secretaries at this school average less than 75 words per minute on...
WRITE CODE IN JAVA it is now your turn to create a program of your choosing....
WRITE CODE IN JAVA it is now your turn to create a program of your choosing. If you are not sure where to begin, think of a task that you repeat often in your major that would benefit from a program. For example, chemistry unit conversions, finding the area for geometric shapes, etc. You can also create an interactive story that changes based on the user input/decisions. The possibilities are endless. The program must include instructions for the user. Be...
Do the following using R. You must also turn in a copy of your R code....
Do the following using R. You must also turn in a copy of your R code. (10) What is the probability a beta (1, 8) random variable is less than 0.13? (11) What is the probability a beta (3, 9) random variable is greater than .4? (12) What is the probability a beta (18,4.4) random variable is between 0.6 and 0.7? (13) At what value of x is the probability that a beta (4, 7) random varable is less than...
You are to use your code from part A of this assignment and add to it....
You are to use your code from part A of this assignment and add to it. In this part of the project, you are to do the following: 1. In the count_voters function you are to add code to: • Count the number of votes for Trump • Count the number of votes for Biden • Count the number of females • Count the number of males • Count the number of Democrats • Count the number of Republicans •...
Part 1- A) Design a circuit that utilizes one switch to turn on 2 motors and...
Part 1- A) Design a circuit that utilizes one switch to turn on 2 motors and another switch to turn off both motors. Pressing and releasing PB1 will turn on motor 1 immediately and motor 2 after a 3 second delay. Pressing and releasing PB2 will turn off both motors. B) Next, design a circuit that utilizes 1 switch to turn on 3 motors. Pressing and releasing PB 1 will turn on motor 1 immediately; motor 2 will turn on...
Source code with comments explaining your code in C# Program 2: Buh-RING IT! For this assignment,...
Source code with comments explaining your code in C# Program 2: Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack. When the player attacks, it will pick a random number between 0 and the...
You may use your programming of choice to implement and simulate. Please turn in the code, simulation, and a description of what is going on in the simulation.
You may use your programming of choice to implement and simulate. Please turn in the code, simulation, and a description of what is going on in the simulation.
Your hardcopy submission will consist of these two things: Source code (your java file). Screenshot of...
Your hardcopy submission will consist of these two things: Source code (your java file). Screenshot of Eclipse showing output of program. (JPG or PNG format only) Write a program to play the game of Stud Black Jack. This is like regular Black Jack except each player gets 2 cards only and cannot draw additional cards. Create an array of 52 integers. Initialize the array with the values 0-51 (each value representing a card in a deck of cards) in your...
Write a program that solves the Knapsack problem. Code to the following standards. Your source of...
Write a program that solves the Knapsack problem. Code to the following standards. Your source of items to put into the knapsack should consist of five randomly generated integers in the range of 1 to 20. Your knapsack can hold 20 lbs.
Explain in your own words the “power of a statistical test”. Do you proceed with the...
Explain in your own words the “power of a statistical test”. Do you proceed with the test if you don’t have enough power”? How does the “power” affect the results of the test?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT