实现循环来循环字母和数字的组合?

2024-05-18 23:26:25 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的第一个问题,希望我做得对。我正在做一个“猜测”用户输入的密码的项目。这个程序已经知道密码了,但是我想让它做的是让它根据密码的类型遍历所有可能的字符组合。程序还会查找密码是数字、字母数字还是字母数字并包含特殊字符。你知道吗

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

我的问题是:我应该如何着手实现这个目标?我附上了我的程序,分为两个文件。主.py是你应该运行的文件。第43至75行猜测者.py是问题区域。我对Python还比较陌生,这是我做过的规模最大的项目,所以我非常感谢对设计的任何建议或更改!(如果有任何不合理的地方请告诉我)

主.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__()

猜测者.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))

Tags: inforlenifparammaintypepass
1条回答
网友
1楼 · 发布于 2024-05-18 23:26:25

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

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))

相关问题 更多 >

    热门问题