In: Computer Science
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).
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.
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()
File Name : main.py Author : Date : 7/2/18 Description : """ 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()
output: