关于python:在.txt文件中查找字符串索引

Finding string index in .txt file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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.
'
)
return

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


if __name__ == '__main__':
    main()

我的程序从passwords.txt文件中检索哈希。然后,它继续获取salt参数(哈希的前2个字符)并逐行散列dictionary.txt文件中的单词,同时将该哈希与passwords.txt文件中的原始哈希进行比较。

一旦我们有了匹配,它就假定打印出哪个密码是原始散列的解密匹配。

'graildcheese22'是dictionary.txt文件中位置3的第4个单词,它在位置0处不断输出索引。

如何在.txt文件中输出正确的位置?

原始哈希:22cwixwlb7twm

dictionary.txt中的解密哈希:"grilledcheese22"


迭代文件时使用Enumerate。

1
2
3
4
5
6
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}')