dimanche 6 novembre 2016

Creating a Bruteforce Program which shuffles through random non-recurring numbers. Receiving a ValueError - unsure why?

A while back some great people on this site helped me create this code which essentially bruteforces through numbers 0-999999 and tells me how long it took to find the password. Problem is, I need the program to go through these numbers randomly (ie. not from lowest to highest.)

This is the original code which works (but does not go through numbers randomly):

import random

digits = [str(i) for i in range(10)]
s = [''.join([a, b, c, d, e, f]) for a in digits for b in digits for c in     digits for d in digits for e in digits for f in digits]
random.shuffle(s)
real_password = '123456'
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')

And here is the code which I tweaked slightly but isn't working:

import datetime as dt
import random

digits = [str(i) for i in range(10)]
s = [''.join([a, b, c, d, e, f]) for a in digits for b in digits for c in     digits for d in digits for e in digits for f in digits]
random.shuffle(s)

Password4 = 999999

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

guesses = crack_password()

This is what my console looks like (the errors that appear) when I try to run it:

Traceback (most recent call last): File "C:/Users/xxxx/PycharmProjects/untitled1/Math IA/Trials.6.2.1py.py", line 19, in guesses = crack_password() File "C:/Users/xxxx/PycharmProjects/untitled1/Math IA/Trials.6.2.1py.py", line 13, in crack_password password_guess = '{0:04d}'.format(n) ValueError: Unknown format code 'd' for object of type 'str'

Process finished with exit code 1

Thanks everyone for your help! I'm still somewhat of a beginner so forgive me if this is a dumb question :)




Aucun commentaire:

Enregistrer un commentaire