-1

I'm struggling with a piece of code that I am writing for a computer security course.

I am trying to write a piece of test code that test a 4 digit password based on the time to resolve.

I want to start with a pw of 4 numbers all 0's. For each digit, test the number 0 through to 9.

If the I get a response back from a function that is greater than 0.2sec, then alter the pw list with that digit, then move onto the next one.

However my code is not incrementing through the list. I tried simply inserting a break after my if statement, but realized I also have to break our of the outer "for loop", i tried inserting a break here, but still cant get my code to work.

What Am I missing?

import time
import sys # ignore

sys.path.insert(0,'.') # ignore
real_password = '2234'

#This function cannot be changed
def check_password(password): # Don't change it
    if len(password) != len(real_password):
        return False
    for x, y in zip(password, real_password):
        time.sleep(0.1) # Simulates the wait time of the safe's mechanism
        if int(x) != int(y):
            return False
    return True



def crack_password():
    pw = [0,0,0,0]
    cnt = 0
    if cnt < 3:
        for i in range (0,9):
            pw[cnt] = i
            start = time.time()
            check_password(create_string(pw))
            stop = time.time()
            t_time = stop - start
            print(pw[cnt], t_time)
            #Struggling with my code here!


#Creates a string from the list
def create_string(pw_list):
    string = ''
    for char in pw_list:
        string += str(char)
    return string

My print statement in the crack_password function provides:

0 0.10016393661499023
1 0.10017132759094238
2 0.1006174087524414
3 0.10022568702697754
4 0.1002652645111084
5 0.20053935050964355
6 0.10025167465209961
7 0.10107183456420898
8 0.10021018981933594

Thus I know that the first number of the password is 5, as it is greater than 0.2. How can I then break from this loop and move to the next iteration of the pw integer?

blupacetek
  • 155
  • 1
  • 9
  • 1
    Does this answer your question? [How to break out of multiple loops?](https://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops) – jjramsey Mar 02 '21 at 16:10
  • I had read this post earlier and it didn't really help me, I'm struggling with my implementation I think. – blupacetek Mar 02 '21 at 16:39
  • So i realized it wasn't the loop that was breaking my code, it was my timing on my if statement. So the link to break out of loops was correct. – blupacetek Mar 04 '21 at 16:28

1 Answers1

-1
def test_pw():
    pw = [0,0,0,0]
    for i in range(4):
        for x in range(0,10):
            pw[i] = x
            pw_test = create_string(pw)
     start_t = time.time()
     check_password(pw_test)
     end_t = time.time()
     t_results = end_t - start_t
     if t_results > 0.2:
  • 1
    This wont work as I need to check each iteration of x first of all, only when an iteration of x is greater than 0.2 then I need to move to the next integer in the pw list. – blupacetek Mar 02 '21 at 16:43