Поддержка Python на этапе грубой силы Caesar Cipher только с набором печатных символов ASCII (32-126) - PullRequest
1 голос
/ 18 мая 2019

Я пытаюсь ограничить дешифрование на этапе брутфорсинга (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 ]

1 голос
/ 18 мая 2019

Обратите внимание на разницу между расшифровкой вашего символа в функции расшифровки (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)
...

Это исправит изменение порядка, которое вы заметили в предыдущем коде.

0 голосов
/ 21 мая 2019

TRY:

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 Код, который вы предоставилиэто не шифр Цезаря, это модифицированный шифр Цезаря.Разница между ними заключается в том, что шифр Цезаря использует постоянный ключ (ключ = 3), но модифицированный шифр Цезаря может использовать переменные ключи (0 <ключ <26). </p>

...