0
import crypt
import os
import shutil

'''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]
dictFile = open('dictionary.txt', 'r')
for word in dictFile.read().split():
    #print(word)
    cryptWord = crypt.crypt(word, salt)
    #print(cryptWord)
    if(cryptWord == cryptPass):
        print('Found password: %s' % word)
        print('Index located at position: %d' % word.index(" "))
        return
print('Password was not found.\n')
return

def main():
    passFile = open('passwords.txt','r')
    cryptPass1 = passFile.readline()
    testPass(cryptPass1)


if __name__ == '__main__':
    main()

My program retrieves a hash from a passwords.txt file. It then proceeds to take the salt parameters(first 2 characters of the hash) and hashes the words in the dictionary.txt file one at a time, line by line while comparing that hash to the original one in the passwords.txt file.

Once we have a match it presumes to print out which password was a decrypted match for the original hash.

'grilledcheese22' is the 4th word down at position 3 in the dictionary.txt file and it keeps outputting the index at postion: 0

How do I output the correct position in the .txt file?

Original hash: 22CWIxwLb7tWM

Decrypted hash inside dictionary.txt: 'grilledcheese22'

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • 1
    `word.index(" ")` is going to crash since `word` is the output of a `str.split()`: it cannot contain spaces. – Jean-François Fabre May 16 '18 at 19:35
  • Related to https://stackoverflow.com/questions/3519565/find-the-indexes-of-all-regex-matches-in-python – Alan May 16 '18 at 20:11
  • @Jean-FrançoisFabre Thank you, and I am aware. I deleted what was inside of those parentheses because it still was not working. Originally it was word.index(word) but my output was still 0. Any suggestions? –  May 16 '18 at 20:13

1 Answers1

1

Use enumerate while iterating over the file.

for i, word in enumerate(dictFile.read().split()):
    ...
    ...
        print('Index located at position: %d' % i)
        #print('Index located at position: {}'.format(i))
        #print(f'Index located at position: {i}')
wwii
  • 23,232
  • 7
  • 37
  • 77