【问题标题】:Generate all passwords that meet the criteria生成所有符合条件的密码
【发布时间】:2019-10-09 20:13:49
【问题描述】:

我正在尝试创建一个程序,该程序将生成包含 2 个小写字母、2 个数字和 3 个大写字母的所有密码。我一直在尝试通过使用 Streams 在 Java 中完成它。当我看到我无处可去时,我决定用 Python 来做(因为我刚开始使用它,我真的没有任何知识) 最初我认为我将使用 itertools.combinations 和 3 个列表,其中包含所有数字、大写字母和小写字母,但内存不足(使用 32GB 或 RAM)。然后我尝试在自己的变量中获取数字、小写字母和大写字母的所有组合的列表。现在我不知道我怎么能把它放在一起。

这是我目前拥有的

from itertools import combinations_with_replacement
from string import ascii_lowercase
from string import ascii_uppercase

digits = '1234567890'
lowLetters = ascii_lowercase
upLetters = ascii_uppercase

digitComb = combinations_with_replacement(digits, 2)
upLettersComb = combinations_with_replacement(upLetters, 2)
lowLettersComb = combinations_with_replacement(lowLetters, 3)
fullList = digitComb + upLettersComb + lowLettersComb

我真的不知道从这里去哪里。

【问题讨论】:

  • 您知道要生成多少个密码吗?不管是什么语言,你的 32GB 内存都不够用。
  • 公平点。但是对此有什么好的方法呢?
  • 26**5 * 20 个排列。可能不是所有密码都可以选择。稍微集中一下问题,有人可能会提供帮助。
  • 问题是我需要所有的组合。最好存储在一个文件中,然后我可以稍后在脚本中运行以进行暴力攻击(不要尝试做任何恶意的事情,这只是我的一个课程的作业的一部分)
  • @StefanCiprianIuga 为什么需要预先生成密码?如果您正在运行脚本进行暴力攻击,只需让脚本在尝试时生成密码。这使您不必存储它们。

标签: python generator


【解决方案1】:

好吧,我设法创建了生成密码的脚本。另外,我将进行更正,说明我不需要为 7 的每个排列生成所有可能性。相反,它必须分组完成。

from itertools import product
import string

numbers = string.digits
uppercaseList = string.ascii_uppercase
lowercaseList = string.ascii_lowercase

number_prod = product(numbers, numbers)
up_prod = product(uppercaseList, uppercaseList)
low_prod = product(lowercaseList, lowercaseList, lowercaseList)

result = product(number_prod, up_prod, low_prod)

f = open("passwords.txt", "w+")
print("Please wait")
for i in result:
    f.write(''.join(str(x) for v in i for x in v) + "\n")
f.close()
print("done")


【讨论】:

    【解决方案2】:

    你熟悉正则表达式吗?如果是这样,您可以(使用某些库)生成正则表达式的所有可能匹配项:

    ^(?=.{7}$)(?=[^0-9]*[0-9][^0-9]*[0-9][^0-9]*)(?=[^A-Z]*[A-Z][^A-Z]*[A-Z][^A-Z]*[A-Z][^A-Z]*)(?=[^a-z]*[a-z][^a-z]*[a-z][^a-z]*).*$
    

    正如 Nico238 所说,你的记忆力不会喜欢它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多