Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON!!!! PLEASE HELP USING PYTHON CODE!!!!!! <><><><><><><><><><><><><><><><><

ID: 3600591 • Letter: P

Question

PYTHON!!!! PLEASE HELP USING PYTHON CODE!!!!!!

<><><><><><><><><><><><><><><><><><><><><><>

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. (CODE IMAGES BELOW)

3.6 Creating a Key 107 removeMatches (myString, removeString): newStr = "" for ch in myString: def if ch not in removeString: newStr = newStr + ch return newStr Listing 39 Remove the characters in one string from another >>>removeDupes 'topsecret') topsecr >removeMatches ('abcdefghijklmnopqrstuvuxyz', 'topsecr') abdfghijklmnquvwxyz ' >>> removeMatches ('abcdefghijklmnopqrstuvuxyz',removeDupes 'bondjamesbond') 'cfghiklpgrtuvwxyz' Session 3.11 Testing removeDupes and removeMatches Now that we have implemented removeDupes and removeMatches, the rest of our alphabet scrambling algorithm is fairly easy. Listing 3.10 shows the Python code needed to provide a scrambled version of the alphabet using a password as the starting point def genKeyFromPass (password) key = 'abcdefghijklmnopqrstuvwxyz' password = removeDupes(password) lastChar password [-1] last!dx = key, find (1astChar) afterString renoveMatches (key[1astlax+1:) ,password) beforeString - removeMatches (key [: lastIdx],password) key = password + afterString + beforeString return key Listing 3.10 Generating a key starting from a password

Explanation / Answer

# This function removes duplicate characters in the given string

def removeDupes(myString):
""" This function removes duplicate characters in the given string """
newStr = ""
# Loop thru characters of myString,
# Ignore if it is already encountered
for ch in myString:
if ch not in newStr:
newStr = newStr + ch
return newStr

# This function removes some characters of the given string
# if those are present in removeString
def removeMatches(myString,removeString):
"""This function removes some characters of the given string if those are present in removeString """
newStr = ""
# Loop thru characters of myString,
# Ignore if it is present in removeSttring
for ch in myString:
if ch not in removeString:
newStr = newStr + ch
return newStr

# This function removes spaces within a string
def removeSpaces(myString):
""" This function removes spaces within a string """
newStr = ""
# Loop thru characters of myString,
# Ignore if it is a space
for ch in myString:
if ch != ' ':
newStr = newStr + ch
return newStr

# This function generates a key from a password
# The idea is to get the last character of the password
# after removing duplicates from it
# Find its index in the alphabets
# Now the key will be unification of trimmed versions of
# password, the alphabet string after the index and
# the alphabet string before the index
def generateKeyFromPass(password):
""" This function generates a key from a password """
alphabet = "abcdefghijklmnopqrstuvwxyz"
password = removeDupes(password)
lastCh = password[-1]
index = alphabet.find(lastCh)
afterString = removeMatches(alphabet[index+1:],password)
beforeString = removeMatches(alphabet[:index],password)
key = password + afterString + beforeString
return key

# This function returns an encrypted string for a given plainText
# The idea is to generate a key from password and
# For each character of the plainText,
# find the index in alphabet string
# find the char in key for that index
# append each such char to the result string
def substitutionEncrypt(plainText,password):
""" This function returns an encrypted string for a given plainText """
alphabet = "abcdefghijklmnopqrstuvwxyz"
plainText = removeSpaces(plainText)
plainText = plainText.lower()
key = generateKeyFromPass(password)
cipherText = ""
for ch in plainText:
index = alphabet.find(ch)
cipherText = cipherText + key[index]
return cipherText

# This function returns the actual plain text for the encrypted string
# This will be the reverse process of encyption
# So for each char in cipherText
# find its index in key
# find the char for that index in alphabet string
# append each such char to result plainText string
def substitutionDecrypt(cipherText,password):
""" This function returns the actual plain text for the encrypted string """
alphabet = "abcdefghijklmnopqrstuvwxyz"
key = generateKeyFromPass(password)
plainText = ""
for ch in cipherText:
index = key.find(ch)
plainText = plainText + alphabet[index]
return plainText

# main function to test our substitutionEncrypt & substitutionDecrypt functions
def main():
""" main function to test our substitutionEncrypt & substitutionDecrypt functions """
# Test the helper functions
print removeDupes("HarryPotter")
print removeMatches("HarryPotter","Larry")
print removeSpaces("Harry Potter First")
  
# Hard code the input string and password for initial testing
inputString = "Harry Potter First"
password = "Bond"
encryptedStr = substitutionEncrypt(inputString,password)
print " Encrypted string for " +inputString +" is "+encryptedStr
print " Decrypted: "+substitutionDecrypt(encryptedStr,password)
  
# Now read the input from console and perform encryption and decryption
inputString = raw_input('Enter your input string:')
password = raw_input('Enter your password:')
encryptedStr = substitutionEncrypt(inputString,password)
print " Encrypted string for " +inputString +" is "+encryptedStr
print " Decrypted: "+substitutionDecrypt(encryptedStr,password)
  
main()