系统密码长度为6-12个字符

时间:2014-07-22 07:45:17

标签: python passwords system

我是Python的新手,已经完成了以下任务:

设计,编码,测试和评估系统以接受和测试某些特征的密码:

  • 它应至少为6个字符,且长度不超过12个字符。
  • 系统必须指出密码失败以及原因,要求用户重新输入他们的选择,直到输入成功的密码。
  • 必须显示表示密码可接受的消息。
  • 可以根据简单的标准评估密码强度,以评估其适用性;例如,仅使用大写和小写字母字符和数字字符的密码系统可以评估密码强度为:
    • 如果只使用一种类型,则会出现问题,例如:全部小写或全部数字
    • 如果使用两种类型,则为MEDIUM
    • 如果使用了所有三种类型,则为STRONG。

到目前为止,我已经完成了以下操作,但没有让它正常工作:

def password():

    print ('Welcome user ! Please enter password below\n')
    print ('The password entered must be between 6-12 characters long\n')

    while True:
        password = input ('Please enter your password . . . :')
        weak = 'weak'
        med = 'medium'
        strong = 'strong'
        if len(password) >12:
            print ('password is too long It must be between 6 and 12 characters')
        elif len(password) <6:
            print ('password is too short It must be between 6 and 12 characters')
        elif len(password) >=6 and len(password) <= 12:
            print ('password ok\n')
            if password.lower()== password or password.upper()==password or password.isalnum()==password:
                print ('password is', weak)
            elif password.lower()== password and password.upper()==password or password.isalnum()==password and password.upper()==password:
                print ('password is', medium)
            else:
                password.lower()== password and password.upper()==password and password.isalnum()==password
                print ('password is', strong)
            break

password()

我试图引入while循环:

while invalid:
    if len(password) >=6 and (password) <=12:
        password=False
        # number in range
        # sets invalid to False to stop loop
    else:
        print('Sorry the password you entered was not between 6 and 12 characters long')
        print('Please try again')
print('You have entered a valid password')

但仍然无法让它工作请帮助!!!

5 个答案:

答案 0 :(得分:1)

好的,不清楚你面临的确切问题但是你检查密码是强度中等的条件是草率的

 elif password.lower()== password and password.upper()==password or password.isalnum()==password and password.upper()==password:

这是一个考虑布尔变量

的建议

D - &gt;包含至少1位数的字符串

U - &gt; string包含至少1个大写和

L-&gt;字符串包含至少1个小写字符

D | U | L == low | medium | strong 

0   0   0     1
0   0   1     1 
0   1   0     1
0   1   1           1     
1   0   0     1
1   0   1           1
1   1   0           1
1   1   1                     1

只有一种方法可以将密码视为强大

您可以使用regex

计算D.
_digits = re.compile('\d')
def contains_digits(d):
    return bool(_digits.search(d))

条件U和L可以很容易地计算

现在减少你的表达后

strong = D * U * L

中=(!D * U * L)+(D *!U * L)+(D * U *!L)

低=(!D *!U)+(!D *!L)+(!U *!L)

所以你的代码看起来像

D = areDigits(Password)
U = areUpper(Password)
L = areLower(Password)


if((!D and !U) or (!D and !L) or (!U and !L)):
    print("weak password")
elif(D and U and L):
    print("strong password")
else:
    print("medium strength password")

这可能看起来有点丑陋,但它是一种更系统的方式来处理这个想想如果你要包括特殊字符和其他要求你会做什么!

答案 1 :(得分:0)

尝试使用正则表达式检查密码并根据各种条件检查密码。您可能会发现此链接很有用:

Password checker containing many conditional statements

答案 2 :(得分:0)

同意这是正则表达式的一个很好的用例。

此外,您的媒体案例似乎永远不会被触发:

elif password.lower()== password and password.upper()==password or password.isalnum()==password and password.upper()==password:

>>> 'a123'.lower() == 'a123' and 'a123'.upper() == 'a123'
False

>>> '1234'.isalnum() and '1234'.upper() == '1234'
True

因为下部和上部必须同时为真,否则isalnum和isalupper必须同时触发 - &gt;但是A12345或12345被认为是弱的,所以它不能触发药物......

您可以考虑为您的密码进行测试。

答案 3 :(得分:0)

您的代码有点难以阅读。我会将密码的实际测试分成一个单独的函数,就像下面的程序一样。

然后您可以更清楚地看到正在测试的内容,并且您可以对主要功能中的测试结果做出反应。

我使用密码类来存储测试结果,因为我喜欢它的外观。

Python 3的示例:

import re

def check_password(chars, min_chars=6, max_chars=12):
    class Password: pass

    Password.has_uppercase = bool(re.search(r'[A-Z]', chars))
    Password.has_lowercase = bool(re.search(r'[a-z]', chars))
    Password.has_numbers = bool(re.search(r'[0-9]', chars))

    if not min_chars <= len(chars) <= max_chars:
        print("Password needs to be between %s and %s characters." % (min_chars, max_chars))
        return


     /* Return a list of only my attributes
        of the Password class. */

     return [t for t in vars(Password).items() if not t[0].startswith("__")]



if __name__ == "__main__":
    meanings = {0: "unacceptable", 1: "Weak", 2: "Medium", 3: "Strong"}

    while True:
        chars = input('\nEnter a password: ')
        results = check_password(chars)
        if results:

            /* Points are equal to how many tests are successful */
            points = len([p[0] for p in results if p[1] is True])

            /* Print out the results to the console */
            print("\nPassword strength is %s.\n" % meanings.get(points))

            for condition, result in results:
                print("{0:<15} {1} {2}".format(condition, ":", bool(result)))
            break

答案 4 :(得分:0)

while invalid:
    if len(password) >=6 and (password) <=12:
        password=False
        # number in range
        # sets invalid to False to stop loop
    else:
        print('Sorry the password you entered was not between 6 and 12 characters long')
        print('Please try again')
print('You have entered a valid password')