gpt4 book ai didi

python - Python 3.2 中的暴力破解脚本

转载 作者:太空狗 更新时间:2023-10-30 00:24:28 25 4
gpt4 key购买 nike

我是编写代码的初学者,我从 Python 开始,因为它似乎是最简洁、最容易上手的(我目前使用的是 Python 3.2)。现在我已经阅读了一些关于 python 编码的在线书籍等等,我已经制作了一些小程序,仅此而已。

但后来我想制作一个可以暴力破解随 secret 码的程序,例如:

PassWord = random.randint(0,9999)

我做了一些可以尝试随 secret 码的东西:

import random
PassWord = str(random.randint(0,9999))
Trial = ' '
while Trial != PassWord:
Trial = str(random.randint(0,9999))
print(Trial)
if Trial == PassWord:
print('The password is: '+PassWord)
input()

但这并不是真正的暴力攻击,它更像是随机猜测密码。我认为蛮力攻击是首先尝试所有可能性的 1 位数,然后是 2、3 等等。但我不知道如何做到这一点。

如果有人会说如何创建一个程序,首先用 1 个数字检查所有可能性,如果可能的话,以正确的顺序(0、1、2、3 等),然后是 2、3 和4 位数字。

然后我可以解决它,并学习更多关于 Python 的知识。

最佳答案

代码优先:

from itertools import product

chars = '0123456789' # chars to look for

for length in range(1, 3): # only do lengths of 1 + 2
to_attempt = product(chars, repeat=length)
for attempt in to_attempt:
print(''.join(attempt))

itertools.product 生成其输入的笛卡尔连接 - 在这种情况下,它正在“连接”到自身。所以在第一次迭代中,每个字符都被打印出来。然后在下一次迭代中,由于 repeat=length(并且 length 现在 == 2),生成 '00'、'01' 等...值得尝试并查看输出以更好地理解它。

这也意味着您可以输入字母(大写/小写),并在 range 函数中更改上限。

它当然不会打破代码破解的世界,但应该让您了解 Python 的灵 active 和可用的工具。

我会让你检查密码是否匹配并打破循环。

关于python - Python 3.2 中的暴力破解脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11367553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com