西安电子科技大学Python程序设计上机实验——密码强度检测
常用密码规则: 含有大小写字母;含有数字;长度至少为 8 位。密码中包含的字符种类越多,认为其安全性越高。按照这个标准,可以把安全强度分为强密码、中高、中低、弱密码。其中, 强密码表示密码字符串中同时含有数字、大写字母、小写字母、标点符号这 4 类字符;弱密码表示密码字符串中仅仅包含4类字符中的一种。
import string
con = input("要检查的密码为:")
num, upp, low, oth, cnt = 0, 0, 0, 0, 1
for i in con:
if i.isdigit():
num = 1
if i.islower():
low = 1
if i.isupper():
upp = 1
if i in string.punctuation:
oth = 1
s = num + upp + low + oth
print('检测结果如下:', end='')
if len(con) < 8:
print('密码长度至少为八位,', end="")
cnt = 0
if num == 0:
print('密码至少要包含一个数字,', end="")
if upp == 0:
print('密码至少要包含一个大写字母,', end="")
if low == 0:
print('密码至少要包含一个小写字母,', end='')
if oth == 0:
print('密码至少要包含一个表达符号,')
if s == 1:
print('密码强度:弱')
if s == 2:
print('密码强度:中低')
if s == 3:
print('密码强度:中高')
if s == 4 and cnt:
print('密码符合规则', '密码强度:强', sep='\n')