蛮力密码用词和数字

时间:2015-01-17 03:22:18

标签: python python-3.x

我正在为学校做这个科学博览会项目:http://www.sciencebuddies.org/science-fair-projects/project_ideas/CompSci_p046.shtml#summary

该项目为您提供了一个基本程序,用于猜测您输入程序的密码,并使用不同的方法进行猜测。该项目的一部分是提出要添加到程序中的新算法。其中一种方法是使用一长串常用密码来匹配给定的密码,另一种方法是使用最多8位数字的简单锁定拨号模拟。我想知道是否有两种方法将它们结合起来,所以它检查列表中的一个单词加上单词后面的数字拨号。如何写一个简单的代码来做到这一点?

我真正要求的是有人帮我写一个新方法,通过单词列表并在单词后添加数字拨号(蛮力与数字),然后检查看看如果这是输入的密码。

可在此处下载原始程序和单词列表:http://www.sciencebuddies.org/science-fair-projects/project_ideas/CompSci_p046.shtml#materials

这是程序使用的数字刻度盘模拟

def search_method_1(num_digits):
    global totalguesses
    result = False
    a=0
    #num_digits = 3    # How many digits to try. 1 = 0 to 9, 2 = 00 to 99, etc.
    starttime = time.time()
    tests = 0
    still_searching = True
    print("Using method 1 and searching for "+str(num_digits)+" digit numbers.")
    while still_searching and a<(10**num_digits):
        ourguess = leading_zeroes(a,num_digits)
        tests = tests + 1
        totalguesses = totalguesses + 1
        if (check_userpass(which_password, ourguess)):
            print ("Success! Password "+str(which_password)+" is " + ourguess)
            still_searching = False   # we can stop now - we found it!
            result = True
        else:
            print ("Darn. " + ourguess + " is NOT the password.")
        a=a+1

    seconds = time.time()-starttime
    report_search_time(tests, seconds)
    return result

这是贯穿单词列表的代码:

def search_method_3(file_name):
    global totalguesses
    result = False

# Start by reading the list of words into a Python list
f = open(file_name)
words = f.readlines()
f.close
# We need to know how many there are
number_of_words = len(words)
print("Using method 3 with "+str(number_of_words)+" in the list")

## Depending on the file system, there may be extra characters before
## or after the words. 
for i in range(0, number_of_words):
    words[i] = cleanup(words[i])

# Let's try each one as the password and see what happens
starttime = time.time()
tests = 0
still_searching = True
word1count = 0           # Which word we'll try next

while still_searching:
    ourguess_pass = words[word1count]
    #print("Guessing: "+ourguess_pass)
    # Try it the way it is in the word list
    if (check_userpass(which_password, ourguess_pass)):
        print ("Success! Password "+str(which_password)+" is " + ourguess_pass)
        still_searching = False   # we can stop now - we found it!
        result = True
    #else:
        #print ("Darn. " + ourguess_pass + " is NOT the password.")
    tests = tests + 1
    totalguesses = totalguesses + 1
    # Now let's try it with the first letter capitalized
    if still_searching:
        ourguess_pass = Cap(ourguess_pass)
        #print("Guessing: "+ourguess_pass)
        if (check_userpass(which_password, ourguess_pass)):
            print ("Success! Password "+str(which_password)+" is " + ourguess_pass)
            still_searching = False   # we can stop now - we found it!
            result = True
        #else:
            #print ("Darn. " + ourguess_pass + " is NOT the password.")
        tests = tests + 1
        totalguesses = totalguesses + 1

    word1count = word1count + 1
    if (word1count >=  number_of_words):
        still_searching = False

seconds = time.time()-starttime
report_search_time(tests, seconds)
return result

1 个答案:

答案 0 :(得分:1)

我将您的问题解释为您希望将数字猜测与字母猜测交织在一起。

您面临的一个问题是,保留时间的逻辑和检查密码是否正确的逻辑与生成猜测的逻辑混合在一起。您可能希望使用生成器将“猜测生成”分解为单独的函数。

from itertools import izip

def numerical_guess():
    current_guess = 0
    while True:
        yield current_guess
        current_guess += 1

def dictionary_guess():
     for word in {'apple', 'banana', 'orange'}:
          yield word

for guess in izip(numerical_guess(), dictionary_guess()):
    if guess == password:
         print "Got it!"
    else:
         print "Not it!"

你需要考虑一些事情,比如如果一个发电机在另一个发电机之前耗尽了。

你提到你不太了解这个级别的代码,但我没有看到任何代码如此复杂的东西。您发布的代码仅使用python的最基本控件结构,所以我想知道这篇文章是否对您有所帮助。也许你可以逐行浏览代码并提及代码中任何令你困惑的部分。