有python解密和加密算法的麻烦

时间:2017-10-30 22:27:07

标签: python encryption

不确定为什么这不起作用。这应该加密和解密一个句子。我找回了正确的字母,但它们不在右边 订购。

def get_encrypt(original_text,dictionary):
    #4
    match= ""
    for base in original_text:
        match += dictionary[base]
    return match

def rev_look(encrypted_text, dictionary):
    match_key = []

    for key in dictionary:
        i = 0
        for base in encrypted_text:
          print(base)
          if dictionary[key] == base:
            match_key.append(key)
          i += 1
    return match_key


def main():
    print('* Simple Substitution Cipher Tool *')
    #Define the dictionary
    dictionary = {' ':' ','a':'p', 'b':'h', 'c':'q', 'd':'g', 'e':'i', 'f':'u', 'g':'m', 'h':'e', 'i':'a', 'j':'y', 'k':'l', 'l':'n','m':'o', 'n':'f', 'o':'d', 'p':'x', 'q':'j', 'r':'k', 's':'r', 't':'c', 'u':'v', 'v':'s', 'w':'t', 'x':'z', 'y':'w', 'z':'b'}
    #2
    original_text =(input('Please enter the original text: '))
    #3
    encrypted_text = get_encrypt(original_text,dictionary)
    print('The encrypted text is: ', encrypted_text)
    print('The decrypted text is: ', rev_look(encrypted_text,dictionary))


main()

1 个答案:

答案 0 :(得分:0)

你没有按顺序获取它们,因为你是第一次遍历字典(第一个for循环)。我修改了一下

def rev_look(encrypted_text, dictionary):
    match_key = []        
    for base in encrypted_text:
        for key in dictionary:
            if dictionary[key] == base:
                match_key.append(key)

    return match_key

注意:当您浏览字典时,无保证您将按顺序获取元素