维吉尼亚密码加密和解密

问题描述 投票:0回答:1

我编写了一段代码,其中包含凯撒密码,然后是维吉尼亚密码。该代码提供了加密或解密的选择,加密工作正常,但解密却不行。我做错了什么?

  playing = True
    string = ""
    Alphabet = ('z','a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')

    def EncryptVigenere():
            plainText = input("Please enter the plain text: ")
            key = input("Please enter the key: ")
            keyList = []
            keyLength = 0
            num = 0
            while keyLength < len(plainText):
                try:
                    char = key[num]
                except IndexError:
                    num = 0
                    char = key[num]
                keyList.append(str(char))
                keyLength += 1
                num+= 1
                CipherText = [] 
                IndexValue = 0
                keyIncrement = 0
            for plainTextChar in plainText:
                 IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)
                 while IndexValue > 26:
                     IndexValue = IndexValue - 26
                 CipherText.append(Alphabet[IndexValue])
                 keyIncrement = keyIncrement + 1
                 print (''.join(CipherText))

            finish = input('Would you like to go again Y or N')
            if finish == 'n' or finish == 'N':
                retry = input ("Would you like to go again? Y or N: ")
                if retry == 'N' or retry == 'n':
                    print ("Please exit the window")
                    import time
                    time.sleep(1)
                    import sys
                    sys.exit()


    def DecryptVigenere():             
                plainText = input("Please enter the plain text: ")
                key = input("Please enter the key: ")
                keyList = []
                keyLength = 0
                num = 0
                while keyLength < len(plainText):
                    try:
                        char = key[num]
                    except IndexError:
                        num = 0
                        char = key[num]
                    keyList.append(str(char))
                    keyLength += 1
                    num -= 1
                    CipherText = [] 
                    IndexValue = 0
                    keyIncrement = 0
                for plainTextChar in plainText:
                     IndexValue = Alphabet.index(keyList[keyIncrement]) - Alphabet.index(plainTextChar)
                     while IndexValue > 26:
                         IndexValue = IndexValue - 26
                     CipherText.append(Alphabet[IndexValue])
                     keyIncrement = keyIncrement + 1
                     print (''.join(CipherText))

                finish = input('Would you like to go again Y or N')
                if finish == 'n' or finish == 'N':
                        retry = input ("Would you like to go again? Y or N: ")
                        if retry == 'N' or retry == 'n':
                                print ("Please exit the window")
                                import time
                                time.sleep(1)
                                import sys
                                sys.exit()


    while playing == True:
        keyIncrement = 0    
        string = ""
        eord = input('Type "d" to "decrypt" and "e" to "encrypt": ')

        if eord == 'e':
            texte = input ("Type your word to encrypt: ")
            key1 = int(input("Choose a key between 1-26: "))
            for letter in texte:
                number = (ord(letter)) + (key1)
                letter=(chr(number))
                string = (str(string)) + (str(letter))
            print (string)
            keyword = input ("Type 'encrypt' code further or 'decrypt' further: ")

            if keyword == 'encrypt' or keyword == 'e':
                EncryptVigenere()

            elif keyword == 'decrypt' or keyword == 'd':
                DecryptVigenere()   


        elif eord == 'd':
            texd = input ("Type your word to decrypt: ")
            key2 = int(input("Choose a key between 1-16: "))        
            for letter in texd:
                number = (ord(letter)) - (key2)
                letter=(chr(number))
                string = (str(string)) + (str(letter))
            print (string)
            keyword = input ("Type 'encrypt' code further or 'decrypt' further: ")

            if keyword == 'decrypt' or keyword == 'd':
                DecryptVigenere()

            elif keyword == 'encrypt' or keyword == 'e':
                EncryptVigenere()    

当我尝试使用维吉尼亚密码打印解密的代码时,代码输出不正确。提前致谢:)。

python encryption vigenere
1个回答
0
投票

这是我用于解密的代码: ''' #维吉尼亚密码 婴儿床=["A","B","C","D","E","F","G","H","I","J","K","L" ,"M","N","O","P","Q","R","S","T","U","V","W","X"," Y”,“Z”] 打印(婴儿床)

cipher = input("Input Cipher").upper()

key=input("Input Key").upper()

# iterate through the cipher and append each index of each letter to a new list called cipherPos


def vigenere(cipher,key):
  cipherPos=[]
  keyPos=[]
  subtract=[]
  outcome = ""
  for i in range(len(cipher)):
    cipherPos.append(crib.index(cipher[i]))
  print(cipherPos)
  for i in range (len(cipher)):
    keyPos.append(crib.index(key[i%len(key)]))
  print(keyPos)
  for i in range (len(cipherPos)):
    if (cipherPos[i] - keyPos[i]) >= 0:
      subtract.append(cipherPos[i] - keyPos[i])
    else:
      subtract.append((cipherPos[i] - keyPos[i]) % 26)
    outcome = outcome + crib[subtract[i]]
  return (outcome)
  
    
    
print(vigenere(cipher ,key))

'''

© www.soinside.com 2019 - 2024. All rights reserved.