Python暴力密码破解程序未完成例程

2024-04-19 07:57:08 发布

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

我为我的一个类制作了一个强力密码破解程序,但不明白为什么它没有完全检查每个组合。如果我只使用数字或小写字母的简单密码,它通常是有效的,但复杂的字符组合不一定能给出答案

以下是我提交的作业代码:

from datetime import datetime
from itertools import combinations_with_replacement, permutations
#############################
# Password Cracking Program #
#############################
possibleChars = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
                 "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
                 "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4",
                 "5", "6", "7", "8", "9", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")"]

# Get the current time after program runs, subtracts it from the initial time
now = datetime.now()
password = input("Enter a 4 character password:\n")

combinations = list(combinations_with_replacement(possibleChars, 4)) + list()
totalTries = 0
for combo in combinations:
    totalTries += 1
    print(join(combo))
    if password == "".join(combo):
        print("Your password is: " + password)
        print(totalTries)
        # Output the difference in seconds
        later = datetime.now()
        diff = (later - now).total_seconds()
        print(diff)

        break

感谢您的帮助


Tags: thefromimport密码datetimetimewithpassword
2条回答

combinations = list(combinations_with_replacement(possibleChars, 4)) + list()行是错误的

_与_替换函数的组合正确使用如下:combinations_with_replacement('ABCD', 2) # Output: AA AB AC AD BB BC BD CC CD DD

遗憾的是,您将整个列表传递给函数,但需要字符串


资料来源:https://docs.python.org/3.9/library/itertools.html

仔细研究之后,将_与_replacement()组合使用是错误的函数。我对您的代码做了一些更改,但无法使其正常工作。查看[1] 我知道这个函数不是你想要的。挖 还有一些,我得到了[2],它同时适用于1234e!34

所以,最后,我得到:

from datetime import datetime
from itertools import product

#############################
# Password Cracking Program #
#############################
possibleChars = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
                 "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
                 "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4",
                 "5", "6", "7", "8", "9", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")"]

# Get the current time after program runs, subtracts it from the initial time
now = datetime.now()
password = input("Enter a 4 character password:\n")

combinations = product(possibleChars, repeat=4)
totalTries = 0
for combo in combinations:
    totalTries += 1
    if password == "".join(combo):
        print("Your password is: " + password)
        print(totalTries)
        # Output the difference in seconds
        later = datetime.now()
        diff = (later - now).total_seconds()
        print(diff)
        break

[1]-https://www.geeksforgeeks.org/python-itertools-combinations_with_replacement/

[2]Python combination with replacement

相关问题 更多 >