Substitution Encryption This assignment is adapted from a problem in pset 2 from a popular Harvard's course CS50. The original assignment was a C program but we are implementing the functionality with...

1 answer below »
It a code not an essay


Substitution Encryption This assignment is adapted from a problem in pset 2 from a popular Harvard's course CS50. The original assignment was a C program but we are implementing the functionality with Python. Write a python program that implements a substitution cipher, per the below. Background (Links to an external site.) In a substitution cipher, we “encrypt” (i.e., conceal in a reversible way) a message by replacing every letter with another letter. To do so, we use a key: in this case, a mapping of each of the letters of the alphabet to the letter it should correspond to when we encrypt it. To “decrypt” the message, the receiver of the message would need to know the key, so that they can reverse the process: translating the encrypt text (generally called ciphertext) back into the original message (generally called plaintext). A key, for example, might be the string NQXPOMAFTRHLZGECYJIUWSKDVB. This 26-character key means that A (the first letter of the alphabet) should be converted into N (the first character of the key), B (the second letter of the alphabet) should be converted into Q (the second character of the key), and so forth. A message like HELLO, then, would be encrypted as FOLLE, replacing each of the letters according to the mapping determined by the key. Let’s write a program called hw6.py that enables you to encrypt messages using a substitution cipher.  Similar to other python projects, to run your program you will need to do: ~/cmpsc131python-userid$ cd hw6 ~/cmpsc131python-userid/hw6$ python3 -i hw6.py Here are a few examples of how the program might work. For example, if the user inputs a key of YTNSHKVEFXRBAUQZCLWDMIPGJO and a plaintext of HELLO: $ python3 hw6.py Enter a 26 letter key: YTNSHKVEFXRBAUQZCLWDMIPGJO Plain Text: HELLO Cipher Text: EHBBQ Here’s how the program might work if the user provides a key of VCHPRZGJNTLSKFBDQWAXEUYMOI and a plaintext of hello, world: $ python3 hw6.py Enter a 26 letter key: VCHPRZGJNTLSKFBDQWAXEUYMOI Plain Text: hello, world Cipher Text: jrssb, ybwsp $ python3 hw6.py Enter a 26 letter key: fgRzPMBkJAOWYDieQhNStcULxv Plain Text: I hope you find CMPSC131 a fun class, where you learn how to program in Python and C; where you learn to use version control system like git and GitHub and where you are having lots of fun! Cipher Text: J kiep xit mjdz RYENR131 f mtd rwfnn, ukphp xit wpfhd kiu si ehibhfy jd Exskid fdz R; ukphp xit wpfhd si tnp cphnjid ridshiw nxnspy wjop bjs fdz BjsKtg fdz ukphp xit fhp kfcjdb wisn im mtd! Notice that neither the comma nor the space were substituted by the cipher. Only substitute alphabetical characters! Notice, too, that the case of the original message has been preserved. Lowercase letters remain lowercase, and uppercase letters remain uppercase. Whether the characters in the key itself are uppercase or lowercase doesn’t matter. A key of VCHPRZGJNTLSKFBDQWAXEUYMOI is functionally identical to a key of vchprzgjntlskfbdqwaxeuymoi (as is, for that matter, VcHpRzGjNtLsKfBdQwAxEuYmOi). And what if a user doesn’t provide a valid key? Either the key does not have 26 letters or the key is missing one of the letter in the alphabet a-z. $ python3 hw6.py Enter a 26 letter key: abcdefg Invalid key. $ python3 hw6.py Enter a 26 letter key: VcHpRzGjNtLsKfBdQwAxEuYmoZ Invalid key. Specification (Links to an external site.) Design and implement a python program, hw6.py, that encrypts messages using a substitution cipher. · Implement your program in a file called hw6.py in a hw6/ directory after you do an update to your python repo. · The run() function is implemented for you already that will get user to enter a key, call a function you implemented to check the validity of the key and proceed to get plainText, encrypt with the key and print out the cipherText. · The isValidKey() function will check If the key is invalid (as by not containing 26 characters, containing any character that is not an alphabetic character, or not containing each letter exactly once) and return False if it is not valid and return True if it is valid. · Your substitution() function will generate a cipherText from plainText using a valid key. The cipherText is obtained by replacing each alphabetical character in the plainText substituted for the corresponding character in the ciphertext; non-alphabetical characters should be left unchanged. You should first implement the replace() function that do the substitution for one letter, and then map the replace() function to every letter in the plainText. · Your program must preserve case: capitalized letters must remain capitalized letters; lowercase letters must remain lowercase letters. Here is your starter code in hw6.py: # Author: Yanling Wang [email protected] def isValidKey(key): """   Returns True if key is a string that has 26 characters and each of the letter   'a'/'A'-'z'/'Z' appeared once and only once in either lower case or upper case.   Returns False otherwise.   """ return True def replace(letter, key): """   Assume letter is a single characer string.   Replace a single letter with its corresponding key, returns letter if it is   not in the alphabet 'a'-'z' or 'A'-'Z'   """ return letter def substitution(plainText, key): """   Returns encrypted string for the plainText using substitution encryption, which   is to substitute 'a'/'A' with the lower/upper case of key[0], and substitute   'b'/'B' with the lower/upper case of key[1], and so on all the way to 'z'/'Z'.   Leave all other non-alpha characters as it is in plainText.   Your algorithm should be efficient so that it can run fairly quickly for very   large strings.   1. use replace() to convert each letter to its encrypted letter   2. append encrypted letter to a list   3. use ''.join(list_of_letters) to form the final string. Do not use + to do   string concatenation to form the encrypted string.   """ return plainText def run(): """   1. Prompt user for a key;   2. Check if key is valid. If not, print an error message then return;   if valid, prompt for a plain text to be encrypted with the valid key,   3. Send the plain text and valid key to substitution function.   4. Print out the encrypted message.   """ key = input("Enter a 26 letter key: ") if not isValidKey(key): print("Invalid key.") return plainText = input("Plain Text: ") cipherText = substitution(plainText, key) print(f"Cipher Text: {cipherText}") return if __name__ == "__main__": run() PreviousNext
Answered Same DayOct 09, 2021

Answer To: Substitution Encryption This assignment is adapted from a problem in pset 2 from a popular Harvard's...

Arun Shankar answered on Oct 11 2021
163 Votes
# Author: Yanling Wang [email protected]
def isValidKey(key):
"""
  Returns True if key is a string
that has 26 characters and each of the letter
  'a'/'A'-'z'/'Z' appeared once and only once in either lower case or upper case.
  Returns False otherwise.
  """
if(len(key) != 26):
return False

key = key.upper()
for letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
if letter not in key:
return False
return True
def replace(letter, key):
"""
  Assume letter is a single characer string.
  Replace a single letter with its corresponding key, returns letter if it is
  not in the alphabet 'a'-'z' or 'A'-'Z'
  """
key = key.upper()
isUpperCase = letter[0].isupper()
capitalLetter...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here