code mã hóa enigma trên python



  • em có xem video này: https://www.youtube.com/watch?v=6Z231g2NmrI nhưng không có file code đi kèm nên em code lại như trên video, nhưng vì không nhìn được hết code nên chạy theo thì bị lỗi mong mọi người chỉ giúp em, em mới học thôi ạ.

    import string
    
    rotor1 = list(string.ascii_lowercase)
    rotor2 = list(string.ascii_lowercase)
    rotor3 = list(string.ascii_lowercase)
    key = list(string.ascii_lowercase)
    values = ['b','w','d','c','f','e','h','g','j','i','l','k','n','p','o','q','']
    
    def initial_rotor_settings():
        global rotor15
        global rotor25
        global rotor35
        combo1 = ["d","m","t","w","s","i","l","r","u","y","q","n","k","f","e","j","c",""]
        combo2 = ["h","q","z","g","p","j","t","m","o","b","l","n","c","i","f","d","y",""]
        combo3 = ["u","q","n","t","l","s","z","f","m","r","e","h","d","p","x","k","i",""]
    
        while True:
            x = input("How would you like to arrange the 3 rotor? Rotors are numbered 1-3 and each munber may only used once. Enter here: ")
            if len(x) != 3:
                print("Error. %s is invalid. There must be exactly 3 digits." %x)
    
            if len(x) != len(set(x)):
                print("Error. %s is invalid. Each of the 3 digits may only be used once" %x)
    
            if ord(list(x)[0]) < 49 or ord(list(x)[0]) > 51 or ord(list(x)[1]) < 49 or ord(list(x)[1]) > 51 or ord(list(x)[2]) < 49 or ord(list(x)[2]) > 51:
                print("Error. %s is invalid. There must be exactly 3 digits." %x)
    
            if x== "123":
                rotor15 = rotor1
                rotor25 = rotor2
                rotor35 = rotor3
    
            if x== "132":
                rotor15 = rotor1
                rotor25 = rotor3
                rotor35 = rotor2
    
            if x== "213":
                rotor15 = rotor2
                rotor25 = rotor1
                rotor35 = rotor3
    
            if x== "231":
                rotor15 = rotor2
                rotor25 = rotor3
                rotor35 = rotor1
    
            if x== "312":
                rotor15 = rotor3
                rotor25 = rotor1
                rotor35 = rotor2
    
            if x== "321":
                rotor15 = rotor3
                rotor25 = rotor2
                rotor35 = rotor1
    
    def switchboard():
        postvalue = 0
        postkeynumber = 0
        while True:
            x = input("Enter 'yes' if you would like to make a change to the switchboard, or enter 'no' if you would like to keep it as is. Enter here: ")
            answer_list = list(x)
            if answer_list[0] == 'y':
                while True:
                    y = input("What letter would you like to change? Enter here: ")
                    if len(y) == 1 and ord(y) >= 97 and ord(y) <= 122:
                        keychange = y
                        while True:
                            z = input("What letter would you like to assign to this letter? Enter here: ")
                            if z == keychange : 
                                print("Error. %s is invalid. You cannot assign a letter")
                            if len(z) == 1 and ord(z) >= 97 and ord(z) <= 122 and z != 0:
                                valuechange = z
                                for i in range(0,26):
                                    if values[i] == valuechange:
                                        postkeynumber = i
    
                                for k in range(0,26):
                                    if keys[k] == keychange:
                                        postvalue = values[k]
                                        values[k] = valuechange
                                        values[postkeynumber] = postvalue
                                print("Your new switchboard includes " + keychange + "- kjah")
                                while True:
                                    answer = input("Would you like make another change ")
                                    if answer[0] == 'n':
                                        return
                                    if answer[0] == 'y':
                                        switchboard()
                                        return
                                    else:
                                        print("Error. Please enter 'yes' or 'no'.")
                            else:
                                print("Error. You must enter a single letter.")
                    else:
                        print("Error. You must enter a single letter. ")
            if answer_list[0] == 'n':
                return
            else:
                print("Error. The first letter must be 'y' for 'yes' or 'n' for 'no'")
    
    def spin_rotor(rotor, number):
        i = 1
        while i <= number:
            rotor.insert(0, rotor.pop())
            i += 1
    
    def assign_key():
        while True:
            x = input("Please enter your 3 letter key using only lowercase letters from")
            key = x
            key_list = list(x)
            if len(list(x)) != 3:
                print("Error. %s is invalid. The key must only contain 3 letters." %x)
            if ord(key_list[0]) >= 97 and ord(key_list[0]) <= 122 and ord(key_list[1]) >= 97 and ord(key_list[1]) <= 122 and ord(key_list[2]) >= 97 and ord(key_list[2]) <= 122:
                spin_rotor(rotor15, 123 - ord(key_list[0]))
                spin_rotor(rotor25, 123 - ord(key_list[1]))
                spin_rotor(rotor35, 123 - ord(key_list[2]))
                break
    
            else:
                print("Error. %s is invalid. The key must only contain lowercase letter" %x)
    
    def spin_rotors(number):
        spin_rotor(rotor15, 1)
        if number / 26 == 1:
            spin_rotor(rotor25, 1)
        if number / 676 == 1:
            spin_rotor(rotor35, 1)
    
    def reflect(letter_index):
        for m in range(0,26):
            if rotor35[letter_index] == rotor3[m]:
                for n in range (0,26):
                    if rotor25[m] == rotor2[n]:
                        for o in range(0,26):
                            if rotor15[n] == rotor1[o]:
                                for p in range(0,26):
                                    if rotor1[o] == values[p]:
                                        modified_phrase.append(key[p])
    def run_through_rotors_with(letter_index):
        for switchkeynumber in range(0,26):
            if text_list[letter_index] == keys[switchkeynumber]:
                for i in range(0,26):
                    if values[switchkeynumber] == rotor[i]:
                        for j in range (0,26):
                            if rotor1[i] == rotor15[j]:
                                for k in range(0,26):
                                    if rotor2[j] == rotor25[k]:
                                        for l in range(0,26):
                                            if rotor3[k] == rotor35[l]:
                                                if (l+1) % 2 == 1:
                                                    reflect(l +1)
                                                else:
                                                    reflect(l - 1)
    
    def modify_message():
        global modified_phrase
        global text_list
        modified_phrase = []
        global rotor_spin_counter
        rotor_spin_counter = 0
        x = input("Please enter your message. Numbers and simple punctuation and upp")
        text_list = list(x)
        len_text_list = len(text_list)
        for item in range(0, len_text_list):
            if ord(text_list[item]) < 97 and ord(text_list[item]) > 122:
                modified_phrase.append(text_list[item])
            if ord(text_list[item]) >= 97 and ord(text_list[item]) <= 122:
                rotor_spin_counter += 1
                spin_rotors(rotor_spin_counter)
                run_through_rotors_with(item)
        if len(modified_phrase) > len(text_list):
            modified_phrase.pop()
        phrase = "" .join(modified_phrase)
        print("Here is your message:" + phrase)
        print("Enter enigmamachine() to play again. You decypt your by ent")
    
    def enigmamachine():
        print("Chào Mừng!!!")
        initial_rotor_settings()
        switchboard()
        assign_key()
        modify_message()
    
    enigmamachine()
    

  • administrators

    Lỗi gì em?



  • nó bị lặp lại ạ

    Chào Mừng!!!
    How would you like to arrange the 3 rotor? Rotors are numbered 1-3 and each munber may only used once. Enter here: 213
    How would you like to arrange the 3 rotor? Rotors are numbered 1-3 and each munber may only used once. Enter here:
    

    sau đấy em chưa chạy được nữa nên không biết có lỗi gì nữa không


Hãy đăng nhập để trả lời
 

Có vẻ như bạn đã mất kết nối tới Cộng đồng Python Việt Nam, vui lòng đợi một lúc để chúng tôi thử kết nối lại.