CSc 110: Mix

This assignment should be written in the python programming language. You should only use python features that we have discussed up to this point. If you have questions about whether or not a particular feature can/should be used, ask the instructors! You may use any python version 3.0 or newer.

All of your programming should be well-formatted and easy for the graders to read and comprehend. Each program file should have a header comment at the top that has the following format:

#
# Author: Student Name
# Description:
#    A short description of what this program does!
# 

If any part of your scripts are particularly complex, you should put documentation comments above those lines of code.

The Assignment

This assignment exercises file reading and writing, random, lists, and dictionary use. You are going to write two separate programs which work together to encrypt (“mix-up”) and decrypt (“re-assemble”) python code.

Perhaps you’ve hear of the concept of “Encryption” in passing. In this assignment, we are going to be writing programs that are capable of encrypting and decrypting text files. The Wikipedia definition of Encryption goes like so:

In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. Encryption does not itself prevent interference, but denies the intelligible content to a would-be interceptor. In an encryption scheme, the intended information or message, referred to as plaintext, is encrypted using an encryption algorithm, generating ciphertext that can only be read if decrypted. For technical reasons, an encryption scheme usually uses a pseudo-random encryption key generated by an algorithm. It is in principle possible to decrypt the message without possessing the key, but, for a well-designed encryption scheme, considerable computational resources and skills are required. An authorized recipient can easily decrypt the message with the key provided by the originator to recipients but not to unauthorized users.

We are going to implement two programs (one to encrypt a text file, and one to decrypt).

Mixer

The first program you will write shall be named mixer.py. The job of this program will be to encrypt (“mix” or “jumble”) 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 run, your program will first request a file to encrypt. Requesting the file name will look like:

Enter a name of a python program to mix: X

This program will then run it’s encrypting (mixing) algorithm on the text file X. It will save the mixed version of the program to a file named encrypted.txt.

At this point you might be asking, how does the mixing work? 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 mixer.py encrypt a python file named get-credentials.py that looks like this:

#get the username from a prompt
username = raw_input("Login: >> ")

#list of allowed users
user1 = "Jack"
user2 = "Jill"

#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 mixer.py and request that it mix this file:

Enter a name of a python program to mix: get-credentials.py

After running, encrypted.txt will have the following contents:


    elif username == user2:

if username == user1:
username = raw_input("Login: >> ")
user2 = "Jill"
    print "Access granted"
        print "Welcome to the system"
user1 = "Jack"
#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"

Whenever mixer.py runs, it will also write an index (key) file. This file will contain the corresponding indexes of each line in the mixed file. Given the example above, index.txt would look like this:

7
11
3
9
2
6
10
12
5
4
13
8
1
14

The number of each line saves the line number that each shuffled line was on in the original program.

Without the index.txt file, it would be very hard for a program, or even a human to re-assemble the program so it works as expected. But with the index file, it isn’t hard at all. Thus the index file works like a secret “key” for the program. Whoever has access to both the index file and the mixed-up text can “unlock” the mixed text. Others cannot.

Unmixer

After writing mixer.py, you are to write a related program named unmixer.py. unmixer.py will take the name of a python file and index (key) file, and then it will decrypt (unmix) the test. The program will read in these two files, and using the information stored within them, it will re-construct the original text file. The un-mixed file will be saved to a file named decrypted.txt

Running unmixer.py will look like so:

Enter the name of a mixed text file: X
Enter the mix index file: Y

Where X is the name of the mixed file, and Y is the name of the index file.

You should use lists and dictionaries to figure out how to re-assemble the code using the indexes from the index file.

There are multiple examples of running this program on the diff-testing tool. Make sure to test your code carefully using the tool!

The mixing Algorithm

The diff-testing site has several example runs of mixer.py. The examples will include what we expect the encrypted (mixed) file and the index file should look like.

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 mixing algorithm that is used in the solution.

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.

By choosing the seed 123, you should get the same results if you follow the steps carefully.

Submission and grading

It is due on 7/24/2017 at 7:00pm.

You should put all of your code in two python files, named mixer.py and unmixer.py. Submit these files to the associated D2L dropbox. Following these turn-in and naming instructions closely is very important, because our grading scripts will depend on some of the details. You may lose points if these instructions are not followed precisely!