This assignment exercises file reading and writing, random, and use of lists.
You are going to write two separate programs which work together to encrypt (“mix-up”) and decrypt (“re-assemble”) .txt
files, .py
files, and other text-based files.
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).
The first program you will write shall be named encrypter.py
.
The job of this program will be to encrypt (“mix” or “shuffle”) 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 text file to encrypt:
This program will then run it’s encrypting (mixing) algorithm on the text file named file_name.
It will save the encrypted version of the text file to a file named encrypted.txt
.
Your program will encrypt an input file by re-arranging the lines of the input file, based on indexes retrieved from calling randint
.
For example, the user decide to encrypt a file named sample.txt
that looks like this:
|'''''|
| 0 0 |
| ^ |
| - |
-----
#
#######
###
# #
% %
You then run
If the user were to use encrypter.py
to encrypt the file, the prompt and input should look like so:
Enter a name of a text file to encrypt:
sample.txt
After running, encrypted.txt
will have the following contents:
#######
| 0 0 |
# #
| ^ |
###
% %
#
|'''''|
| - |
-----
All of the same lines from the original file exist in this file, but the order is not necessarily the same.
Whenever encrypter.py
runs, it will also write an index (key) file.
This file will contain the corresponding indexes of each line in the encrypted file.
Given the example above, index.txt
would look like this:
7
2
9
3
8
10
6
1
4
5
The number on each line of the file is the line number that each shuffled line was on in the original program.
For example: 7
is on the first line.
This means that the first line of the encrypted file was originally on line 7 of the input file.
2
is on the second line.
This means that the second line of the encrypted file was originally on line 2 of the input file (the same line!).
Without the index.txt
file, how would another program or a human know how to decrypt the encrypted.txt
file.
If a human or program has both the encrypted file and the index file, it can be decrypted systematically.
Thus the index file works like a secret “key” for the program.
Whoever has access to both the index file and the encrypted text can decrypt the encrypted text.
After writing encrypter.py
, you are to write a related program named decrypter.py
.
decrypter.py
will take the name of a text file and index (key) file, and then it will decrypt the text.
The program will read in these two files, and using the information stored within them, it will put the contents back in the original order.
The decrypted file should be saved to a file named decrypted.txt
Running decrypter.py
will look like so:
Enter the name of an encrypted text file:
file_name_a
Enter the name of the encryption index file:
file_name_b
Where file_name_a
is the name of the encrypted file, and file_name_b
is the name of the index file.
You should use lists and the indexes from the index file to help with getting the lines back in the correct order.
There will be multiple examples of running this program on the diff-testing tool. Make sure to test your code carefully using the tool!
The diff-testing site has several example runs of encrypter.py
.
The examples will include what we expect the encrypted 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 encryption algorithm that is shown below.
The steps of the algorithm are outlined below and you must implement these steps in python. Note that the below steps are just the steps for how to determine which lines to put where in the encrypted file. You also need to handle all of the file reading, writing, etc.
125
.
(Just put the line of code random.seed(125)
at the beginning of main).By choosing the seed 125
, you should get the same results if you follow the steps carefully.
Below are suggestions for the sequence steps for both of the programs:
random.seed(125)
encrypted.txt
.
Also, write the shuffled indexes from the index list to index.txt
.
Make sure to close both files!decrypted.txt
.For your reference, my solution to encrypter.py
is around 43 lines of code, not including comments, and my solution for decrypter.py
is around 26 lines of code (not including comments).
This information is meant to be encouraging, and keep you on the correct track.
The code to be written is not very long.
However, getting the encryption loop and decryption loop correct might be tricky, so start early!
It is due on Tuesday, 11/5/2019 at 7:00pm.
You should put all of your code in two python files, named encrypter.py
and decrypter.py
.
You only nede to upload these two files to gradescope for this PA.
Submit these files to Gradescope, and try your best to write your programs in such a way that they pass every test case.