Cryptography is the science of hiding or securing information. In cryptography, encryption is the process of rendering information unreadable using an algorithm; decryption is the reversal of the encryption process using a key. In this project, you will create a program which both encrypts and decrypts messages using either a substitution cipher or a Caesar cipher.The Substitution Cipher A substitution cipher maps each letter of the alphabet to another (possibly different, possibly not) letter of the alphabet. We will work with just lower-case English letters, so our alphabet will be abcdefghijklmnopqrstuvwxyz. The key will be some rearrangement of these letters such as qwertyuiopasdfghjklzxcvbnm. Using this particular key, we would map a to q, b to w, and so on, following a one-to-one correspondence between the alphabet and the key. In general, if a letter occurs at position i in the alphabet it will be replaced by the letter in position i in the key. If a letter from the alphabet is not listed in the key, then it remains the same. For example, capital letters, punctuation, and digits are not in the example key, so those characters in the alphabet would remain the same. To encrypt a message, you replace each letter in the message with the letter that occupies the corresponding position in the key. To decrypt, find the position of the encrypted letter in the key and replace it with the letter in the corresponding position in the alphabet. Some examples: alphabet = abcdefghijklmnopqrstuvwxyz key = qwertyuiopasdfghjklzxcvbnm Message: Attack at dawn! Resulting encryption: Azzqea qz rqvf! Message: this is INFO 103 Resulting encryption: ziol ol INFO 103 Message: substitute me for him Resulting encryption: lxwlzozxzt dt ygk iod The Caesar Cipher The Caesar cipher is a shift-substitution cipher. For this cipher, the key is a number indicating a shift. To encrypt your message, you replace the original letter in the message with the letter (shift mod 26) characters ahead of it, where the shift is an integer greater than zero. Since the alphabet is abcdefghijklmnopqrstuvwxyz, the letter at alphabet[25] would be z. The position of the letter 2 characters ahead of z is found using the formula alphabet[ (25 + 2) % 26], thus wrapping around the alphabet to the letter b. If you simply ask for alphabet[25 + 2], you will get a string index out of range error. To decrypt, the encrypted letter is replaced with the letter shift mod 26 letters behind it. For convenience, we will only deal with shifting lower case letters, keeping capital letters, punctuation, digits, and spacing the same. Some examples: Message: attack at dawn Shift: 3 Resulting encryption: dwwdfn dw gdzq Message: this is info 103 Shift: 1 Resulting encryption: uijt jt jogp 103 Message: were doing a shift-substitution! Shift: 27 Resulting encryption: xfsf epjoh b tijgu-tvctujuvujpo! For a more detailed explanation, check out Wikipedias description of the Caesar Cipher. Input and Ouput Files You will write an encryption/decryption program that reads messages from a text file and writes output to a text file. The text file containing the original, unencrypted messages will be named message.txt. The Substitution encoding of message.txt will be named Sencoded.txt and the substitution decoding will be named Sdecoded.txt. The Caesar encoding will be namedCencoded.txt and the decoding will be named Cdecoded.txt. The Sencoded.txt and Cencoded.txt files can provide the encrypted messages used as input for the decrypting process. Strings, Characters, and Lists The text files containing the messages to be encoded or decoded will be read in using the readlines function, which returns a list of strings. Some examples: Original text: Soybeans are a great source of protein. readlines() returns: [Soybeans are a great source of protein.] Original text: Soybeans are a great source of protein.And theyre delicious! readlines() returns: [Soybeans are a great source of protein. , And theyre delicious!] Some important facts about strings: The characters in a string are indexed starting at 0, just like a list. This means you can access the characters in a string just like you would elements in a list. For example:
mystring = lrxqprint mystring[1]
will print out the character r . You can put two strings together using the + operator. For example:
mystring = lrx + q
will assign lrxq to the variable mystring . Pasting together two strings like this is referred to as concatenation. You can test whether two strings are equal to each other using the boolean operator ==, and whether they are not equal using !=. For example:
print A == A
will print out True
print A != A will print out False You can find out how many characters are in a string by using the len() function. For example:
mystring = lrxqprint len(mystring)
will print out 4. Chapter 7 has a much more complete description of how strings are manipulated; you should review those sections and the related course notes. Some important facts about characters: Remember what ASCII is? Since the message will only be in lower case letters, only characters with ASCII values between 97 and 122, inclusive, will need to be shifted. Any character outside of the range should be kept the same. Heres an ASCII table for reference If you want to get the ASCII value of a string, use the ord() function. If you want to get a character for an ASCII value, use the chr() function. For example:
print ord(a)will print out the integer 97.
print chr(97)will print out the string a. Finally, suppose we want to access a specific character in a string in a list. We can do this using the [] operator twice. For example:
mylist = [dtoo , lrxq , hack , edoc ]print mylist[1][2] Would print out x. In general, using mylist[i][j] will access character j in string i of the list. Accessing an element in a list of lists is accomplished in the same way. For example:
mylist = [[0,1,2], [3, 4], [8,10,6,7] ]print mylist[1][0] will print out 3. Project Tasks Download the message.txt, Sencoded.txt, and Cencoded.txt files you can use for testing. Next download the cipher.py file in which you will write your code. Make sure all the files are in the same directory. We have provided some code for you and indicated what code you should write in the comments. In general, you should write the main block of any project first. This code should be short and call functions to accomplish its task. The main block of code is at the end of the cipher.py file, and test1 has been provided to get you started. Heres an overview of the steps to take to complete the project: 1. Write the function file_input(filename)This function takes the following parameter:
filename: a string containing the name of the input file that holds the text message This function should do the following: 2. Open the input file for reading 3. Use the readlines function to read the input file into a list of strings 4. close the input file 5. return the list of strings Be sure to test the function by calling it in the main program and printing the returned list of strings. 2. Write the function file_output(filename, outStrings)This function takes the following parameters: filename: a string containing the name of the output file to which the encrypted or decrypted strings will be written outStrings: a list of strings that has been encrypted or decrypted This function should do the following: 2. Open the output file for writing 3. Use the writelines function to write the list of strings to the output file 4. close the output file 3. Complete the function substitute_encrypt(lines, key)This function takes the following parameters:
lines: a list of strings to be encrypted key: a string that is the key to the cipherThe function returns a list of encrypted strings. The function should loop through every string in lines and for every character in the string it should generate an encrypted character. The function should build up a list of encrypted strings and return them. 4. Write the function substitute_decrypt(lines, key)This function takes the following parameters:
lines: a list of strings to be decrypted key: a string that is the key to the cipher This function should look a lot like substitute_encrypt, only it should reverse the process. 5. Write the function caesar_encrypt(lines, shift)This function takes the following parameters:
lines: a list of strings to be encrypted shift: an integer that indicates the number to shift by This function should look similar to substitute_encrypt but should use the Caesar encryption method instead. Make sure to use mod 26 for encrypting to wrap around the alphabet. 6. Write the function caesar_decrypt(lines, shift)This function takes the following parameters:
lines: a list of strings to be encrypted shift: an integer that indicates the number to shift by This function should look a lot like caesar_encrypt, only it should reverse the process. Write the main part of the file that calls the functions that have been declared in steps 3-6. 2. Initiate the key variable as qwertyuiopasdfghjklzxcvbnm for the Substitution cipher and use the integer value 2 as the key (shift) for the Caesar Cipher. 3. Call the file_input function to get the strings from the input file. (ex. Message.txt for substitution_encrypt) 4. Call one of the functions substitution_encrypt, substitution_decrypt, caesar_encrypt, or caesar_decrypt with the list of strings returned from step 2 and the key from step 1 as the arguments. Save the returned list of strings in a variable called outStrings. 5. Call the file_output function to save the encrypted/decrypted strings to an output file. (ex. Sencoded.txt for substitution_encrypt) 7. Test your functions by running cipherTests.py (put it in the same directory as your files).
Reviews
There are no reviews yet.