关于密码生成的问题
大佬们,我在做作业的时候遇到了个问题这是代码# 密码安全性检查代码
#
# 低级密码要求:
# 1. 密码由单纯的数字或字母组成
# 2. 密码长度小于等于8位
#
# 中级密码要求:
# 1. 密码必须由数字、字母或特殊字符(仅限:~!@#$%^&*()_=-/,.?<>;:[]{}|\)任意两种组合
# 2. 密码长度不能低于8位
#
# 高级密码要求:
# 1. 密码必须由数字、字母及特殊字符(仅限:~!@#$%^&*()_=-/,.?<>;:[]{}|\)三种组合
# 2. 密码只能由字母开头
# 3. 密码长度不能低于16位
import string
word = string.ascii_letters
number = '1234567890'
punctuation = '~!@#$%^&*()_=-/\,.?<>;:[]{}|'
def checking(password):
str(password)
if len(password) <= 8 and (password.isalpha() == 0 or password.isnumeric() == 0):
return '低级密码'
length = len(password)
print(length)
word_check = 0
number_check = 0
punctuation_check = 0
for i in range(0, length + 1):
cache = password
if cache in word:
word_check = 1
print('wordfound')
if cache in number:
number_check = 1
print('numberfound')
if cache in punctuation:
punctuation_check = 1
print('punctuationfound')
if word_check + number_check + punctuation_check == 2 and length >= 8:
return '中级密码'
elif word_check + number_check + punctuation_check == 3 and length >= 16 and password in word:
return '高级密码'
else:
print(word_check,number_check,punctuation_check)
return '不符合规定'
if __name__ == '__main__':
password = input('请输入密码')
print('正在检查')
print(checking(password))
调试数据是这样的
请输入密码123456789
正在检查
9
numberfound
numberfound
numberfound
numberfound
numberfound
numberfound
numberfound
numberfound
numberfound
wordfound #这三个哪里来的
numberfound #这三个哪里来的
punctuationfound #这三个哪里来的
1 1 1
不符合规定
进程已结束,退出代码为 0
有大佬帮我下吗?苦思不得其解啊 本帖最后由 阿奇_o 于 2021-7-6 01:30 编辑
import re
def passwd_test(pw):
# 低强度
if re.fullmatch(r"(\d{1,8})|(\w{1,8})", pw):
# print(re.fullmatch(r"(\d{1,8})|(\w{1,8})", pw))
return "弱密码"
# 中强度
# https://gist.github.com/leadfast/1932460490b637260b92ad9a200d4eac
pt0 = re.compile(r"(?=^.{8,15}$)(?=(?:.*?\d){1})(?=(?:.*?){1})(?=(?:.*?[-~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]){1})[-0-9a-zA-Z~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]*$")
pt1 = re.compile(r"(?=^.{8,}$)(?=(?:.*?){1})(?=(?:.*?[-~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]){1})[-a-zA-Z~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]*$") # 字母和特殊字符的组合,无数字
pt2 = re.compile(r"(?=^.{8,}$)(?=(?:.*?\d){1})(?=(?:.*?[-~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]){1})[-0-9~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]*$")# 数字和特殊字符组合,无字母
pt3 = re.compile(r"\d\w{8,}")# 数字和字母组合,无特殊字符
for p in :
if re.search(p, pw):
return "中强度"
# 高强度
pt = re.compile(r"(?=^{1,}.{15,}$)(?=(?:.*?\d){1})(?=(?:.*?){1})(?=(?:.*?[-~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]){1})[-0-9a-zA-Z~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]*$")
if re.search(pt, pw):
return "强密码"
print(passwd_test('1234678'))
print(passwd_test('123z678@'))
print(passwd_test('password1234!@#')) # 中强度,位数不够
print(passwd_test('password1234!@#$')) # 强密码,刚够16位
print(passwd_test('password1234!@#$.'))# 强密码
print(passwd_test('100password1234!@#$.')) # 中强度,不是字母开头
正则。。you make me crazy! {:10_245:}{:10_266:} for i in range(0, length + 1): #改成for i in range(0, length):
cache = password #改成password
数组下标越界所致,你仔细数一数,实际上已经输出了9次正确结果
第10次的时候 cache = password 实际上 cache还被赋予了一次值,值的结果为 ''
然后因为你的if 为并列关系,所以cache 匹配了3次。 caps = +
digits =
symbols = ++
def isHigh(p: 'passwords') -> bool:
if len(p) < 16:
return False
elif ord(p) not in caps:
return False
return (any()\
and any()\
and any())
def isMedium(p: 'passwords') -> bool:
if len(p) < 8:
return False
x = any()
y = any()
z = any()
a = x and y and (not z)
b = x and z and (not y)
c = y and z and (not x)
return any()
def isLow(p: 'passwords') -> bool:
if len(p) > 8:
return False
x = any()
y = any()
z = any()
return y and (not x) and (not z)
def check(p: 'passwrods') -> str:
if isHigh(p):
return "高級密碼"
elif isMedium(p):
return "中級密碼"
elif isLow(p):
return "低級密碼"
else:
return "密碼不合法"
def main():
print(check(input("請輸入密碼:\n")))
if __name__ == "__main__":
main()
页:
[1]