-1

我为我的一个课程制作了一个强力密码破解程序,但无法弄清楚为什么它没有完全检查每个组合。如果我使用只有数字或小写字母的简单密码,它通常可以工作,但复杂的字符组合根本不一定会产生答案。

这是我为我的作业提交的代码:

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

任何帮助表示赞赏!

4

2 回答 2

0

combinations = list(combinations_with_replacement(possibleChars, 4)) + list()线路错了。

combination_with_replacement 函数的正确使用是这样的: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

于 2020-10-30T00:51:37.773 回答
0

经过一番挖掘,combinations_with_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 组合替换

于 2020-10-30T01:04:58.303 回答