Question: WHY AM I GETTING THIS ERROR IN PYTHON???? Here is the homework question: Encrypt The first program you will write should be named encrypt.py .
WHY AM I GETTING THIS ERROR IN PYTHON???? Here is the homework question:
Encrypt
The first program you will write should be named encrypt.py. The job of this program will be to encrypt, mix, the lines of a text file, but do it in such a way that it can be un-done later with a separate program, which you will also write).
When you run your program, your program will first request a file to to encrypt. Requesting the file name will look like:
| Enter the name of a file to encrypt: X |
The program will then run its encrypting algorithm on the file X. It will then save the encrypted version of the program to a file named encrypted.txt.
You might be wondering how the encryption works and how can I mix in such a way that it can be undone by another program?
Your program will mix an input file by randomly re-arranging the lines of the input file. For example, you might request that encrypt.py python file named get_credentials.py that looks like:
| #get the username from a prompt username = raw_input("Login: >> ") #list of allowed users user1 = "Luke" user2 = "Leia" #control that the user belongs to the list of allowed users if username == user1: print ("Access granted") elif username == user2: print ("Welcome to the system") else: print ("Access denied") |
You then run encrypt.py and request that it mix this file:
| Enter the name of a file to encrypt: get_credentials.py |
After encrypt.py has finished executing you encrypted.txt should look like:
| elif username == user2: if username == user1: username = raw_input("Login: >> ") user2 = "Leia" print ("Access granted") print ("Welcome to the system") user1 = "Luke" #list of allowed users else: #control that the user belongs to the list of allowed users #get the username from a prompt print ("Access denied") |
When encrypt.py runs, it will also write an index (key) file. This file contains the corresponding indices of each line in the encrypted file. Given the example above, index.txt should look like:
| 7 11 3 9 2 6 10 12 5 4 13 8 1 14 |
The number of each lines saves the line number that each shuffled line was in the original file. Without index.txt file, it would be very hard for any program or even a human to reassemble the program so it looks as expected. But with the index file, it isnt hard at all. Thus the file works like a secret key for the program. Whomever has access to both the index file and the encrypted text can unlock the encrypted text and other cannot.
Decrypt
After you finishing writing encrypt.py, you are to write a related program named decrypt.py. decrypt.py will take the name of a encrypted file and index (key) file, and then it will decrypt (unmix) the file. The program will read in these two files and using information stored within them and reconstruct the original file. The decrypted file will be saved to a file name decrypt.txt
Running decrypt.py will look like:
| Enter the name of a mixed text file: X Enter the mix index file: Y |
X is the name of the mixed file and Y is the name of the index file.
You need to use lists and dictionaries to figure out how to reassemble the code using the indices from the index file.
I will be providing different examples of expected outputs and files you can use to test your programs.
The Encryption Algorithm
Note that the below steps are just the steps for how to determine which lines to put where in the mixed file. You also need to handle all of the file reading, writing, etc. The steps of the algorithm are outlined below and you must implement these steps in python.
Import the random python library
Initialize the random library with a seed value of 123. Do this only once in your program.
However many lines there are in the input text file, repeat the following steps (line_count * 3) times
Choose two random integers starting from zero and going up to the number of lines in the file
Swap the content of the lines at these two indexes
By choosing the seed 123, you should get the same results if you follow the steps carefully.
The contents of these files must match what we expect exactly. In order to get the outputs to match, you will need to follow the same random mix
HERE IS MY CODE:
ENCRYPT:
import random random.seed(123)
fname=input("Enter the name of a file to encrypt: ") with open(fname) as f: content = f.readlines() content = [x.strip() for x in content]
lines=len(content) l=[] for j in range(lines): l.append(j) for i in range(3*lines): rand1=random.randint(0,lines-1) rand2=random.randint(0,lines-1) if rand1!=rand2: temp=content[rand1] content[rand1]=content[rand2] content[rand2]=temp temp=l[rand1] l[rand1]=l[rand2] l[rand2]=temp
with open('encrypted.txt', 'a') as the_file: for line in content: the_file.write(line+' ')
with open('index.txt', 'a') as the_file: for line in l: the_file.write(str(line)+' ')
My "get_credentials.py" is exactly what is shown above in the homework question so when python asks me for the user input and I type in "get_credentials.py" I get an error:
Enter the name of a file to encrypt: get_credentials.py Traceback (most recent call last): File "/Users/chloedillingham/Desktop/encrypt.py", line 10, in
WHY? please help, thank you!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
