Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Encrypt The first program you will write should be named encrypt.py. The job of

ID: 3914301 • Letter: E

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 : >> ") #11st of allowed users user1"Luke" user2 "Leia" #control that the user belongs to the list of allowed users if usernameuser1: print ("Access granted") print ("Welcome to the system") print ("Access denied") elif usernameuser2: else: You then run encrypt.py and request that it mix this file Enter the name of a file to encrypt: get-credentials. py

Explanation / Answer

------------------encrypt.py---------------------

import random

random.seed(123)

filename = raw_input("Enter the name of a file to encrypt: ")

file = open(filename)

lines = file.readlines()

file.close()

numOfLines = len(lines)

numSteps = len(lines)*3

for i in xrange(numOfLines):

lines[i] = (lines[i],i+1)

if(not lines[-1][0].endswith(" ")):

lines[-1] = (lines[-1][0]+" ",lines[-1][1])

for i in xrange(numSteps):

r1 = random.randint(0,numOfLines-1)

r2 = random.randint(0,numOfLines-1)

lines[r1],lines[r2] = lines[r2],lines[r1]

file2 = open("encrypted.txt","w")

for line in lines:

file2.write(line[0])

file2.close()

file3= open("mix.txt","w")

for line in lines:

file3.write(str(line[1]) + " ")

file3.close()

------------------decrypt.py---------------------

textFileName = raw_input("Enter the name of a mixed text file: ")

indexFileName = raw_input("Enter the mix index file: ")

file = open(textFileName)

lines = file.readlines()

file.close()

numLines = len(lines)

decrypted = [""]*numLines

indexFile = open(indexFileName)

i = 0

for line in indexFile:

num = int(line.strip())

decrypted[num-1] = lines[i]

i+=1

file2 = open("decrypted.txt","w")

for line in decrypted:

file2.write(line)

file2.close()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote