如何在python中编写自动密钥密码?

时间:2017-03-23 18:09:44

标签: python

有人可以帮我在python中编写auto_key密码吗?我理解逻辑,但不知道如何开始。我编写了代码,但它没有工作这是我的auto_key密码,当我运行它时给了我新的 密钥如果密钥小于消息。然后停在那里,它不起作用。谢谢你!

enter code here
ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def main():
    message = input('enter message:\n')
    key = input('enter your key:\n')
    mode = input('encrypt or decrypt\n')
    if len(key) < len(message):
        key = key[0:] + message[:100]
    #print(key)
    if mode == 'encrypt':
       cipher = encryptMessage(message, key)

    elif mode == 'decrypt':
       cipher = decryptMessage(message, key)
    #print(' message:',  (mode.title()))
    print(cipher)
def encryptMessage (keys, messages):
    return cipherMessage(keys, messages, 'encrypt')
def decryptMessage(keys,messages):
    return cipherMessage(keys, messages, 'decrypt')
def cipherMessage (keys, messages, mode):
    cipher = []
    k_index = 0
    key = keys.upper()
    for i in messages:
        text = ALPHA.find(i.upper())
        if text != -1:
            if mode == 'encrypt':
                 text += ALPHA.find(key[k_index])


            elif mode == 'decrypt':
                 text -= ALPHA.find(key[k_index])
            text %= len(ALPHA)
            k_index += -1
            if k_index == len(key):
                k_index = 0
       else:
           cipher.append(i)
    return ''.join(cipher)

if __name__ == "__main__":
main()

1 个答案:

答案 0 :(得分:0)

即使我修复了你的代码,我建议你重新开始。 你的代码很乱! :d 它现在有效,但如果输入不合适,它仍然会失败,这是你实现的逻辑。

我们走吧:

  • 第8-9行:
    此时没用!! 注意:会发生什么if len(key) == 5 and len(message) == 130
  • 第16-17,18-19和20行:
    在第10行和第13行中,您拨打function(message, key),但在此处 打电话:

    def function((keys, messages)):
        return cipherMessage((keys, messages), 'encrypt')
    

    您切换了消息和密钥,这就是IndexError的原因。

  • 第26行: 在这个for-loop

  • 中没用
  • 第34行: k_index必须增加!
  • 第35-37行:
    • if在此for循环
    • 中无用
    • else:是永远无法cipher.append(i)
    • 的原因
  • 第38行: 要cipher,您需要添加简短加密/解密的字符, char form message

解决所有问题,您将获得以下代码。

注意:##的评论表明我对您的代码发表了评论。

ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def main():
    message = input('enter message:\n')
    key = input('enter your key:\n')
    mode = input('encrypt or decrypt\n')
    ## if len(key) < len(message):
        ## key = key[0:] + message[:100]
    #print(key)
    if mode == 'encrypt':
       cipher = encryptMessage(message, key)
    elif mode == 'decrypt':
       cipher = decryptMessage(message, key)
    #print(' message:',  (mode.title()))
    print(cipher)


## def encryptMessage (keys, messages):
##     return cipherMessage(keys, messages, 'encrypt')
  def encryptMessage (messages, keys):  
    return cipherMessage(messages, keys, 'encrypt')


## def decryptMessage(keys,messages):
##     return cipherMessage(keys, messages, 'decrypt')
def decryptMessage(messages, keys):
    return cipherMessage(messages, keys, 'decrypt')


## def cipherMessage (keys, messages, mode):
def cipherMessage (messages, keys, mode):
    cipher = []
    k_index = 0
    key = keys.upper()
    for i in messages:
        text = ALPHA.find(i.upper())
        ## if text != -1:
        if mode == 'encrypt':
             text += ALPHA.find(key[k_index])
             key += i.upper()  # add current char to keystream

        elif mode == 'decrypt':
             text -= ALPHA.find(key[k_index])
             key += ALPHA[text]  # add current char to keystream
        text %= len(ALPHA)
        ## k_index += -1
        k_index += 1
        ## if k_index == len(key):
        ##     k_index = 0
        ## else:
        cipher.append(ALPHA[text])
    return ''.join(cipher)

if __name__ == "__main__":
    main()