2017-10-30 2 views
0

Nicht sicher, warum dies nicht funktioniert. Dies soll einen Satz verschlüsseln und entschlüsseln. Ich bekomme die richtigen Buchstaben zurück, aber sie sind nicht in der richtigen Reihenfolge .Probleme mit Python-Entschlüsselung und Verschlüsselungsalgorithmus

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

Vielleicht iterieren die Zeichen im Text bekommen dann * finde * das Zeichen im Wörterbuch - nicht umgekehrt. - aber das scheint für mich in Ordnung zu sein. "foo" -> "udd" -> "foo" – wwii

Antwort

0

Sie sind nicht sie, um bekommen, weil Sie zuerst durch Wörterbuch iteriert werden (die erste for Schleife). Ich änderte es ein wenig

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 

Hinweis: Wenn Sie durch Wörterbuch durchqueren, gibt keine Garantie dafür gibt, dass Sie die Elemente um

Verwandte Themen