我是一名初学程序员,并认为创建一个程序来循环使用#34; all"可能的密码。 在1-10的范围内,您对以下"黑客程序的评价效率如何?"有没有办法让以下代码更有效?您可能知道的任何算法或技巧?
我考虑过移动' e'和更常见的字母到我的字符数组的开头,因为这些字母更常用。
import sys
password = raw_input("Enter a password: ")
characters = []
lower = ["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"]
upper = ["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"]
number = ["1","2","3","4","5","6","7","8","9","0"]
symbol = ["!","@","#","$","%","^","&","*","(",")","_","+","-","="]
#Creates array with most keyboard characters
for i in range(len(lower)):
characters.append(lower[i])
for i in range(len(upper)):
characters.append(upper[i])
for i in range(len(number)):
characters.append(number[i])
for i in range(len(symbol)):
characters.append(symbol[i])
found = False
#For 1 character passwords
for a in range(len(characters)):
if (characters[a] == password):
print (characters[a])
found = True
if (found):
sys.exit()
#For 2 character passwords
for a in range(len(characters)):
for b in range(len(characters)):
if (characters[a] + characters[b] == password):
print (characters[a] + characters[b])
found = True
if (found):
sys.exit()
for a in range(len(characters)):
for b in range(len(characters)):
for c in range(len(characters)):
if (characters[a] + characters[b] + characters[c] == password):
print (characters[a] + characters[b] + characters[c])
found = True
if (found):
sys.exit()
for a in range(len(characters)):
for b in range(len(characters)):
for c in range(len(characters)):
for d in range(len(characters)):
if (characters[a] + characters[b] + characters[c] + characters[d] == password):
print (characters[a] + characters[b] + characters[c] + characters[d])
found = True
if (found):
sys.exit()
for a in range(len(characters)):
for b in range(len(characters)):
for c in range(len(characters)):
for d in range(len(characters)):
for e in range(len(characters)):
if (characters[a] + characters[b] + characters[c] + characters[d] + characters[e] == password):
print (characters[a] + characters[b] + characters[c] + characters[d] + characters[e])
found = True
if (found):
sys.exit()
答案 0 :(得分:3)
首先,您不必输入所有这些符号
>>> import string
>>> string.uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.digits
'0123456789'
>>>
所以你可以这样做:
lower = string.lowercase
等等
这可以消失:
#Creates array with most keyboard characters
for i in range(len(lower)):
characters.append(lower[i])
for i in range(len(upper)):
characters.append(upper[i])
for i in range(len(number)):
characters.append(number[i])
for i in range(len(symbol)):
characters.append(symbol[i])
并替换为:
characters = "".join(upper, lower, number, symbol)
这可以简化:
#For 1 character passwords
for a in range(len(characters)):
if (characters[a] == password):
print (characters[a])
found = True
if (found):
sys.exit()
为:
for a in characters:
print a
sys.exit()
多字符检查需要循环,但尝试编写循环,就像我在上面的单个字符代码中所显示的那样
答案 1 :(得分:1)
在这方面,必须提及itertools
。
这个答案并没有提供答案,这就是为什么你的编程效率高或不高的原因"但它提醒你如果你很快就想要一个设置/脚本,你可以通过它来强制某些东西,那里有一个很好的lib。
import itertools
import string
for guess in itertools.product(string.lowercase, repeat=10):
password = ''.join(guess) #put the array elements together
print password
itertools.product
为您提供给定数组元素的所有可能组合(笛卡尔交叉积),在本例中为小写alpahabet,您可以使用生成所有3个行的任意长度密码的代码代码。
现在将repeat=10
重写为变量表达式,并从for
执行1
循环到您喜欢的任何密码长度。
答案 2 :(得分:0)
最有效的代码是
password = raw_input("Enter a password: ")
guess = password
print guess
:)
好吧开玩笑说,你的解决方案是一个简单的暴力攻击来找到密码,如果我们没有关于密码的额外信息(除了法律字母表是什么)那么暴力攻击是最好的攻击(就其而言)算法复杂度)。
但是你的代码有改进实现的余地。
而不是
for i in range(len(lower)):
characters.append(lower[i])
for i in range(len(upper)):
characters.append(upper[i])
for i in range(len(number)):
characters.append(number[i])
for i in range(len(symbol)):
characters.append(symbol[i])
这样做
characters = lower + upper + number + symbol
而不是有太多的案例来处理密码可能是什么,让我们使用一些数学来简化实现:)
请注意,如果该字符列表的大小为n,并且密码的长度为l,那么使用一些基本的Discrete Math / Combinatorics,我们可以使用给定的字母表集合进行n ^ l种可能的组合。我们可以简单地将每个字符组合映射到以base n表示的单个数字。例如,如果n = 2并且字母表仅包括' a'并且' b'然后基数2中的数字串10将对应于字符串" ba"。 Similalry字符串00100将压缩到" aabaa"
因此,我们的任务减少为生成所有可能的字符串。
这可以通过递归来完成(它有一点开销,但它减少了实现的负担)
def recurse(s, l, n):
if n == l:
if s == password: return True
return False:
for c in characters:
if recurse(s + c, l, n+1) == True: return True:
return False
recurse("", len(password), 0)