Password Security Project- Question about Python

Ask questions about projects relating to: computer science or pure mathematics (such as probability, statistics, geometry, etc...).

Moderators: kgudger, bfinio, Moderators

Locked
taligator
Posts: 5
Joined: Tue Dec 29, 2015 12:55 pm
Occupation: Student

Password Security Project- Question about Python

Post by taligator »

I'm doing the Password Security project for my science fair and I was looking at some of the other questions because each password that I had entered was taking a really long time. I saw that someone suggested removing the unnecessary characters from method 2 for each password and I proceeded to do that, while also changing the code below- where it says > 0 to > 26.
if pass_wheel_array > 26:
ourguess_pass = wheel[pass_wheel_array] + ourguess_pass

However, when I tried to run the module, it kept saying "expected an indented block" and had the second line from above highlighted. When I looked it up online to see what the message meant, there was an example in which the row was too far indented. I tried to fix that on my code as well, but it didn't change anything, and I continue to get the "expected an indented block" message.

If anyone knows what I should do that would be greatly appreciated.

Thank you!
Talia
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Password Security Project- Question about Python

Post by HowardE »

That is one of the fun features of Python. It knows what defines a block of code simply by seeing how the indentation looks. The code does have to have clearly aligned left margins like this:
Image
(http://pythoniter.appspot.com)

Do your editing in IDLE and you can fix your formatting by deleting off the spaces and tabs to the left of your line and then retabbing it back over so it lines up. You can also find online Python formatters (like http://pythoniter.appspot.com) that may help.

Howard
taligator
Posts: 5
Joined: Tue Dec 29, 2015 12:55 pm
Occupation: Student

Re: Password Security Project- Question about Python

Post by taligator »

Hi, I was continuing my project from before and my first two passwords (which were all lowercase or capital letters), worked pretty quickly, however, the third password (which included numbers) did not work. When I tried a password with a symbol it did not work either.

My project is measuring the effect of adding numbers, symbols and capital letters on the time it takes for the computer to guess it. I am not sure why this isn't working because in the example password, 314, the computer was able to guess it in little time. I keep getting this error message whenever a password contains a symbol or number.

Code: Select all

Traceback (most recent call last):
  File "/Users/Talia/Desktop/crack2-3.py", line 446, in <module>
    sys.exit(main(sys.argv[1:]))
  File "/Users/Talia/Desktop/crack2-3.py", line 397, in main
    foundit = search_method_3("passwords.txt")
  File "/Users/Talia/Desktop/crack2-3.py", line 252, in search_method_3
    ourguess_pass = Cap(ourguess_pass)
  File "/Users/Talia/Desktop/crack2-3.py", line 205, in Cap
    s = s.upper()[0]+s[1:]
IndexError: string index out of range
If you know how to fix this it would be greatly appreciated because my experiment data is due next Tuesday.

Thanks!
Talia B
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Password Security Project- Question about Python

Post by HowardE »

A good way of finding out why things don't work in your code is to print things out to the screen. Line 232 of search_method_3 takes your guess and passes it off to a program that makes the first character upper case. The error is suggesting that the string is too short. once Cap() only cares about the first character, it may be that the string is empty.

There is a commented out print statement right after the call to Cap(). I'd try putting one before it and see what you're using as your guess. Something like:

Code: Select all

       if still_searching:
            print("Capitalizing: "+ourguess_pass)
            ourguess_pass = Cap(ourguess_pass)
            #print("Guessing: "+ourguess_pass)
The program should then display what it's working with for a guess and then crash. Hopefully what it prints will give you a clue.

Howard
taligator
Posts: 5
Joined: Tue Dec 29, 2015 12:55 pm
Occupation: Student

Re: Password Security Project- Question about Python

Post by taligator »

Thanks for getting back to me so quickly!

I tried doing the code you suggested, and the error messages were again there as you said. I just wanted to let you know this is the first time that I've used Python, and while I could figure some stuff out, I do not understand how to fix this.

I understand you are trying not to just give me the answer because then I wouldn't be learning anything, but it would be great if you could help me out, since I'm having a difficult time trying to figure out how to make this work.

Thank you again and have a great weekend!
Talia B
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Password Security Project- Question about Python

Post by HowardE »

Talia-

Yes, I certainly am not interested in writing you code for you but I'd be happy to help you find that bug. Needless to say, I can't help you debug code I can't see.

The suggestion I gave you should have printed out the guess *before* it changed the first character to upper case and failed. If you posted the results that would help, or if you want to post the code for that method here I'd be happy to see if I can find the problem.

Howard
taligator
Posts: 5
Joined: Tue Dec 29, 2015 12:55 pm
Occupation: Student

Re: Password Security Project- Question about Python

Post by taligator »

Code: Select all

Traceback (most recent call last):
  File "/Users/Talia/Desktop/crack2-5.py", line 447, in <module>
    sys.exit(main(sys.argv[1:]))
  File "/Users/Talia/Desktop/crack2-5.py", line 398, in main
    foundit = search_method_3("passwords.txt")
  File "/Users/Talia/Desktop/crack2-5.py", line 253, in search_method_3
    ourguess_pass = Cap(ourguess_pass)
  File "/Users/Talia/Desktop/crack2-5.py", line 205, in Cap
    s = s.upper()[0]+s[1:]
IndexError: string index out of range
>>> 
So this is what happened when I inputed Football7 as the password. I also input more passwords into the list with more recent data.

I'm a little confused about what's wrong with lines 447, 398, and 205 because I didn't change anything with those.

Code: Select all

# search method 3 uses a list of dictionary words. In this case, we have a list
# of the 500 most commonly used passwords in 2005 as collected by Mark Burnett
# for his book "Perfect Passwords" (ISBN 978-1597490412). Because the list comes
# from so many people around the world, we had to remove some of the passwords.
# People like to use passwords that they think will shock other people, so
# sometimes they're not fit for polite company.
def search_method_3(file_name):
    global totalguesses
    result = False
    
    # Start by reading the list of words into a Python list
    f = open("passwords.txt")
    words = f.readlines()
    f.close
    # We need to know how many there are
    number_of_words = len(words)
    print("Using method 3 with "+str(number_of_words)+" in the list")
    
    ## Depending on the file system, there may be extra characters before
    ## or after the words. 
    for i in range(0, number_of_words):
        words[i] = cleanup(words[i])

    # Let's try each one as the password and see what happens
    starttime = time.time()
    tests = 0
    still_searching = True
    word1count = 0           # Which word we'll try next

    while still_searching:
        ourguess_pass = words[word1count]
        #print("Guessing: "+ourguess_pass)
        # Try it the way it is in the word list
        if (check_userpass(which_password, ourguess_pass)):
            print ("Success! Password "+str(which_password)+" is " + ourguess_pass)
            still_searching = False   # we can stop now - we found it!
            result = True
        #else:
            #print ("Darn. " + ourguess_pass + " is NOT the password.")
        tests = tests + 1
        totalguesses = totalguesses + 1
        # Now let's try it with the first letter capitalized
        if still_searching:
            print("Capitalizing: "+ourguess_pass)
            ourguess_pass = Cap(ourguess_pass)
            #print("Guessing: "+ourguess_pass)
            if (check_userpass(which_password, ourguess_pass)):
                print ("Success! Password "+str(which_password)+" is " + ourguess_pass)
                still_searching = False   # we can stop now - we found it!
                result = True
            #else:
                #print ("Darn. " + ourguess_pass + " is NOT the password.")
            tests = tests + 1
            totalguesses = totalguesses + 1

        word1count = word1count + 1
        if (word1count >=  number_of_words):
            still_searching = False

    seconds = time.time()-starttime
    report_search_time(tests, seconds)
    return result
I also have line 205 right here.

Code: Select all

## A little helper program that capitalizes the first letter of a word
def Cap (s):
    s = s.upper()[0]+s[1:]
    return s
Thank you so much for helping!
Last edited by taligator on Sat Jan 09, 2016 10:14 am, edited 2 times in total.
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Password Security Project- Question about Python

Post by HowardE »

Talia-

If you noticed in your error log, just before the crash it showed you that you had a word with no length:
  • Capitalizing:
    Traceback (most recent call last):
    File "/Users/Talia/Desktop/crack2-5.py", line 447, in <module>
    sys.exit(main(sys.argv[1:]))
Your password list has a blank line in it. The program is trying to compare against an blank password which isn't very useful anyway, and when it tries to make the first character upper case, it dies. So please change a few things.

Code: Select all

## A little helper program to remove any weird formatting in the file
def cleanup (s, n):
    s = s.strip()
    if 0 == len(s):
        print("Word "+str(n)+" in your word list appears to be blank!!")
    return s
That's a new version of cleanup() that does the same thing but also prints a message on the screen if you read in a blank line. Since you want to know where it is in the file, it takes two arguments now instead of one. The second is the line number. It only uses it to display in the event of a blank. So since you have to pass it the line number, when you call it you call it a bit differently:

Code: Select all

    ## Depending on the file system, there may be extra characters before
    ## or after the words. 
    for i in range(0, number_of_words):
        words[i] = cleanup(words[i],i)
Please notice that instead if calling it as "cleanup(words[ i])" we call it now as "cleanup(words[ i],i)". The second value is the line number it can use if it needs to. This happens a couple of times in the code so you have to search and find all of them to add the extra ",i".

And finally, let's make Cap() a little smarter so that if a blank word shows up it doesn't die:

Code: Select all

## A little helper program that capitalizes the first letter of a word
def Cap (s):
    # If the length of the string is 0, we have nothing to uppercase
    if len(s) > 0:
        s = s.upper()[0]+s[1:]
    return s
If the word it gets is blank, or 0 length, it doesn't do anything.

It's good that you found extra words to add to the password file. If you could do me a favor though, please delete that section from your posting. If you remember the comment in the code about my cleaning up the 500 words so that the ones not fit for polite company would be gone, I see that you've put a bunch of them back. You probably won't need those in testing and we don't need them displayed in your post.

Make those changes and when you run the program it will run just fine but will also tell you which line of your password file has the blank line. Edit it out with your text editor - it just wastes time and slows the program down a little. At least with those modifications it shouldn't crash. The good news is that your program didn't have any errors in it - it just wasn't smart enough to handle a broken password file. A good program should be able to handle bad data. I will say that I intentionally left out any extra error checking code because it slows things down and I wanted the code to run as fast as possible. The change to cleanup() won't make any huge difference because reading in the file only happens once her method. The change to Cap() though, that adds a tiny bit of time to every call and will make a small but measurable difference in performance. If you can tell what that is, maybe this is an interesting tidbit to add to the discussion of your methods and results?

Obviously, please write back if you have any more issues or just to say things worked.

Howard
taligator
Posts: 5
Joined: Tue Dec 29, 2015 12:55 pm
Occupation: Student

Re: Password Security Project- Question about Python

Post by taligator »

Ok thank you so much for all those tips!

I do have a question though. I input the password Football! as password 0, and it has gone through methods 3, 4 and 1, and now is checking using method 2. I intend to use punctuation in later passwords, but when I read through Method 2 I didn't see anything that checked for punctuation. Can I just add the symbols to the wheel below?

Code: Select all

   wheel = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Password Security Project- Question about Python

Post by HowardE »

Yes, that's perfectly fine. When the code checks to see if the wheel has gone all the way around it compares a counter to 62 because that's the number of characters now. If you add or subtract characters, change that number appropriately. You might also consider using len() to determine the length , store that in a variable and use that in the test.

Howard
Locked

Return to “Grades 9-12: Math and Computer Science”