'How to use a "for loop" to search for a correct password out of a big list of potential passwords [closed]
I recently participated in a capture the flag event in a beginners cyber security class. One of the challenges was to write a "for loop" to check for the correct password out of a big list (over 1000). How would I accomplish this quickly? Or is a "for loop" even the best way to do it? (I have a txt file containing all possible passwords)
Solution 1:[1]
myGuessedPassword = "PythonIsGr8"
with open("my_password_file.txt", "r") as f:
my_password_list = f.readlines()
for passwd in my_password_list:
if passwd == myGuessedPassword:
print(passwd)
else:
print("The password was not in the file")
Solution 2:[2]
With python, you can simply read the the file into a list assuming they are in a text file.
Sudo code -
pwList = reader(file)
for word in pwList:
if word == “password”
print (“Found password - “,word)
Or you can check to see if word is in list.
if password in pwList:
print(“Password exists in list”)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | DevSteve |
