Question

MINDTAP From Cengage Programming Exercise 4.3 decrypt.pyencrypt.py Instructions 1Modify the progran below Modify the scripts of Projects 1 and 2 to encrypt and decrypt entire files of text. 4 Probject 4.1 5 6 Encypts an input string of the ASCII characters and prints 7 the result. The other input is the distance value An example of the program interface is shown below: Enter the input file nane: encrypted.txt Enter the output file nane: a Enter the distance value: 3 9 10 # The ASCII values range from through 127 12 plainText input(Enter a nessage: 13 distance-int(input(Enter the distance value: )) 14 code 15 for ch in plainText 16 ordvalue = ord(ch) 17 18 if cipherValue> 127: 19 20 codechr(cipherValue) 21 print (code) Grading As you complete the instructions above you can use the Test button to check if the lab tests are passing. Once you are satisfied with the results, use the Grade button to save your score cipherValue ordValue distance cipherValue -distance (127-ordValue 1)Modify the scripts of Projects 1 and 2 to encrypt and decrypt entire files of text. An example of the program interface is shown below:

both images are from one question. I need it in python, please.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Please indent the code as per the screenshots.

Code Screenshots (encrypt.py):

#Prompt the user to enter #the name of the input file infileName input (Enter the input file name: ) #Prompt the user to enter #the name of the output file. outFileNameinput (Enter the output file name: ) #Prompt the user to enter #the distance value. distance int (input (Enter the distance value: )) #Open the input file to be read. inFileopen (infileName,r) #Open the output file. outFile open (outFileName, w) code #Read the data in the file haracter by character. for line in inFile.readlines ): flag 0 if An in line: flag 1 line - line.stripn) for ch in line: oravalue ord (ch) ciphervalue ordvalue + distance if cipherValue > 127: cipherValuedistance - (127 - ordvalue 1) code chr (cipherValue) if flag-1: code += \n #Write the data #to the file. outFile.write (code)

Sample Input File (encrypted.txt):

Sample Output:

Sample Output File(out.txt):

Code To Copy (encrypt.py):

#Prompt the user to enter
#the name of the input file.
infileName = input("Enter the input file name: ")

#Prompt the user to enter
#the name of the output file.
outFileName = input("Enter the output file name: ")

#Prompt the user to enter
#the distance value.
distance = int(input("Enter the distance value: "))

#Open the input file to be read.
inFile = open(infileName,"r")

#Open the output file.
outFile = open(outFileName,"w")
code = ""

#Read the data in the file
#character by character.
for line in inFile.readlines():
flag = 0
if '\n' in line:
flag = 1
line = line.strip('\n')
for ch in line:
ordValue = ord(ch)
cipherValue = ordValue + distance
if cipherValue > 127:
cipherValue = distance - (127 - ordValue + 1)
code += chr(cipherValue)
if flag == 1:
code += '\n'

#Write the data
#to the file.
outFile.write(code)

#Close the files.
inFile.close()
outFile.close()

Code Screenshots (decrypt.py):

Sample input file (out.txt):

Sample Output:

Sample Output file (decry.txt):

Code To Copy (decrypt.py):

#Define the function to
#decode the cipher text.
def decode_ceasor(encrypted_text, distance):
output = ""
for i in encrypted_text:
x = ord(i) - (distance % 26)
if i.isalpha():
if i.isupper():
if x > ord('Z'):
x = x - 26
elif x < ord('A'):
x = x + 26
output += chr(x)
else:
output += chr(x)
return output

#Prompt the user to enter
#the name of the input file.
infileName = input("Enter the input file name: ")

#Prompt the user to enter
#the name of the output file.
outFileName = input("Enter the output file name: ")

#Prompt the user to enter
#the distance value.
distance = int(input("Enter the distance value: "))

#Open the input file to be read.
inFile = open(infileName,"r")

#Open the output file.
outFile = open(outFileName,"w")

decrypted_text = ""

for encrypted_text in inFile:
flag = 0
if '\n' in encrypted_text:

flag = 1
  
#Read the text from the file.
encrypted_text = encrypted_text.strip('\n')
decrypted_text += decode_ceasor(encrypted_text,distance)
if flag == 1:
decrypted_text += '\n'
  

#Call the function decode_ceaser()
# and write the decoded
#text into the output file.
outFile.write(decrypted_text)

#Close the file.
outFile.close()
inFile.close()

Add a comment
Know the answer?
Add Answer to:
Modify the scripts of Projects 1 and 2 to encrypt and decrypt entire files of text....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Opening Files and Performing File Input 1 V/ Flowers.cpp This program reads nanes of flowers and ...

    Opening Files and Performing File Input in C++ Opening Files and Performing File Input 1 V/ Flowers.cpp This program reads nanes of flowers and whether they are grown in shade or sun from an input 2 file and prints the information to the user's screen. 3 Input: flowers.dat 41 Output: Names of flowers and the words sun or shade Summary In this lab, you open a file and read input from that file in a prewritten C++ program. The program...

  • 1. Write a C++ program called Password that handles encrypting a password. 2. The program must...

    1. Write a C++ program called Password that handles encrypting a password. 2. The program must perform encryption as follows: a. main method i. Ask the user for a password. ii. Sends the password to a boolean function called isValidPassword to check validity. 1. Returns true if password is at least 8 characters long 2. Returns false if it is not at least 8 characters long iii. If isValidPassword functions returns false 1. Print the following error message “The password...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • For milestone #1, we will start the CARIT site with three static HTML pages and a...

    For milestone #1, we will start the CARIT site with three static HTML pages and a CSS file. Create a dedicated folder for this project. This folder should contain all related files in this project. The future milestones are cumulative and built directly on top of your prior work. Function/content requirements: A home page named “index.html”, which include these contents at least: Description of the center. You may reference the example sites. Latest news: use list tags; make up some...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT