Revise the substitutionEncrypt function from section 3.5 (p. 102) of the textbook to (1) remove all spaces from the plaintext message before it is encrypted and (2) generate the substitution cipher key from a password. (substitutionEncrypt will call genKeyFromPass to do this.)

Revise the substitutionEncrypt function from section 3.5 (p. 102) of the textbook to (1) remove all spaces from the plaintext message before it is encrypted and (2) generate the substitution cipher key from a password. (substitutionEncrypt will call genKeyFromPass to do this.) The password should be a parameter, psw, which will replace key as a parameter in the function header.

Write function substitutionDecrypt, which will have two parameters, cipherText, a string, a message encrypted by substitutionEncrypt, and psw, the password used to generate the encryption key, also a string. substitutionDecrypt should return the original plaintext message with spaces removed (a string).

Don't use plagiarized sources. Get Your Custom Essay on
Revise the substitutionEncrypt function from section 3.5 (p. 102) of the textbook to (1) remove all spaces from the plaintext message before it is encrypted and (2) generate the substitution cipher key from a password. (substitutionEncrypt will call genKeyFromPass to do this.)
Just from $8 /Page 0r 300 words
Order Now

Finally, write a top-level function, main. main should (a) use Python’s built-in input function to get a string to encrypt and a password; (b) call substitutionEncrypt to encrypt the input string; (c) print the value returned by substitutionEncrypt; (d) call substitutionDecrypt to convert the value returned by substitutionEncrypt back to plaintext; (e) print the value returned by substitutionDecrypt.

Save your time - order a paper!

Get your paper written from scratch within the tight deadline. Our service is a reliable solution to all your troubles. Place an order on any task and we will take care of it. You won’t have to worry about the quality and deadlines

Order Paper Now

Use doctest.testmod to check the simple examples included in the function docstrings. The functions should also return correct values for the test cases given here:

>>>subEncrypt(‘the quick brown fox’, ‘ajax’)

‘qdznrexgjoltkblu’

>>>subDecrypt(‘qdznrexgjoltkblu’, ‘ajax’)

‘thequickbrownfox’

Finally, include code in your .py file to call the main function, to check results for additional strings and passwords.

Please revise this code to meet the requirements:

“””

def SubstitutionEncrypt(plainText, psw):
“””
>>> SubstitutionEncrypt(‘the quick brown fox’,’ajax’)
‘qdznrexgjoltkblu’

:param plainText:
:param psw:
:return:
“””
alphabet = “abcdefghijklmnopqrstuvwxyz ”

# removing spaces from text
plainText = removeMatches(plainText.lower(), ” “)

# Generating key from password
generated_key = genKeyFromPass(psw)
cipherText = “”
for ch in plainText:
idx = alphabet.find(ch)
cipherText = cipherText + generated_key[idx]
return cipherText

def removeDupes(myString):
“””

:param myString:
:return:
“””
newStr = “”
for ch in myString:
if ch not in newStr:
newStr = newStr + ch
return newStr

def removeMatches(myString, removeString):
“””

:param myString:
:param removeString:
:return:
“””
newStr = “”
for ch in myString:
if ch not in removeString:
newStr = newStr + ch
return newStr

def genKeyFromPass(password):
“””

:param password:
:return:
“””
key = ‘abcdefghijklmnopqrstuvwxyz’
password = removeDupes(password)
lastChar = password[-1]
lastIdx = key.find(lastChar)
afterString = removeMatches(key[lastIdx + 1:], password)

beforeString = removeMatches(key[:lastIdx], password)
key = password + afterString + beforeString
return key

def SubstitutionDecrypt(cipherText, psw):
“””
>>> SubstitutionDecrypt(‘qdznrexgjoltkblu’,’ajax’)
‘thequickbrownfox’

:param cipherText:
:param psw:
:return:
“””
alphabet = “abcdefghijklmnopqrstuvwxyz ”

# Generating key from password
generated_key = genKeyFromPass(psw)
plain_text = “”
for ch in cipherText:
idx = generated_key.find(ch)
plain_text += alphabet[idx]
return plain_text

def main():
text = input(“Enter String to Encrypt:”)
password = input(“Enter password:”)
encrypted_text = SubstitutionEncrypt(text, password)
print(“encrypted_text:”, encrypted_text)
decrypted_text = SubstitutionDecrypt(encrypted_text, “ajax”)
print(“decrypted_text:”, decrypted_text)

if __name__ == “__main__”:
# import doctest
# doctest.testmod()

main()

 

In: Computer Science

Looking for a similar assignment? Get help from our qualified experts!

Order Now