Caeser密码破解Python

2024-09-27 21:28:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python2.7.12运行这个程序

charset="ABCDEFGHIJKLMNOPQRSTUVWXYZ" # The list of characters to be    encrypted
numchars=len(charset) # number of characters that are in the list for encryption

 def caesar_crack(crackme,i,newkey):

    print '[*] CRACKING - key: %d; ciphertext: %s' % (i,crackme)
    crackme=crackme.upper()
    plaintext='' #initialise plaintext as an empty string

    while i <= 26:
        for ch in crackme:   #'for' will check each character in plaintext against charset
            if ch in charset:
                pos=charset.find(ch)    #finds the position of the current character
                pos=pos-newkey
            else:
                new='' # do nothing with characters not in charet
            if pos>=len(charset):   #if the pos of the character is more or equal to the charset e.g -22 it will add 26 to get the correct letter positioning/value
                pos=pos+26
            else:
                new=charset[pos]
            plaintext=plaintext+new
        print '[*] plaintext: ' + plaintext

        if i <= 27:
                newkey=newkey+1
                i=i+1
        return plaintext

def main():
    # test cases
    newkey=0
    i=0
    crackme = 'PBATENGHYNGVBAFLBHUNIRPENPXRQGURPBQRNAQGURFUVSGJNFGUVEGRRA' 
    # call functions with text cases
    caesar_crack(crackme,i,newkey)

# boilerplate
if __name__ == '__main__':
    main()

这就是我到目前为止所拥有的,我目前正在寻找让它循环多次,最好是26次(字母表中每个数字/字母1次)。你知道吗

我觉得我所拥有的应该可以很好地工作,但是我几乎可以肯定,我拥有的应该可以工作,但是在运行时它只会运行一次,例如newkey = 0i = 0,但是会增加到下一个值newkey = 1i = 1,但是不会重新运行。你知道吗

有人能发现我遗漏的致命缺陷吗?或任何关于如何使其运行更有效的提示,都将不胜感激。你知道吗


Tags: ofthetoinposnewforif
1条回答
网友
1楼 · 发布于 2024-09-27 21:28:08

只需移动

return plaintext

向左一步

这将解决循环问题,它将通过所有26个数字

没有检查程序的剩余部分是否良好

相关问题 更多 >

    热门问题