2016-10-10 15 views
-1

Hi, I'm still a beginner and a bit lost. I'm working on a project for school that requires me to write different small programs that will 'guess' the given password. This is a bruteforce program, and I need it to guess every possible combination of 4 number passwords like those on the old iPhones. My problem is that when I use random.sample it generates the same random numbers multiple times. What function can I use, or what should I change so that the random numbers within the given range don't repeat themselves? I tried doing rand.int but it gave me "TypeError: 'int' object is not iterable"如何在python中的循環和範圍內生成隨機的非循環數字?

Additional questions: - How do I get my loop to stop once n == Password4 ? It simply continues, even after the correct password is found. - Is there a way I can count the number of fails(n != Password4) before my success (n == Password4)?

這是我的代碼:

import random 

    Password4 = 1234 
    def crack_password(): 

while True: 
    for n in (random.sample(range(1112, 10000), 1)): 
     while n == Password4: 
      print(n, "is the password") 
      break 
     if n != Password4: 
      print('fail') 
      break 

    crack_password() 

更新:使用代碼現在不產生隨機的非經常性的數字,但適用於我預期目的。請仍然可以自由回答原始問題,並非常感謝您的好意和迅速回復。

新代碼(歸功於@roganjosh):

import datetime as dt 

    Password4 = 9999 

    def crack_password(): 
     start = dt.datetime.now() 
     for n in range(10000): 
      password_guess = '{0:04d}'.format(n) 
      if password_guess == str(Password4): 
       end = dt.datetime.now() 
       print("Password found: {} in {}".format(password_guess, end - start)) 
       break 
    guesses = crack_password() 
+0

您的代碼將總是重複:'循環 –

+0

這是棘手的,因爲'int's不能有前導零。所以如果密碼是0000-0999,你不能把它作爲一個數字。像字符串一樣對待密碼,而不是數字。 –

+0

如果您嘗試在一個範圍內使用隨機密碼,而不是按順序執行,會有什麼不同?如果您沒有跡象表明某項失敗的原因,請逐一從「0000」到「9999」。順便提一下,這也可以計算作爲副產品的故障次數。 – roganjosh

回答

1

如果你真的想隨機嘗試所有的密碼。這更容易被

import random 
digits = [str(i) for i in range(10)] 
s = [''.join([a,b,c,d]) for a in digits for b in digits for c in digits for d in digits] 
random.shuffle(s) 
real_password = '1234' 
i = 0 
for code in s: 
    if code == real_password: 
     print() 
     print('The password is: ', code) 
     break 
    else: 
     i += 1 
     print(i, ' failures', end='\r') 
+0

感謝您的幫助。雖然...執行您的解決方案仍然有問題...這是新代碼:import random Password4 = 1234 digits = [str(i)for i in range(10)] s = [''.join( [a,b,c,d])爲數字爲b的數字爲c的數字爲d的數字] random.shuffle(s) 代碼s: while s == Password4: print (s,「is the password」) break if s!= Password4: print('fail') break 但它所做的只是打印'失敗'一次,如果我打印它會給我所有可能的麻煩無w/o說哪個是密碼 –

+0

@AlexH我更新了我的答案,向您展示了像這樣的程序的基本流程。你的問題是使用不必要的'while'循環 –

+0

再次感謝。我試圖在我的IDE上運行你的代碼,但是當我做時沒有發生任何事情。我做錯了什麼(請記住我是一個非常新的初學者)? –

0

你可能要檢查出所有可能的值,在一定規則密碼,e.g「4位」或「8個小寫字符」。考慮這些問題的答案爲出發點:

+0

感謝您的建議。我只使用數字(從組合1111到9999)。我無法弄清楚如何爲我使用的大範圍做到這一點,但我非常感謝您的意見。 –

0

你有兩個while迴路來完成,所以即使你試圖break當你找到密碼,外(第一)while循環剛剛起步而過一遍。

如果你想獨特的猜測,那麼你將不得不考慮排列。但是,由於假定密碼本身是隨機的,因此隨機猜測密碼在破解密碼方面不會比僅僅依次查看整個潛在密碼列表更有效。

嘗試像這樣的:`而真正的原因

import datetime as dt 

Password4 = 5437 

def crack_password(): 
    start = dt.datetime.now() 
    for n in range(9999): 
     password_guess = '{0:04d}'.format(n) 
     if password_guess == str(Password4): 
      end = dt.datetime.now() 
      print "Password found: {} in {}".format(password_guess, end - start) 
      break 
guesses = crack_password() 
+0

感謝您的建議!我怎麼知道密碼找到之前有多少'失敗'(這對我的論文很重要)?或者,有沒有一種方法可以測量程序中的時間量/其他變量,以確定找到密碼有多困難? –

+0

@AlexH由於您按順序檢查了密碼列表,即0000,0001 ....因此,如果您發現密碼爲1234,則您有1233次失敗:P找到密碼沒有真正的「困難」。如果你隨機選擇,密碼只能在0000和9999之間,那麼每個猜測都有可能破解它,所以通過無數次運行,每個密碼的難度是相同的(如果你使用的是真隨機數發生器)。在我的情況下,更高的數字需要更長的時間才能破解,因爲我們順序進行。 – roganjosh

+0

我已編輯我的問題擺脫所有'while'循環,因爲他們不需要,並添加了一個計時器 – roganjosh