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

2024-05-23 22:53:49 发布

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

我试图将破解阶段(3)中的解密限制在ascii32和ascii126之间。在前两个阶段我已经成功了,但是我在野蛮的执行过程中遇到了一些困难,所以我的结果会准确地返回。所需输出为:

*** 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

如你所见,它需要生产“秘密松鼠”。在

在残暴中,我不知道该在哪里实施

^{pr2}$

所以我也可以实现从ascii32到ascii126的解密输出。在

任何帮助都将不胜感激。在

我尝试将它放入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个)。在


Tags: toinputstringoffsetinttotalmenuprint
2条回答

尝试:

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

输出:-

^{pr2}$

p.S.您提供的密码不是Caesar密码,而是修改后的Caesar密码。两者之间的区别在于Caesar密码使用常量密钥(key=3),但是修改后的Caesar密码可以使用可变密钥(0<;key<;26)。在

注意解密函数(x)和蛮力函数(chrDecrypt)的字符解密之间的区别。在后面,您不能确保字符循环正确。这就是条件应该在的地方,基本上确保在值32到128上循环。在

一种实施方法如下:

shifting = (ord(decryptList[decryptIndex]) - ord(" ") - offsetValue) % 95
chrDecrypt = chr(shifting + ord(" "))

这将是所需字符上的移位模块。在

为了处理decryptList数组的重写,可以执行以下操作:

^{pr2}$

这将修复您在前面的代码中注意到的顺序更改。在

相关问题 更多 >