In this assignment you will be writing several functions that process files and dictionaries to encrypt and decrypt messages. A dictionary will be used to create a random substitution cypher. Each letter of the alphabet will be mapped to another random letter in the alphabet. No letter will map or be mapped to any duplicate letter. For example given the partial dictionary below you can encrypt a message. Be sure to use the starter file provided so you can see the appropriate outputs. Be sure to download and place the appropriate text files in the same directory as your python file. You only need to submit your python file. Partial Dictionary: {d : a, e: f, h : b, l : p, l : e, o : j, r: m, w: i} originalMessage = “hello world” → encryptedMessage = “bfeej ijmea” Part 1 - Encrypt a Message: You will write 2 functions to build a dictionary and encrypt a message Function buildCypher() → dict: This function is to build a dictionary that can be used to create a random substitution cypher. Your goal is to assign a random mapping of each lower case letter of the alphabet to another letter. Return the dictionary created. *Hint all characters are mapped to an integer value from 97 - 122. See the ASCII chart linked here You can convert an integer to a character using the chr() function. But there are many ways to do this so find something that works for you. This random cipher should be different each time it is executed. Function encryptMessage(text: str, cypher: dict) → str: This function should convert all characters of parameter text to lowercase letters and then encrypt each character according to the cypher parameter. Each character in the text should be converted to the value based upon the key in the cypher parameter. If the character does not exist in the keys of the cypher it should not be encrypted. These characters will be spaces, quotes, new lines, etc. See the example of the partial dictionary above. The function should return a string of the text in encrypted form. Part2 - Decrypt a Message: You will write 2 functions to load a dictionary from a pickled file and decrypt a message using that dictionary. Function decryptMessage(text: str, cypher: dict) → str: This function will do the reverse of the encryption function above. It will go through each character of the text parameter which is an encrypted message. Each character is a value in the cypher dictionary and needs to be replaced by the associated key from the dictionary. It will only decrypt lower case letters and leave all other characters in their original form (spaces, quotes, etc) It will return a string representing the decrypted message. ** You’ll know when you have it correct. Function loadCypher(filename: str) → dict: This function will open a binary file with the passed filename parameter. It will load it’s contents and return the dictionary from the file cipher.py import pickle import random def openEncryptedMessage(filename="EncryptedMessage.txt"): """Loads the encrypted text file and returns its contents""" infile = open(filename) contents = infile.read() infile.close() return contents def buildCipher() -> dict: pass # Remove pass instruction and write your function definition here def encryptMessage(text: str, cipher: dict): pass # Remove pass instruction and write your function definition here def loadCipher(fileName: str): pass # Remove pass instruction and write your function definition here def decrypt(text: str, cipher: dict): pass # Remove pass instruction and write your function definition here def main(): """ Test Area Remove or add comments for functions to test """ print("TESTING PART 1") #### Remove comments to test buildCypher function #### # cipher = buildCipher() # print(cipher) # displays your cypher and should be different each time it is run #### Remove comments to test encryptMessage Function #### # message = "The Quick Brown Fox, Jumps Over the lazy Dog" # encryptedMessage = encryptMessage(message, cipher) # print(encryptedMessage) # Part 2 print("\nTesting Part 2") #### Remove Comments to test decrypt function #### # decryptedMessage = decrypt(encryptedMessage, cipher) # print(decryptedMessage) #### Remove comments to test loadCypher Function #### # new_cipher = loadCipher("cipher.dat") # print(decrypt(openEncryptedMessage(), new_cipher)) if __name__ == '__main__': main() cipher.dat
In this assignment you will be writing several functions that process files and dictionaries to encrypt
and decrypt messages. A dictionary will be used to create a random substitution cypher. Each letter
of the alphabet will be mapped to another random letter in the alphabet. No letter will map or be
mapped to any duplicate letter. For example given the partial dictionary below you can encrypt a
message.
Be sure to use the starter file provided so you can see the appropriate outputs. Be sure to download
and place the appropriate text files in the same directory as your python file. You only need to submit
your python file.
Partial Dictionary: {d : a, e: f, h : b, l : p, l : e, o : j, r: m, w: i}
originalMessage = “hello world” → encryptedMessage = “bfeej ijmea”
Part 1 - Encrypt a Message: You will write 2 functions to build a dictionary and encrypt a message
Function buildCypher() → dict:
This function is to build a dictionary that can be used to create a random substitution cypher. Your
goal is to assign a random mapping of each lower case letter of the alphabet to another letter.
Return the dictionary created. *Hint all characters are mapped to an integer value from 97 - 122.
See the ASCII chart linked here You can convert an integer to a character using the chr()
function. But there are many ways to do this so find something that works for you. This random
cipher should be different each time it is executed.
Function encryptMessage(text: str, cypher: dict) → str:
This function should convert all characters of parameter text to lowercase letters and then encrypt
each character according to the cypher parameter. Each character in the text should be
converted to the value based upon the key in the cypher parameter. If the character does not exist
in the keys of the cypher it should not be encrypted. These characters will be spaces, quotes,
new lines, etc. See the example of the partial dictionary above. The function should return a
string of the text in encrypted form.
Part2 - Decrypt a Message: You will write 2 functions to load a dictionary from a pickled file and
decrypt a message using that dictionary.
Function decryptMessage(text: str, cypher: dict) → str:
This function will do the reverse of the encryption function above. It will go through each character
of the text parameter which is an encrypted message. Each character is a value in the cypher
dictionary and needs to be replaced by the associated key from the dictionary. It will only decrypt
lower case letters and leave all other characters in their original form (spaces, quotes, etc) It will
return a string representing the decrypted message. ** You’ll know when you have it correct.
Function loadCypher(filename: str) → dict:
This function will open a binary file with the passed filename parameter. It will load it’s contents
and return the dictionary from the file
cipher.py
import pickle
import random
def openEncryptedMessage(filename="EncryptedMessage.txt"):
"""Loads the encrypted text file and returns its contents"""
infile = open(filename)
contents = infile.read()
infile.close()
return contents
def buildCipher() -> dict:
pass # Remove pass instruction and write your function definition here
def encryptMessage(text: str, cipher: dict):
pass # Remove pass instruction and write your function definition here
def loadCipher(fileName: str):
pass # Remove pass instruction and write your function definition here
def decrypt(text: str, cipher: dict):
pass # Remove pass instruction and write your function definition here
def main():
""" Test Area
Remove or add comments for functions to test
"""
print("TESTING PART 1")
#### Remove comments to test buildCypher function ####
# cipher = buildCipher()
# print(cipher) # displays your cypher and should be different each time it is run
#### Remove comments to test encryptMessage Function ####
# message = "The Quick Brown Fox, Jumps Over the lazy Dog"
# encryptedMessage = encryptMessage(message, cipher)
# print(encryptedMessage)
# Part 2
print("\nTesting Part 2")
#### Remove Comments to test decrypt function ####
# decryptedMessage = decrypt(encryptedMessage, cipher)
# print(decryptedMessage)
#### Remove comments to test loadCypher Function ####
# new_cipher = loadCipher("cipher.dat")
# print(decrypt(openEncryptedMessage(), new_cipher))
if __name__ == '__main__':
main()
cipher.dat
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 3 images