【问题标题】:Python Assistance in bruteforce stage of Caesar Cipher with only the printable ASCII character set (32-126)仅使用可打印 ASCII 字符集 (32-126) 的 Caesar Cipher 蛮力阶段的 Python 协助
【发布时间】:2019-05-18 12:17:21
【问题描述】:

我试图将暴力破解阶段 (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 个)。

【问题讨论】:

  • 这是一个引人入胜且写得很好的第一个问题,尽管我认为更具体的标题会有所帮助。
  • 已修订。这样更好吗?
  • 看起来你至少用负值解密。你的凯撒密码似乎走错了方向。
  • @MaartenBodewes 你知道这是在哪里发生的吗?
  • 不,因为您没有向我们展示复制问题的完整代码。

标签: python encryption cryptography ascii


【解决方案1】:

请注意您在解密函数 (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)
...

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

【讨论】:

  • 还有一个+,模数95不能突然变成96。
  • 几乎,示例代码中的解密是使用- 对应offsetValue
  • 我实现了你的想法并运行它,它看起来更接近我的需要,但它有点偏离。如所需输出所示,偏移量 1 移动每个字符 -1、偏移量 2 - 2、偏移量 3 - 3 等。我的 for 循环有问题吗?
  • 当您分配 decryptList[decryptIndex] = chrDecrypt 时,您的循环会在每次迭代时覆盖原始字符串,因此下一次迭代是相对于前一个班次计算的。您可以继续覆盖并每次使用 offsetValue=1,或者(最好)保持原始字符串不变,这样您就可以继续阅读它,并写入另一个变量。
【解决方案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

输出:-

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

P.S. 您提供的代码不是凯撒密码,而是修改后的凯撒密码。两者的区别在于凯撒密码使用的是常量密钥(key = 3),而修改后的凯撒密码可以使用可变密钥(0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    • 2017-03-25
    • 2023-03-09
    相关资源
    最近更新 更多