Python哈希破解程序:如何正确输出字典文件中匹配密码的行索引?
Let's break down what's going wrong here and get your script to output the correct line index for the matched password.
The Root Problem
Your current line for getting the index:
print('Index located at position: %d' % word.index(" "))
is doing something totally different than you expect. word.index(" ") searches for the first occurrence of a space inside the current word string—not the position of the word in your dictionary file. Since your target password grilledcheese22 has no spaces, this line should actually throw a ValueError... but if it's outputting 0, that means you're accidentally matching a word that does contain a space (or there's a quirk in how you're reading the file).
To get the correct line position (index 3 for the 4th line), you need to track the line number as you read each entry from dictionary.txt.
The Fix
Here's how to modify your code to track the line index correctly:
- Read the dictionary file line-by-line with line numbers using
enumerate()—this gives you both the index (starting at 0) and the line content. - Clean up each line with
strip()to remove newline characters and any leading/trailing whitespace. - Replace the incorrect index logic with the tracked line index from
enumerate().
Here's the revised code with key changes highlighted:
import crypt import os import shutil # Uncomment these if you still need them for file setup ''' curDir = os.getcwd() print(curDir) os.mkdir('NixFiles') ''' ''' shutil.move("/Users/Ayounes/Desktop/Python_dev/dictionary.txt", "/Users/Ayounes/Desktop/Python_dev/") shutil.move("/Users/Ayounes/Desktop/Python_dev/passwords.txt", "/Users/Ayounes/Desktop/Python_dev/") ''' def testPass(cryptPass): salt = cryptPass[0:2] with open('dictionary.txt', 'r') as dictFile: # Use with statement for safer file handling # Use enumerate to get line index and content for index, line in enumerate(dictFile): word = line.strip() # Remove newline/whitespace cryptWord = crypt.crypt(word, salt) if cryptWord == cryptPass: print(f'Found password: {word}') print(f'Index located at position: {index}') # Use the tracked line index return print('Password was not found.\n') return def main(): with open('passwords.txt','r') as passFile: # Safer file handling with with cryptPass1 = passFile.readline().strip() # Remove any trailing newline from the hash testPass(cryptPass1) if __name__ == '__main__': main()
Key Improvements Explained
enumerate(dictFile): This iterates over each line in the file, returning a tuple of(index, line_content)whereindexstarts at 0 (perfect for your needs, since the 4th line has index 3).line.strip(): Ensures we remove newline characters (\n) and any accidental leading/trailing spaces from each line, so we're hashing the exact word from the dictionary.withstatements: These automatically close the files when we're done with them, which is safer and cleaner than manually opening/closing files.- F-strings: Used for more readable string formatting (you can still use
%formatting if you prefer, but f-strings are modern and easier to read).
With these changes, when your script matches grilledcheese22 on the 4th line, it will correctly output Index located at position: 3.
内容的提问来源于stack exchange,提问作者user9570858

