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?
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? I'm also interested in how other would solve this issue.
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) if t_time > 0.2: cnt +=1 {break out of the loop and move to the next digit} #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 stringMy 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? I'm also interested in how other would solve this issue.