仅使用32126(可打印)中的ASCII字符集进行Python暴力解密

2024-06-16 11:58:25 发布

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

Hi all, having immense trouble here and can't figure out where errors are so seeking assistance. I've > pasted my two (best? closest?) attempts below.

I'm attempting a brute-force approach to decryption, but not using just the usual 26 characters of the > alphabet. The intent is to encapsulate the full set of printable characters from ASCII 32 to ASCII 126.

If I can get this written correctly, it should print out a result at the bottom of this post.

Where am I going wrong?

input_string = input('Enter code for decryption: ')
input_list = list(input_string)
shift_value = 0
decryption_index = 0
while shift_value in range(1, 95, 1) : 
    while decryption_index < len(input_list):
        decryption_index = 0
        shifter = (ord(input_list[decryption_index]) - ord(" ") - shift_value) % 95
        chr_decrypt = chr(ord(input_list[decryption_index]) + shift_value)
        input_list[decryption_index] = chr_decryption
        decryption_index += 1
    shift_value -= 1
    input_string = ''.join(input_list)
    print ('Shift value',shift_value,' = Decrypted code:' ,input_string)

&...

input_string = input('Please enter code for decryption: ')
input_list = list(input_string)
shift_value = 0
decryption_index = 0


for shift_value in range(1, 95, 1):
    for decryption_index in range(len(input_list)):

        shifting = (ord(input_list[decryption_index]) - ord(" ") - shift_value) % 95
        chr_decryption = chr(shift_value + ord(" "))
        input_list[decryption_index] = chr_decryption
        decryption_index += 1

input_string = ''.join(input_list)

print('Shift value', shift_value, '= Decrypted code:', input_string)

Example intended output:

移位值1=解密代码:xjhwjy%xvznwjq

 (through to)

Shift value 94 = Decrypted code: zljyl{'zx|pyyls

However, I get only:

Shift value 94 = Decrypted code: ~~~~~~~~~~~~~~~

or a blank.

Update as requested; method used to encrypt is:

input_string = input("Please enter string to encrypt: ")
    shift_value = int(input("Please enter shift value (1 to 94): "))
    total = ""

    for char in input_string:
        x = ord(char)
        x = x + shift_value

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

        total += chr(x)

    print("")
    print("Encrypted string:")
    print(total)

Tags: thetoforinputstringindexshiftvalue
1条回答
网友
1楼 · 发布于 2024-06-16 11:58:25

如果使用bruteforce进行解密,则无法检查哪一个是正确的输出。因此,您需要使用shift_value来解密字符串

def decrypt(s, key):
    ans = ''
    for char in s:
        x = (ord(char) - ord(' ') - key) % 95
        ans += chr(x + ord(' '))
    return ans

decrypt('xjhwjy%xvznwwjq', 93) #zljyl{'zx|pyyls

在加密代码中,还有一件事是在while循环中更改x值。此操作不需要循环。如果条件允许,您可以直接使用

def encrypt(s, key):
    ans = ''
    for char in s:
        x = ord(char) + key
        if x < 32:
            x += 95
        if x > 126:
            x -= 95
        ans += chr(x)  
    return ans
encrypt("zljyl{'zx|pyyls", 93) #xjhwjy%xvznwwjq

相关问题 更多 >