[Learning Python] How many seconds does it take to brute force a 4-digit passcode randint / for / time /

Thing you want to do When I was looking at my iPhone, I was wondering how long it would take to break the 4-digit passcode lock. A quick calculation shows that there should be 10000 combinations of 10 to the 4th power from "0000" to "9999". When I brute force this using Python, I measured how many seconds it took to detect the passcode.

"8400 rpm. 420 horsepower In the time you finish reading this sentence Accelerate to 100km / h. " This is a promotional phrase for the BMW M3 Coupe that was published in an automobile magazine about 10 years ago.

Is it faster or slower than that? Compete with the BMW M3 Coupe!

background ・ I am a Python beginner. ・ I learned the basics, but I don't know how to apply it, I don't use it at work, and my motivation is low.

** Thinking ** ・ The 4-digit PIN code is assumed to be a combination of 4 digits, which is often used for bank PINs and smartphone lock screens. -Assuming that you do not know the PIN code, it is randomly generated using a random function. -Try brute force from "0000" to "9999", and if there is a match, the message "iPhone unlocked" will be displayed.

code

pin_code_breaker.py


from random import randint
import time
        
pin_n = randint(0, 9999)

def iphone_unlock(pin_n):
    for i in range(0, 9999):
        print(format(i, '04'))
        if i == pin_n:
            print(f"PIN is {pin_n}, iPhone unlocked.")
            break
    else:
        print('Process finished.')
        
        
start = time.time()
iphone_unlock(pin_n)
elapsed_time = time.time() - start
print(f"elapsed time: {elapsed_time}")

** Consideration ** -Since matching is tried by brute force from 0000, the younger the generated PIN code (closer to 0000), the shorter the processing time, and the larger it is (closer to 9999), the longer the processing time. ・ However, the process is completed within 3 seconds at the longest. ・ It turned out that the 4-digit password itself is defenseless.

Recommended Posts

[Learning Python] How many seconds does it take to brute force a 4-digit passcode randint / for / time /
How many months will it take to pay off your debt? [Python]
How to define multiple variables in a python for statement
How to make a Python package (written for an intern)
How to use machine learning for work? 03_Python coding procedure
Python learning basics ~ How to output (display) a character string? ~
Put the process to sleep for a certain period of time (seconds) or more in Python
[Python] How to force a method of a subclass to do something specific
How about Anaconda for building a machine learning environment in Python?
[Introduction to Python] How to use the in operator in a for statement?
[For beginners] How to register a library created in Python in PyPI
How to write a Python class
How to get the date and time difference in seconds with python
Take the free "Introduction to Python for Machine Learning" online until 4/27 application
[Python] It was very convenient to use a Python class for a ROS program.
How to stop a program in python until a specific date and time
How to generate a QR code and barcode in Python and read it normally or in real time with OpenCV