Python在Caesar密码的bruteforce阶段提供帮助,仅使用可打印的ASCII字符集(32-126)

Python在Caesar密码的bruteforce阶段提供帮助,仅使用可打印的ASCII字符集(32-126),python,encryption,cryptography,ascii,Python,Encryption,Cryptography,Ascii,我试图将暴力破解阶段(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 decr

我试图将暴力破解阶段(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
正如你所看到的,它需要生产“秘密松鼠”

在残酷的折磨中,我不知道该在哪里实施

for char in stringEncrypt:
        x = ord(char)
        x = x + offsetValue

        while x < 32:
            x += 95
        while x > 126:
            x -= 95  

        total += chr(x)
但我得到的却是:

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;
Offset 1=解密的字符串:xjhwjy%xvznwjq
偏移量2=解密字符串:vhfuhw#vtxluuho
偏移量3=解密字符串:秘密松鼠
偏移量4=解密的字符串:oa_nap{omqennah
偏移量5=解密的字符串:j\Zi\kvjhl`ii\c
偏移量6=解密字符串:dVTcVepdbfZccV]
偏移量7=解密的字符串:]OM\O^i][\u S\\OV
偏移量8=解密的字符串:ugetgvauswktgn
偏移量9=解密的字符串:L>MXLJNBKK>E
偏移量10=解密的字符串:B42A4CNB@D8AA4;

(最多94个)。

请注意解密函数(x)和蛮力函数(chrdepcrypt)中的字符解密之间的区别。之后,您无法确保字符正确循环。这就是条件所在,基本上确保在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)
...
这将修复您在前面代码中注意到的顺序更改。

请尝试:

cipher_text = input("Enter the Cipher Text = ")
length = len(cipher_text)
plain_text = ""
i = 0
key = 1
while key < 26:
    plain_text = ""
    while i < length:
        if ord(cipher_text[i]) - key < 32:
            plain_text += chr(ord(cipher_text[i]) - key + 95)
        else:
            plain_text += chr(ord(cipher_text[i]) - key)
        i += 1

    i = 0

    print("Decrypting cipher text with key ", key, "is", plain_text)
    key += 1

p.S.您提供的代码不是凯撒密码,而是修改后的凯撒密码。两者之间的区别在于凯撒密码使用常量密钥(key=3),而修改后的凯撒密码可以使用变量密钥(0这是一个引人入胜且写得很好的第一个问题,尽管我认为这会有助于更具体地描述你的标题。修改。这样更好吗?看起来你至少是在用负值解密。你的凯撒密码似乎走错了方向。你知道这是在哪里发生的吗,@MaartenBodewes?不,因为你知道我们甚至没有向我们展示复制该问题的完整代码。仍然有一个
+
,模数95不能突然变为96。几乎,示例代码中的解密是用
-
偏移值进行的。我实现了您的想法并运行了它,它看起来更接近我所需要的,但有点小偏差。如图所示所需的输出,偏移量1移动每个字符-1、偏移量2-2、偏移量3-3等。我的for循环中是否有错误?分配
decryptList[decryptIndex]时,每次迭代时,您的循环都会覆盖原始字符串=chrdefcrypt
,因此下一次迭代是相对于上一次移位计算的。您可以保持重写并每次使用offsetValue=1,或者(最好)保持原始字符串不变,以便可以继续读取它并写入另一个变量。
...
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)
...
cipher_text = input("Enter the Cipher Text = ")
length = len(cipher_text)
plain_text = ""
i = 0
key = 1
while key < 26:
    plain_text = ""
    while i < length:
        if ord(cipher_text[i]) - key < 32:
            plain_text += chr(ord(cipher_text[i]) - key + 95)
        else:
            plain_text += chr(ord(cipher_text[i]) - key)
        i += 1

    i = 0

    print("Decrypting cipher text with key ", key, "is", plain_text)
    key += 1
Enter the Cipher Text = ykixkz&yw{oxxkr
Decrypting cipher text with key  1 is xjhwjy%xvznwwjq
Decrypting cipher text with key  2 is wigvix$wuymvvip
Decrypting cipher text with key  3 is vhfuhw#vtxluuho
Decrypting cipher text with key  4 is ugetgv"uswkttgn
Decrypting cipher text with key  5 is tfdsfu!trvjssfm
Decrypting cipher text with key  6 is secret squirrel
Decrypting cipher text with key  7 is rdbqds~rpthqqdk
Decrypting cipher text with key  8 is qcapcr}qosgppcj
Decrypting cipher text with key  9 is pb`obq|pnrfoobi
Decrypting cipher text with key  10 is oa_nap{omqennah
Decrypting cipher text with key  11 is n`^m`oznlpdmm`g
Decrypting cipher text with key  12 is m_]l_nymkocll_f
Decrypting cipher text with key  13 is l^\k^mxljnbkk^e
Decrypting cipher text with key  14 is k][j]lwkimajj]d
Decrypting cipher text with key  15 is j\Zi\kvjhl`ii\c
Decrypting cipher text with key  16 is i[Yh[juigk_hh[b
Decrypting cipher text with key  17 is hZXgZithfj^ggZa
Decrypting cipher text with key  18 is gYWfYhsgei]ffY`
Decrypting cipher text with key  19 is fXVeXgrfdh\eeX_
Decrypting cipher text with key  20 is eWUdWfqecg[ddW^
Decrypting cipher text with key  21 is dVTcVepdbfZccV]
Decrypting cipher text with key  22 is cUSbUdocaeYbbU\
Decrypting cipher text with key  23 is bTRaTcnb`dXaaT[
Decrypting cipher text with key  24 is aSQ`Sbma_cW``SZ
Decrypting cipher text with key  25 is `RP_Ral`^bV__RY