Posts: 2
Joined: Sun Jan 15, 2017 1:53 pm
Occupation: Student

Password Security: Python Cracker

Postby myavb77 » Sun Jan 15, 2017 2:04 pm

I am doing the Password Security: How easily can your password be hacked? For my science fair project. I have to have my results by this Friday and I do not understand at all what I am doing. I am doing Method 3. I am confused on both the mathematics part and the Python part. Could you please help me? I am confused on the red notes. I need help as soon as possible! Thank you

Posts: 2
Joined: Sun Jan 15, 2017 1:53 pm
Occupation: Student

Python Password Cracker

Postby myavb77 » Sun Jan 22, 2017 7:29 pm

I am doing the How Easily Can Your Password Be Hacked? and was wondering how I do method three. I know method 3 uses a list of dictionary words but how do I put the list of dictionary words in the code? I am also confused on "We need to know how many there are?" what does this mean? How many words or characters? Please help as soon as possible!!!

Posts: 634
Joined: Fri Jun 20, 2014 4:42 pm
Occupation: Biostatistician/Data Scientist

Re: Password Security: Python Cracker

Postby MadelineB » Sat Feb 25, 2017 9:16 pm

Hello myavb77,
Thank you for your patience while I found some help for you:
Before I explain method 3, let's think of an example of what method 3 is trying to do. Let's pretend you were trying to guess your friend's password. You could try method #1 or method #2, but you know this can take a very long time. Instead, you can try to think of things your friend might choose as passwords. Like we said in the background, people like to choose passwords that are related to things in their life. Since this person is your friend, you can take the things he or she has said to you in the past to try as their password. For example, you might try the name of their dog or the name of their crush. These are much more likely to be their password rather than a bunch of random letters and numbers.

Method #3 tries to utilize this idea by using the most common passwords that everyone chooses. If you open up the file ‘passwords.txt’, you'll see words like "123456", "password", or "qwerty" (you can see this is a bad password just by looking at the 6 keys of your keyboard starting from the top left!).

So, how does Method #3 use this file? Well, it opens up 'passwords.txt' and stores each line of ‘passwords.txt’ in an array. This is done with the command 'words = f.readlines()'.

Now ‘words’ is an array that contains ['123456', 'password',...]. Each element in the array ‘words’ is taken from the file ‘passwords.txt’.

The ‘while still_searching’ loop looks at each element in the array ‘words’ and tests that element to see if it is the correct one. If you uncomment the print line (print("Guessing: "+ourguess_pass)) the guess at each element in the loop will display (print) in your terminal window. You'll see it simply tries each line of passwords.txt!

Now for your second set of questions:
You can see that in the function definition, file_name is passed to the function as an argument. The dictionary is loaded on line 220, where the code line is 'f=open(file_names)'. The function is called on line 397 with the argument "passwords.txt". This means that f is a handler to the passwords.txt file we downloaded. Don't forget that the file needs to be in the same working directory as our python script in order for python to find it. For example, if my file is in in C:\Python34, then my passwords.txt also needs to be in C:\Python34.

Once we open the file with 'f=open(file_name)', the next line is 'words=f.readlines()'. This method takes every line in passwords.txt and puts it in a array of strings. You can see the contents by adding 'print(words)' right below it. The first few elements of the array should be something like ['123456','password','12345678'...] if you haven't modified your passwords.txt. If you open passwords.txt, you'll see that these are the first three entries in the file! What python did is split up each word by line break. If we changed the second entry from 'password' to 'the password' in passwords.txt, then the second element in the words array would now be 'the password'.

As for the "We need to know how many there are" line, if we look below that comment, we see the line "number_of_words=len(words)". If you remember, len returns the length of an array. As I explained in the previous paragraph, 'words" is just an array whose elements are strings. So the line "number_of_words=len(words)" returns the number of elements (strings) within the 'words' array, aka the number of words in the passwords.txt file.

Let us know if this helps and be sure to let us know if you have more questions.

Return to “Grades 6-8: Math and Computer Science”