实现循环以循环显示字母和数字组合?

时间:2016-10-31 01:45:10

标签: python passwords

这是我的第一个问题,所以希望我这样做。我正在开展一个猜测'的项目。用户输入的密码。该程序已经知道密码,但我试图让它根据密码的类型循环遍历所有可能的字符组合。该程序还会查找密码是数字,字母数字还是字母数字并包含特殊字符。

到目前为止我还没有遇到太多麻烦(它可以成功找到一个数字密码)但是在尝试了几个小时来配置一个字母数字猜测功能后,我决定寻求一些帮助

我的问题是:我该如何实施?我已经附加了我的程序,该程序分为两个文件。 main.py是您应该运行的文件。 Guesser.py中的第43到75行是问题区域。我对Python比较陌生,这是我做过的规模最大的项目,所以我很欣赏对设计的任何建议或改变! (如果有什么事情没有意义,请告诉我)

MAIN.PY

import string
import Guesser
letters = list(string.printable)
del(letters[94:100])
class Main():
    def __init__():
        qst = input('One password (1) or multiple (m)? ')
        if qst == '1':
            pss = input('Password: ')
            if len(pss) < 4:
                print('Error! Password too short!')
                Main.__init__()
            elif len(pss) > 15:
                print('Error! Password too long!')
                Main.__init__()
            else:
                pass
            Main.type_check(qst, pss)
        else:
            e = 0
            print('Type \'done\' if you have typed in all of your passwords')
            plist = list()
            c = True
            while c == True:
                e = e + 1
                i = input('Password %s: ' % e)
                i.replace('\n', '')
                if len(i) < 4:
                    print('Error! %s is too short.' % i)
                    Main.__init__()
                elif len(i) > 15:
                    print('Error! %s is too long.' % i)
                    Main.__init__()
                if i != 'done':
                    plist.append(i)
                else:
                    c = False
            pss = plist
            Main.type_check(qst, pss)
    def type_check(qst, pss):
        if qst == '1':
            try:
                int(pss)
                pass_type = 'num'
                Guesser.Guesser.check(pss, pass_type)
            except ValueError:
                if any(every in pss for every in list(string.printable)) ==    False:
                    print('Password contains invalid characters')
                    Main.__init__()
                if any(punct in pss for punct in list(string.punctuation)):
                    pass_type = 'alphasp'
                    Guesser.Guesser.check(pss, pass_type)
                else:
                    pass_type = 'alpha'
                    Guesser.Guesser.check(pss, pass_type)
        else:
            pass_dict = dict()
            for x in range(0, len(pss)):
                try:
                    int(pss[x])
                    pass_dict[pss[x]] = 'num'
                except ValueError:
                    if any(every in pss[x] for every in list(string.printable)) == False:
                        print('Password contains invalid characters')
                        Main.__init__()
                    if any(punct in pss[x] for punct in list(string.punctuation)):
                        pass_dict[pss[x]] = 'alphasp'
                    else:
                        pass_dict[pss[x]] = 'alpha'
            pss = pass_dict
            pass_type = None
            Guesser.Guesser.check(pss, pass_type)
if __name__ == '__main__':
    print('Your password(s) has to be 4 digits or longer and 15 digits or shorter; no spaces')
    Main.__init__()

GUESSER.PY

import main
import time
import string
class Guesser():
    #fixed
    def check(pss, pass_type):
        if pass_type != None:
            if pass_type == 'num':
                guess_param = 'n'
            elif pass_type == 'alpha':
                guess_param = 'a'
            else:
                guess_param = 'asp'
            Guesser.guess(pss, guess_param)
        else:
            guess_param = None
            Guesser.guess(pss, guess_param)
    #working on this
    def guess(pss, guess_param):
        t1 = time.time()
        tries = 0
        #single passwords
        if guess_param != None:
            #numeric passwords
            if guess_param == 'n':
                gd = 0
                zeroes = 3
                gp = 0
                while gp != pss:
                    gp = ('0' * zeroes) + str(gd)
                    tries = tries + 1
                    len1 = len(str(gd))
                    #adds to the guess digit to cycle through possibilities
                    gd = gd + 1
                    len2 = len(str(gd))
                    if gd > (10 ** len(gp)):
                        zeroes = zeroes + 1
                        gd = 0
                    if len2 > len1:
                        zeroes = zeroes - 1
            #alphanumeric
            ####PROBLEM AREA###
            elif guess_param == 'a':
                letters = list(string.digits) + list(string.ascii_letters)
                ll = len(letters) - 1
                x = -1
                gd = [letters[x]]
                gdl = 1
                xpos = 0
                gp = 0
                while gp != pss:
                    xpos = len(gd) - 1
                    if x + 1 <= 61:
                        x = x + 1
                        if xpos == 0:
                            gd = [x]
                        else:
                            gd[xpos] = x
                    else:
                        gdl = gdl + 1
                        x = -1
                        xpos = len(gd) - 1
                        gd.insert(0, 0)
                        for n in range(1, (len(gd) - 1)):
                            try:    
                                gd[xpos - n] = gd[xpos - n] + 1
                                break
                            except gd[xpos - n] + 1 > ll:
                                gd[xpos - n] = 0
                    gp = gd
                    for n in range(0, (len(gp) - 1)):
                        for item in gp:
                            gp[n] = letters[item]
                    gp = ''.join(str(e) for e in gp)
                    print(gp)
            ###PROBLEM AREA ABOVE###
        #for multiple passwords
        else:
            #for n in range(0, (len(pass_dict) - 1)):
            pass
        t2 = time.time()
        t = t2 - t1
        #simplifying the time it took; not really necessary but whatever
        if t/60 > 1:
            t = t/60
            unit = 'minutes'
        elif t/3600 > 1:
            t = t/3600
            unit = 'hours'
        else:
            unit = 'seconds'
        print('Password guessed! Password was %s\nIt took %s %s and %s tries.' % (gp, t, unit, tries))

1 个答案:

答案 0 :(得分:0)

好像你正在尝试创建自己的逻辑来创建alpha数字的所有排列。但是为什么!

Python可以很容易地完成这样的微不足道的事情。

import string
num = string.digits
alphanum = string.digits + string.ascii_lowercase

现在获取3位数字的所有可能数字:

for i in itertools.product(num, num, num):
     print("".join(i))

要以通用方式执行此操作,请使用列表乘法运算符。此示例查找包含5位数的所有排列:

for i in itertools.product(*[num] * 5):
     print("".join(i))

你可以循环使用它来做1-6个字母的密码。

因此,通过自然延伸,alphanum只是:

for i in itertools.product(*[alphanum] * 5):
     print("".join(i))