2015-10-03 29 views
1

我有一個作業,我的課程,甚至當我嘗試了一切,東西似乎出錯了我的代碼。我應該製作一個小程序來檢查密碼的強度。誰能告訴我我做錯了什麼?非常感激。密碼實力

p = raw_input("Enter password") if len(p) <= 6: 
    print("Weak1") elif len(p) > 6 and len(p) <=12: 
    if p.lower: 
     print("Weak2") 
    elif p.upper() or int(): 
     print("Medium4") elif len(p) > 12: 
    if p.lower and not p.upper(): 
     print("Strong6") 
    elif p.upper() and p.lower(): 
     print("Strong7") 
    elif int() and p.lower(): 
     print("Strong9") 
+0

您可以先修復代碼的格式嗎? – multivac

+0

'p.lower'和'p.lower()'有什麼區別? –

+0

請參閱[**'str.lower()'**](https://docs.python.org/2/library/stdtypes.html#str.lower)的文檔。 Python中的字符串是不可變的,這意味着你不能改變它們。它以小寫形式返回一個新的字符串。你想檢查整個字符串是否小寫?或數字?等等。 –

回答

0

你的語法有很多問題。重新格式化你的代碼後, 考慮這些內置方法。 full list here

>>> password = "mylowercasepassword" 
>>> password.islower() 
True 
>>> password2 = "MYSPRPSSWRD" 
>>> password2.islower() 
False 
>>> password2.isupper() 
True 
>>> pwd = "MySvp3rStr0ngpw4" 
>>> pwd.isalpha() 
False 
>>> pwd.isalnum() 
True 

代替使用p.lower()p.upper()和等,其始終將評估true,的使用如下所示的方法。

您可以重寫您的代碼,但嘗試瞭解更多真正的解決方案。順便說一句,我已經修復了你的代碼的一些部分。

p = raw_input("Enter password") 

if len(p) <=6: 
# abCd1, Psw44 etc. 
    print "Your password is weak" 
elif len(p) >6 and len(p)<=12: 
## Password, pAssphrase etc. 
    if p.islower(): 
    #password, passphrase 
     print "Length is ok, but you should use mixed case or whatever else" 
    elif p.isupper() or p.isdigit(): 
    #PASSWORD, PASSPHRASE (is upper), 712356433 (is digit) 
     print "Another warning message" 
    #other elifs and else 
elif len(p)>12: 
#l0ngPasswordjumped0v3r1azyrabbit 
    if p.isalpha() or p.isalnum(): 
    #myReallyLongPassword (is alpha), l0ngPasswordjumped0v3r1azyrabbit (is alnum) 
     print "Really strong" 
    elif (not p.islower()) and (not p.isuppper()) and (not p.isdigit()): 
    #ParaPsychOlOGY (is not lower, is not upper, and is not digit) 
     print "Another strong" 
    #other elifs and else 
#other conditional  
0

你在做什麼錯的是你正在發明它不是基於最好的原理和研究自己的密碼強度估算,而是你自己發明的規則:)

exist a Python library zxcvbn,給出了更逼真的密碼強度估計。

請注意,這可能不是您想要的答案,但答案是如果您要構建真正的安全服務,您會做什麼。

+0

雖然你的回答是絕對正確的,但這是一項家庭作業,其中OP應該學習如何處理字符串。我認爲他們應該根據一小部分規則來檢查密碼。 – idjaw

+1

@idjaw:是的,謝謝。我注意到了,並在答案的最後添加了一個免責聲明。 –

0

您需要使用re模塊來查找String是否包含大寫和小寫,多少大寫和小寫等。 這是我做的一個簡單的實現。 它仍然是一個傻瓜腳本!但仍然有效!

import sys, os 
try: 
    import re 
except: 
    os.system("python -m pip install re") 
    import re 
try : 
    import string 
except: 
    os.system("python -m pip install string") 
    import string 


password = (raw_input("Type the Password : ")) 
p = password 
strength = [] 

def AnalyzePassword(): 
    total = 0 
    for k in strength: 
     total = total + int(k) 

    total_analyze = int(total) + 0.0 
    analyzed = int((total_analyze) * 10) 
    analyzed = str(analyzed).replace("00", "0") 
    result = analyzed 
    return result 




def PassLvlChecker(passd): 
    if len(passd) < 18: strength.append("-4") 
    elif len(passd) > 18: strength.append("4") 
    if passd.isalpha():  strength.append("2") 
    if bool(re.compile('[\W]+').findall(passd)) is True: strength.append("7") 
    if bool(re.compile("[A-Z]+").findall(passd)) is True: strength.append("5") 
    if bool(re.compile("[0-9]+").findall(passd)) is True: strength.append("9") 

    if len(passd) > 18 and \ 
    bool(re.compile('[\W]+').findall(passd)) is True and\ 
    bool(re.compile("[A-Z]+").findall(passd)) is True and\ 
    bool(re.compile("[0-9]+").findall(passd)) is True: 
     strength.append("15") 

    strength.append(str(len(passd))) 

    return AnalyzePassword() 





def checkAgain(pas): 
    if pas == '': 
     p = raw_input("Type the Password : ") 
     checkAgain(p) 
    else: 
     passStrength = PassLvlChecker(password) 
       print "Your password is", str((int(passStrength))) + "%", "Secured!" 

if p == '': 
    p = (raw_input("Type the Password : ")) 
    checkAgain(p) 
else: 
    passStrength = PassLvlChecker(password) 
    print "Your password is", str((int(passStrength))) + "%", "Secured!"