我创建了一个密码检查器,它生成一个随机字符并对其进行比较,直到所有字符都相同。但是当它滚动时,它会多次重复该角色。是否有一种方法可以使角色不重复,可能将角色放入列表中?我可以使用
list.del()
停止重复的功能。这是我的代码:
import string
import random
import time
print('Welcome to the password checker')
characters = string.ascii_lowercase + string.digits + string.ascii_uppercase + ' .,!?;:`"+/*()@#$&_-='
password = input("Enter your password: ")
tryFirst = ''.join(random.choice(characters) for i in range(len(password)))
trySecond = ''
Finished = False
attempt = 0
while Finished == False:
print("{}) {}".format(attempt,tryFirst))
trySecond = ''
Finished = True
for num in range(len(password)):
if tryFirst[num] != password[num]:
Finished = False
trySecond += random.choice(characters)
else:
trySecond += password[num]
attempt += 1
tryFirst = trySecond
time.sleep(0.05)
print("Password found! That took {} attempts".format(attempt))
if attempt < 100:
print("Your password isn't safe")
elif attempt < 250:
print("Your password is quite safe, could do with improvement")
elif attempt < 400:
print("Your password is adequately safe")
elif attempt < 600:
print("Your pasword is safe")
答案 0 :(得分:2)
首先,正如其他人已经提到的那样,你的方法不会给你你想要的答案。例如,输入example
作为第一个密码,输入Qm2cl?X
作为第二个密码。您的程序会返回类似的结果,尽管它们显然不是同样强大的密码。第一个可以在字典中找到,而第二个则更难猜测(它具有更多的随机性)。
要回答您的问题,您可以在每次迭代中对单词中的所有位置使用相同的随机字符。然后,您可以安全地从characters
列表中弹出每个选中的字符。这是一个可能的实现:
import string
import random
import time
print('Welcome to the password checker')
characters = string.ascii_lowercase + string.digits + string.ascii_uppercase + \
' .,!?;:`"+/*()@#$&_-='
characters = list(characters)
random.shuffle(characters)
password = input("Enter your password: ")
tryFirst = characters.pop() * len(password)
Finished = False
attempt = 0
while not Finished:
print("{}) {}".format(attempt, tryFirst))
trySecond = ''
Finished = True
try:
char = characters.pop()
except IndexError:
print 'No more characters to check'
for num in range(len(password)):
if tryFirst[num] != password[num]:
Finished = False
trySecond += char
else:
trySecond += password[num]
attempt += 1
tryFirst = trySecond
time.sleep(0.05)
print("Password found! That took {} attempts".format(attempt))
if attempt < 100:
print("Your password isn't safe")
elif attempt < 250:
print("Your password is quite safe, could do with improvement")
elif attempt < 400:
print("Your password is adequately safe")
elif attempt < 600:
print("Your password is safe")
然而问题是你要单独检查每个角色,这对真正的黑客来说是不可能的。通过这样做,您可以显着减少查找正确密码的难题。例如,在您的情况下,characters
列表中有83个不同的字符。通过单独检查每个字符,您将永远不需要超过83次尝试获取正确的密码(如果您删除了重复的问题)。如果您只能单独检查完整的密码而不是单独的字符,就像现场情况一样,查找正确密码的尝试次数要高得多,为83^password_length/2
。例如,您平均需要3444次尝试猜测两个字符的密码,而密码为3个字符需要285893次。
因此,如果您真的想知道原始蛮力算法需要猜测由characters
列表中的83个字符组成的给定密码的尝试次数,请使用以下公式:{{ 1}}。
但请注意,这并未考虑到人类倾向于选择具有特定结构的密码,因此具有一定的可预测性。密码破解程序通过使用例如字典攻击和彩虹表来利用这个弱点。因此,破解程序所需的实际尝试次数可能比该公式所暗示的要低得多。