python - 仅使用 32-126 的 ASCII 字符集(可打印)的 Python 蛮力解密
问题描述
大家好,在这里遇到了巨大的麻烦,无法弄清楚错误在哪里寻求帮助。我已经 > 在下面粘贴了我的两个(最好的?最接近的?)尝试。
我正在尝试一种蛮力解密方法,但不只使用 > 字母表中通常的 26 个字符。目的是封装从 ASCII 32 到 ASCII 126 的全套可打印字符。
如果我能正确写出这篇文章,它应该会在这篇文章的底部打印出一个结果。
我哪里错了?
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)
预期输出示例:
移位值 1 = 解密代码:xjhwjy%xvznwwjq
(through to)
移位值 94 = 解密代码:zljyl{'zx|pyyls
但是,我只得到:
移位值 94 = 解密代码:~~~~~~~~~~~~~~~
或空白。
按要求更新;用于加密的方法是:
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
来解密字符串。
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
值。循环不是必需的。你可以只使用 if 条件。
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
推荐阅读
- c - 奇怪的问题,在尝试从文档而不是空格扫描数据时,程序在 C 中读取新行
- docker - Docker 构建错误:无法连接到 unix:///var/run/docker.sock 上的 Docker 守护程序。docker 守护进程是否正在运行?
- solidity - 在工厂合同中创建合同不起作用
- dwolla - ACH / eCheck 付款处理,无需验证客户和资金来源
- reactjs - 使用 React 获取 api 时方法名称错误中发现无效字符
- visual-studio-code - 我应该如何在 vs 代码中打开 .ipynb 作为新的笔记本版本
- android - Android 11+ 在前台服务 (FLP) 中对后台位置的请求限制是什么?
- python - Python拆分SQL语句
- javascript - 更改属性的值
- javascript - mongodb在写之前关闭连接