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)
如果使用bruteforce进行解密,则无法检查哪一个是正确的输出。因此,您需要使用
shift_value
来解密字符串在加密代码中,还有一件事是在while循环中更改
x
值。此操作不需要循环。如果条件允许,您可以直接使用相关问题 更多 >
编程相关推荐