我试图将暴力破解阶段 (3) 中的解密限制在 ASCII 32 和 ASCII 126 之间。我在前两个阶段已经成功,但在暴力破解期间实现它时遇到了一些麻烦,所以我的结果回来准确。所需的输出是:
*** Menu ***
1. Encrypt string
2. Decrypt string
3. Brute force decryption
4. Quit
What would you like to do [1,2,3,4]? 3
Please enter string to decrypt: ykixkz&yw{oxxkr
Offset: 1 = Decrypted string: xjhwjy%xvznwwjq
Offset: 2 = Decrypted string: wigvix$wuymvvip
Offset: 3 = Decrypted string: vhfuhw#vtxluuho
Offset: 4 = Decrypted string: ugetgv"uswkttgn
Offset: 5 = Decrypted string: tfdsfu!trvjssfm
Offset: 6 = Decrypted string: secret squirrel
Offset: 7 = Decrypted string: rdbqds~rpthqqdk
Offset: 8 = Decrypted string: qcapcr}qosgppcj
Offset: 9 = Decrypted string: pb`obq|pnrfoobi
Offset: 10 = Decrypted string: oa_nap{omqennah
如您所见,它需要生成“ secret 松鼠”。
在暴力破解中,我不知道在哪里实现
for char in stringEncrypt:
x = ord(char)
x = x + offsetValue
while x < 32:
x += 95
while x > 126:
x -= 95
total += chr(x)
因此我还可以实现从 ASCII 32 解密为 ASCII 126 的输出。
如有任何帮助,我们将不胜感激。
我尝试将其放入 while 循环中,并将其放在代码中的不同位置。
print("*** Menu ***")
print(" ")
print("1. Encrypt string")
print("2. Decrypt string")
print("3. Brute force decryption")
print("4. Quit")
print(" ")
selection = int(input("What would you like to do [1,2,3,4]? "))
while selection == 1:
stringEncrypt = input("Please enter string to encrypt: ")
offsetValue = int(input("Please enter offset value (1 to 94): "))
total = ""
for char in stringEncrypt:
x = ord(char)
x = x + offsetValue
while x < 32:
x += 95
while x > 126:
x -= 95
total += chr(x)
print(" ")
print("Encrypted string:")
print(total)
print(" ")
print("*** Menu ***")
print(" ")
print("1. Encrypt string")
print("2. Decrypt string")
print("3. Brute force decryption")
print("4. Quit")
print(" ")
selection = int(input("What would you like to do [1,2,3,4]? "))
while selection == 2:
stringDecrypt = input("Please enter string to decrypt: ")
offsetValue = int(input("Please enter offset value (1 to 94): "))
total = ""
for char in stringDecrypt:
x = ord(char)
x = x - offsetValue
while x < 32:
x += 95
while x > 126:
x -= 95
total += chr(x)
print(" ")
print("Decrypted string:")
print(total)
print(" ")
print("*** Menu ***")
print(" ")
print("1. Encrypt string")
print("2. Decrypt string")
print("3. Brute force decryption")
print("4. Quit")
print(" ")
selection = int(input("What would you like to do [1,2,3,4]? "))
while selection == 3:
stringDecrypt = input("Please enter string to decrypt: ")
decryptList = list(stringDecrypt)
offsetValue = 0
decryptIndex = 0
for offsetValue in range(1, 95, 1):
for decryptIndex in range(len(decryptList)):
shifting = (ord(decryptList[decryptIndex]) - ord(" ") - offsetValue) % 95
chrDecrypt = chr(shifting + ord(" "))
decryptList[decryptIndex] = chrDecrypt
decryptIndex += 1
stringDecrypt = ''.join(decryptList)
print("Offset", offsetValue, " = Decrypted string:", stringDecrypt)
print(" ")
print("*** Menu ***")
print(" ")
print("1. Encrypt string")
print("2. Decrypt string")
print("3. Brute force decryption")
print("4. Quit")
print(" ")
selection = int(input("What would you like to do [1,2,3,4]?"))
if selection == 4:
print("Goodbye.")
我期望输出为输入 ykixkz&yw{oxxkr:
Offset: 1 = Decrypted string: xjhwjy%xvznwwjq
Offset: 2 = Decrypted string: wigvix$wuymvvip
Offset: 3 = Decrypted string: vhfuhw#vtxluuho
Offset: 4 = Decrypted string: ugetgv"uswkttgn
Offset: 5 = Decrypted string: tfdsfu!trvjssfm
Offset: 6 = Decrypted string: secret squirrel
Offset: 7 = Decrypted string: rdbqds~rpthqqdk
Offset: 8 = Decrypted string: qcapcr}qosgppcj
Offset: 9 = Decrypted string: pb`obq|pnrfoobi
Offset: 10 = Decrypted string: oa_nap{omqennah
但我得到:
Offset 1 = Decrypted string: xjhwjy%xvznwwjq
Offset 2 = Decrypted string: vhfuhw#vtxluuho
Offset 3 = Decrypted string: secret squirrel
Offset 4 = Decrypted string: oa_nap{omqennah
Offset 5 = Decrypted string: j\Zi\kvjhl`ii\c
Offset 6 = Decrypted string: dVTcVepdbfZccV]
Offset 7 = Decrypted string: ]OM\O^i][_S\\OV
Offset 8 = Decrypted string: UGETGVaUSWKTTGN
Offset 9 = Decrypted string: L><K>MXLJNBKK>E
Offset 10 = Decrypted string: B42A4CNB@D8AA4;
(最多 94 个)。
最佳答案
请注意解密函数 (x) 和暴力破解函数 (chrDecrypt) 中的字符解密之间的区别。稍后,您无法确保角色是否正确循环。这是条件应该在的位置,基本上确保您在值 32 到 128 上循环。
实现它的一种方法如下:
shifting = (ord(decryptList[decryptIndex]) - ord(" ") - offsetValue) % 95
chrDecrypt = chr(shifting + ord(" "))
这将是所需字符上的移位模块。
为了处理 decryptList
数组的覆盖,您可以执行以下操作:
...
tempDecrypt = []
for decryptIndex in range(len(decryptList)):
shifting = (ord(decryptList[decryptIndex]) - ord(" ") - offsetValue) % 95
chrDecrypt = chr(shifting + ord(" "))
tempDecrypt.append(chrDecrypt)
decryptIndex += 1
stringDecrypt = ''.join(tempDecrypt)
...
这将修复您在之前的代码中注意到的顺序更改。
关于Python 在凯撒密码的暴力破解阶段提供仅可打印 ASCII 字符集 (32-126) 的协助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56198858/