Assignment - Encrypting and Decrypting Text Files
Assignment - Encrypting and Decrypting Text Files
Assignment Objectives
● Use Python to develop code that encrypts and decrypts plain text
● Practice command line arguments
● Read input from a file and write output to another file
Problem Description
In order to defeat a simple letter frequency analysis1, the Magic-V cipher encodes a letter into
one of several cipher letters, depending on its position in the input document. Choose a keyword,
for example MAGIC. Then encode the first letter of the input text like this:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
M N O P Q R S T U V W X Y Z A B C D E F G H I J K L
The encoded alphabet is just the regular alphabet shifted to start at ‘M’, the first letter of the
keyword MAGIC. The second letter is encrypted according to the following map (since we are at
letter ‘A’, the mapping of each alphabet is the same):
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1
You can read more about frequency analysis here: https://en.wikipedia.org/wiki/Frequency_analysis
The third letter, ‘G’:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
G H I J K L M N O P Q R S T U V W X Y Z A B C D E F
The fourth, and fifth letters in the input text are encrypted using the alphabet sequences
beginning with characters I, and C, and so on. Because the key is only five letters long, the sixth
letter of the input text is encrypted in the same way as the first (i.e., ‘M’).
Write a program that encrypts or decrypts a text file using this cipher and write the result
to an output file. You should allow the users of your program to supply arguments to
indicate if they are encrypting or decrypting, the cipher keyword, and to specify the input
and output files.
For example,
decrypts the file myInFile.txt using the keyword WONDERFUL and writes the result to the file
myOutput.txt. (Using ‘-e’ instead of ‘-d’ indicates the user wants to encrypt the input file). It is
an error not to supply all necessary arguments.