Brute force script in Python 3.2

前端 未结 4 1960
日久生厌
日久生厌 2021-01-01 03:17

I\'m a beginner in writing code and I\'ve started with Python because it seemed the neatest and the easiest to start with (I currently have Python 3.2). Now I\'ve read some

4条回答
  •  無奈伤痛
    2021-01-01 03:39

    Code first:

    from itertools import product
    
    chars = '0123456789' # chars to look for
    
    for length in range(1, 3): # only do lengths of 1 + 2
        to_attempt = product(chars, repeat=length)
        for attempt in to_attempt:
            print(''.join(attempt))
    

    itertools.product produces a Cartesian join of its input(s) - in this case, it's being 'joined' to itself. So in the first iteration, each single character is printed. Then in the next iteration, because of repeat=length (and length is now == 2), generates '00', '01', etc... It's worth trying it and seeing the output to understand it better.

    This also means you can throw in letters (uppercase/lowercase), and change the upperbound in the range function.

    It's certainly not going to break the world of code-breaking, but should give you an idea of the flexibility of Python and the tools available to you.

    I'll leave you to check the passwords match and break out the loop.

提交回复
热议问题