判断最少6位密码强度,数字、小写字母、大写字母和指定的标点符号,分别对应 weak、 below middle 、above middle 、strong。
时间: 2024-02-01 22:02:43 浏览: 136
好的,我可以为您提供一个Python代码来实现这个密码强度判断的功能:
```python
import re
password = input("请输入密码:")
# 使用正则表达式检查密码中是否包含数字、小写字母、大写字母、以及指定的标点符号
has_digit = bool(re.search(r'\d', password))
has_lower = bool(re.search(r'[a-z]', password))
has_upper = bool(re.search(r'[A-Z]', password))
has_symbol = bool(re.search(r'[!@#\$%\^&\*\(\)_\+{}:";\']', password))
# 根据密码中包含的元素个数来判断密码强度
if len(password) < 6:
print("密码强度:weak")
elif len(password) < 8:
if has_digit and has_lower and has_upper and has_symbol:
print("密码强度:strong")
else:
print("密码强度:above middle")
else:
if has_digit and has_lower and has_upper and has_symbol:
print("密码强度:strong")
else:
print("密码强度:below middle")
```
这段代码首先使用正则表达式检查密码中是否包含数字、小写字母、大写字母、以及指定的标点符号,然后根据密码中包含的元素个数来判断密码强度。如果密码长度小于6,则认为密码强度为weak;如果密码长度在6到8个字符之间,则根据密码中是否包含所有元素来判断密码强度;如果密码长度大于8个字符,则同样根据密码中是否包含所有元素来判断密码强度。
阅读全文