为凯撒密码写蛮力,得到错误
Writing brute force for a caeser cipher, getting error
所以我在 python 中写了一个凯撒密码,我有一个蛮力选项来查看它是否可以解码具有随机偏移量的单词。由于某种原因,我收到错误消息:
Traceback (most recent call last):
File "C:\Users\nameredacted\Desktop\Python\CipherV2.py", line 60, in <module>
print(getTranslatedMessage(mode, message, key))
File "C:\Users\nameredacted\Desktop\Python\CipherV2.py", line 47, in getTranslatedMessage
ciphertext += alpha2[ (alpha1[i] + brutekey)]
KeyError: 26
我的代码是:
from PyDictionary import PyDictionary
import enchant
MAX_KEY_SIZE = 26
dictionary = PyDictionary()
d = enchant.Dict("en_US")
alpha1 = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ",range(26)))
alpha2 = dict(zip(range(26),"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
#gets if the user wants to encrypt or decrypt
def getMode():
while True:
print('Do you wish to encrypt, decrypt or brute force a message')
mode = input().lower()
if mode in 'encrypt e decrypt d bruteforce bf'.split():
return mode
else:
print('Enter either "encrypt" or "e" or "decrypt" or "d" or "bruteforce" or "bf".')
#gets offset value if needed
def getKey(mode):
key = 0
if mode[0]=='b':
pass
else:
while True:
key = int(input('Enter the offset number (1-%s)' % (MAX_KEY_SIZE)))
if (key >= 1 and key <= MAX_KEY_SIZE):
return key
#translates the message
def getTranslatedMessage(mode, message, key):
ciphertext=''
if mode[0] == 'd':
key = -key
for c in message.upper():
if c.isalpha():
ciphertext += alpha2[ (alpha1[c] + key)]
else: ciphertext += c
elif mode[0] =='e':
for x in message.upper():
if x.isalpha():
ciphertext += alpha2[ (alpha1[x] + key) ]
else:
ciphertext += x
else:
while True:
for i in message.upper():
for brutekey in range (26):
ciphertext += alpha2[ (alpha1[i] + brutekey)]
print(ciphertext)
if d.check(ciphertext):
break
return ciphertext
mode = getMode()
message = input("please input your message")
key = getKey(mode)
print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))
感谢阅读,如果您能对您看到的任何改进发表评论,我也将不胜感激。
正如@TemporalWolf 在评论中提到的,alpha1[i] + brutekey
在某些时候会大于 25。假设消息是 "XYZ"
,而从 0 开始的路由密钥是 2
。然后alpha1["X"] + brutekey = 25
,就可以了。但是 alpha2[ alpha1["Y"] + brutekey ]
会引发 KeyError。要解决此问题,您可以使用模运算符。它允许您像使用模拟时钟一样进行计算:8 + 6 = 2 (mod 12)
。在 python 中,模运算符由 %
表示。您的代码将是:
else:
while True:
for i in message.upper():
for brutekey in range (26):
ciphertext += alpha2[ (alpha1[i] + brutekey) % 26]
print(ciphertext)
我还认为您想更改这两个 for 循环的顺序。通过这种方式,您可以通过该字母的每个 brutekey 修复消息的字母和 运行。那只是字母表。
else:
for brutekey in range (26):
for i in message.upper():
ciphertext += alpha2[ (alpha1[i] + brutekey) % 26]
print(ciphertext)
if d.check(ciphertext):
break
所以我整合了你们所有人的帮助并重新编写了程序。它有点乱,但它有效。代码:
from PyDictionary import PyDictionary
import enchant
MAX_KEY_SIZE = 26
Go='Yes'
dictionary = PyDictionary()
d = enchant.Dict("en_US")
alpha1 = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ",range(26)))
alpha2 = dict(zip(range(26),"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
#gets if the user wants to encrypt or decrypt
def getMode():
while True:
print('Do you wish to encrypt, decrypt or brute force a message')
mode = input().lower()
if mode in 'encrypt e decrypt d bruteforce bf'.split():
return mode
else:
print('Enter either "encrypt" or "e" or "decrypt" or "d" or "bruteforce" or "bf".')
#gets offset value
def getKey(mode):
key = 0
if mode[0]=='b':
pass
else:
while True:
key = int(input('Enter the offset number (1-%s)' % (MAX_KEY_SIZE)))
if (key >= 1 and key <= MAX_KEY_SIZE):
return key
#translates the message
def getTranslatedMessage(mode, message, key):
ciphertext=''
if mode[0] == 'd':
key = -key
for c in message.upper():
if c.isalpha():
ciphertext += alpha2[ (alpha1[c] + key)]
else: ciphertext += c
elif mode[0] =='e':
for x in message.upper():
if x.isalpha():
ciphertext += alpha2[ (alpha1[x] + key)% (MAX_KEY_SIZE) ]
else:
ciphertext += x
else:
brutekey=0
while True:
for i in message.upper():
ciphertext += alpha2[ (alpha1[i] + brutekey)% (MAX_KEY_SIZE)]
print(ciphertext)
if d.check(ciphertext):
break
else:
ciphertext=''
brutekey=brutekey+1
return ciphertext
while Go=='yes' or 'Yes' or 'YES' or 'YEs' or 'yeS' :
mode = getMode()
message = input("please input your message")
key = getKey(mode)
print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))
Go=input('Would you like to use the cipher again?')
if Go in 'yes Yes YES YEs yeS':
pass
else:
print("thank you for using the program")
break
所以我在 python 中写了一个凯撒密码,我有一个蛮力选项来查看它是否可以解码具有随机偏移量的单词。由于某种原因,我收到错误消息:
Traceback (most recent call last):
File "C:\Users\nameredacted\Desktop\Python\CipherV2.py", line 60, in <module>
print(getTranslatedMessage(mode, message, key))
File "C:\Users\nameredacted\Desktop\Python\CipherV2.py", line 47, in getTranslatedMessage
ciphertext += alpha2[ (alpha1[i] + brutekey)]
KeyError: 26
我的代码是:
from PyDictionary import PyDictionary
import enchant
MAX_KEY_SIZE = 26
dictionary = PyDictionary()
d = enchant.Dict("en_US")
alpha1 = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ",range(26)))
alpha2 = dict(zip(range(26),"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
#gets if the user wants to encrypt or decrypt
def getMode():
while True:
print('Do you wish to encrypt, decrypt or brute force a message')
mode = input().lower()
if mode in 'encrypt e decrypt d bruteforce bf'.split():
return mode
else:
print('Enter either "encrypt" or "e" or "decrypt" or "d" or "bruteforce" or "bf".')
#gets offset value if needed
def getKey(mode):
key = 0
if mode[0]=='b':
pass
else:
while True:
key = int(input('Enter the offset number (1-%s)' % (MAX_KEY_SIZE)))
if (key >= 1 and key <= MAX_KEY_SIZE):
return key
#translates the message
def getTranslatedMessage(mode, message, key):
ciphertext=''
if mode[0] == 'd':
key = -key
for c in message.upper():
if c.isalpha():
ciphertext += alpha2[ (alpha1[c] + key)]
else: ciphertext += c
elif mode[0] =='e':
for x in message.upper():
if x.isalpha():
ciphertext += alpha2[ (alpha1[x] + key) ]
else:
ciphertext += x
else:
while True:
for i in message.upper():
for brutekey in range (26):
ciphertext += alpha2[ (alpha1[i] + brutekey)]
print(ciphertext)
if d.check(ciphertext):
break
return ciphertext
mode = getMode()
message = input("please input your message")
key = getKey(mode)
print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))
感谢阅读,如果您能对您看到的任何改进发表评论,我也将不胜感激。
正如@TemporalWolf 在评论中提到的,alpha1[i] + brutekey
在某些时候会大于 25。假设消息是 "XYZ"
,而从 0 开始的路由密钥是 2
。然后alpha1["X"] + brutekey = 25
,就可以了。但是 alpha2[ alpha1["Y"] + brutekey ]
会引发 KeyError。要解决此问题,您可以使用模运算符。它允许您像使用模拟时钟一样进行计算:8 + 6 = 2 (mod 12)
。在 python 中,模运算符由 %
表示。您的代码将是:
else:
while True:
for i in message.upper():
for brutekey in range (26):
ciphertext += alpha2[ (alpha1[i] + brutekey) % 26]
print(ciphertext)
我还认为您想更改这两个 for 循环的顺序。通过这种方式,您可以通过该字母的每个 brutekey 修复消息的字母和 运行。那只是字母表。
else:
for brutekey in range (26):
for i in message.upper():
ciphertext += alpha2[ (alpha1[i] + brutekey) % 26]
print(ciphertext)
if d.check(ciphertext):
break
所以我整合了你们所有人的帮助并重新编写了程序。它有点乱,但它有效。代码:
from PyDictionary import PyDictionary
import enchant
MAX_KEY_SIZE = 26
Go='Yes'
dictionary = PyDictionary()
d = enchant.Dict("en_US")
alpha1 = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ",range(26)))
alpha2 = dict(zip(range(26),"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
#gets if the user wants to encrypt or decrypt
def getMode():
while True:
print('Do you wish to encrypt, decrypt or brute force a message')
mode = input().lower()
if mode in 'encrypt e decrypt d bruteforce bf'.split():
return mode
else:
print('Enter either "encrypt" or "e" or "decrypt" or "d" or "bruteforce" or "bf".')
#gets offset value
def getKey(mode):
key = 0
if mode[0]=='b':
pass
else:
while True:
key = int(input('Enter the offset number (1-%s)' % (MAX_KEY_SIZE)))
if (key >= 1 and key <= MAX_KEY_SIZE):
return key
#translates the message
def getTranslatedMessage(mode, message, key):
ciphertext=''
if mode[0] == 'd':
key = -key
for c in message.upper():
if c.isalpha():
ciphertext += alpha2[ (alpha1[c] + key)]
else: ciphertext += c
elif mode[0] =='e':
for x in message.upper():
if x.isalpha():
ciphertext += alpha2[ (alpha1[x] + key)% (MAX_KEY_SIZE) ]
else:
ciphertext += x
else:
brutekey=0
while True:
for i in message.upper():
ciphertext += alpha2[ (alpha1[i] + brutekey)% (MAX_KEY_SIZE)]
print(ciphertext)
if d.check(ciphertext):
break
else:
ciphertext=''
brutekey=brutekey+1
return ciphertext
while Go=='yes' or 'Yes' or 'YES' or 'YEs' or 'yeS' :
mode = getMode()
message = input("please input your message")
key = getKey(mode)
print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))
Go=input('Would you like to use the cipher again?')
if Go in 'yes Yes YES YEs yeS':
pass
else:
print("thank you for using the program")
break