编程语言
首页 > 编程语言> > 无法杀死我的python代码.怎么了?

无法杀死我的python代码.怎么了?

作者:互联网

好的,所以我在python中编写了一个非常简单的密码破解程序,强制使用字母数字字符强制密码.目前,此代码仅支持1个字符的密码和密码文件,其中包含md5哈希密码.它最终将包含指定您自己的字符限制的选项(破解程序在失败之前尝试的字符数).现在,当我希望它死时,我无法杀死这段代码.我已经包含了一个尝试,除了snippit,但它不起作用.我做错了什么?

代码:http://pastebin.com/MkJGmmDU

import linecache, hashlib

alphaNumeric = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",1,2,3,4,5,6,7,8,9,0]

class main:
    def checker():
            try:
                    while 1:
                            if hashlib.md5(alphaNumeric[num1]) == passwordHash:
                                    print "Success! Your password is: " + str(alphaNumeric[num1])
                                    break
            except KeyboardInterrupt:
                    print "Keyboard Interrupt."

    global num1, passwordHash, fileToCrack, numOfChars
    print "What file do you want to crack?"
    fileToCrack = raw_input("> ")
    print "How many characters do you want to try?"
    numOfChars = raw_input("> ")
    print "Scanning file..."
    passwordHash = linecache.getline(fileToCrack, 1)[0:32]
    num1 = 0

    checker()

main

解决方法:

允许KeyboardInterrupt结束程序的方法是什么都不做.他们的工作依赖于在一个除外块中捕捉它们的任何东西;当一个异常从程序(或线程)中一路消失时,它就会终止.

你所做的是捕获KeyboardInterrupts并通过打印消息然后继续来处理它们.

至于程序卡住的原因,没有任何东西可以导致num1改变,因此md5计算每次都是相同的计算.如果你想迭代alphaNumeric中的符号,那么这样做:对于alphaNumeric中的符号:#用’symbol’做一些事情.

当然,这仍然只考虑每个可能的单字符密码.你将不得不努力尝试……

标签:python,try-catch,keyboardinterrupt
来源: https://codeday.me/bug/20190704/1380055.html