凯撒密码的暴力破解阶段仅提供可打印的ASCII字符集的Python协助(32-126)

时间:2019-05-18 12:17:22

标签: 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 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)

所以我还可以实现将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个)。

2 个答案:

答案 0 :(得分:1)

请注意,您在解密函数(x)上的char解密与蛮力函数(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)
...

这将解决您在上一个代码中注意到的顺序更改。

答案 1 :(得分:0)

尝试:

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

PS 。您提供的代码不是凯撒密码,而是修改后的凯撒密码。两者之间的区别在于,Caesar密码使用恒定密钥(密钥= 3),而修改后的Caesar密码可以使用可变密钥(0 <密钥<26)。