凯撒密码破解 Python
Caeser Cipher Cracking Python
我正在 运行使用 Python 2.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(字母表中每个 number/letter 1)。
我觉得我所拥有的应该可以正常工作,但我几乎可以肯定我所拥有的应该可以工作,但是在 运行 之后它只会 运行 一次,例如 newkey = 0
和 i = 0
但会递增到下一个值 newkey = 1
和 i = 1
但不会再重新 运行.
谁能发现我遗漏的致命缺陷?或者关于如何更有效地 运行 的任何提示,我们将不胜感激。
只需移动
的缩进
return plaintext
向左一步
这将解决循环问题,它将遍历所有 26 个数字
没有检查程序的其余部分是否良好
我正在 运行使用 Python 2.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(字母表中每个 number/letter 1)。
我觉得我所拥有的应该可以正常工作,但我几乎可以肯定我所拥有的应该可以工作,但是在 运行 之后它只会 运行 一次,例如 newkey = 0
和 i = 0
但会递增到下一个值 newkey = 1
和 i = 1
但不会再重新 运行.
谁能发现我遗漏的致命缺陷?或者关于如何更有效地 运行 的任何提示,我们将不胜感激。
只需移动
的缩进return plaintext
向左一步
这将解决循环问题,它将遍历所有 26 个数字
没有检查程序的其余部分是否良好