在Python3中,我必须创建两个函数,substitutionEncrypt和substitutionDecrypt。 必须修改substitutionEncrypt,以便在加密之前删除纯文本的所有空格,并且它将从密码生成密码密钥。(调用genKeyFromPass)。 psw将key替换为函数头中的参数。
substitutionDecrypt将有两个参数(cipherText,psw) 我被给了以下代码。它应该返回原始的plainText并删除空格。
最后,写一个顶级函数,main。 main应该(a)使用Python的内置输入函数来获取要加密的字符串和密码; (b)调用substitutionEncrypt来加密输入字符串; (c)打印substitutionEncrypt返回的值; (d)调用substitutionDecrypt将substitutionEncrypt返回的值转换回 纯文本; (e)打印由substitutionDecrypt返回的值。
注意:subEncrypt和subDecrypt需要调用genKeyFromPass,它调用removeDupes和removeMatches。我已经获得了以下代码。
defSubstitutionEncrypt(plainText, key):
alphabet = "abcdefghijklmnopqrstuvwxyz "
plainText = plainText.lower()
cipherText = " "
for ch in plainText:
idx = alphabet.find(ch)
cipherText = cipherText + key[idx]
return cipherText
def removeDupes(myString):
newStr = " "
for ch in myString:
if ch not in newStr:
newStr = newStr + ch
return newStr
def removeMatches(myString, removeString):
newStr = " "
for ch in myString:
if ch not in removeString:
newStr = newStr + ch
return newStr
def genKeyFromPass(password):
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 substitutionEncrypt(plainText, psw):
alphabet = "abcdefghijklmnopqrstuvwxyz "
newStr = plainText.lower()
cipherText = " "
genKeyFromPass(newStr)
for ch in newStr:
idx = alphabet.find(ch)
cipherText = cipherText + genKeyFromPass(psw)[idx]
return cipherText.replace(" ","")
def genKeyFromPass(psw):
key = 'abcdefghijklmnopqrstuvwxyz'
psw = removeDupes(psw)
lastChar = psw[-1]
lastIdx = key.find(lastChar)
afterString = removeMatches(key[lastIdx+1:], psw)
beforeString = removeMatches(key[:lastIdx], psw)
key = psw + afterString + beforeString
return key
def removeDupes(myString):
newStr = " "
for ch in myString:
if ch not in newStr:
newStr = newStr + ch
return newStr
def removeMatches(myString, removeString):
newStr = " "
for ch in myString:
if ch not in removeString:
newStr = newStr + ch
return newStr
print(substitutionEncrypt('the quick brown fox', 'ajax'))
我得到" nukobjdualhqguyhr"作为输出。 输出示例如下:
subEncrypt('快速的棕色狐狸',' ajax')
' qdznrexgjoltkblu'
subDecrypt(' qdznrexgjoltkblu',' ajax')
' thequickbrownfox'
我的代码出了什么问题?我一直无法从另一个函数调用一个值(从genKeyFromPass调用键)。它在substitutionEncrypt中返回语句之前的行。另外,我将如何进行substitutionDecrypt?我已经走到了这一步:
def substitutionDecrypt(cipherText, psw):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
newStr = cipherText.lower()
plainText = " "
genKeyFromPass(cipherText)
for ch in cipherText:
idx = alphabet.find(ch)
return plainText.replace(" ","")