以下是我的代码。我一直在尝试修复代码以对哈希(SHA-1)执行字典攻击,我得到以下结果。 附:我是编码的初学者。
import hashlib
import random
#plug in the hash that needs to be cracked
hash_to_crack = "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8"
#direct the location of the dictionary file
dict_file = "C:/Users/kiran/AppData/Local/Programs/Python/Python37/dictionary.txt"
def main():
with open(dict_file) as fileobj:
for line in fileobj:
line = line.strip()
if hashlib.sha1(line.encode()).hexdigest() == hash_to_crack:
print ("The password is %s") % (line);
return ""
print ("Failed to crack the hash!")
return ""
if __name__ == "__main__":
main()
结果:
RESTART: C:/Users/kiran/AppData/Local/Programs/Python/Python37/Codes/datest1.py
The password is %s
Traceback (most recent call last):
File "C:/Users/kiran/AppData/Local/Programs/Python/Python37/Codes/datest1.py", line 20, in <module>
main()
File "C:/Users/kiran/AppData/Local/Programs/Python/Python37/Codes/datest1.py", line 13, in main
print ("The password is %s") % (line);
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'
答案 0 :(得分:2)
您正在使用Python 3,其中print
是一个函数。这一行:
print ("The password is %s") % (line)
使用参数print
调用函数"The password is %s"
。该函数返回None
。然后None % (line)
会显示您看到的错误消息。
大多数惯用语是用这种方式写行:
print("The password is", line)
其他有用的方法:
print("The password is %s" % line)
print(("The password is %s") % (line))
答案 1 :(得分:-1)
这一行是无效的python3语法:(编辑:它是一个有效的语法!但不是你想要的:))
print ("The password is %s") % (line);
请改用:
print ("The password is %s" % line)
另外,根据错误消息,行可能是None。