《华为机试》刷题之HJ87 密码强度等级

本文介绍了一种密码强度评估算法,通过计算密码长度、字符类型分布及组合奖励,给出密码安全等级。通过实例演示了如何使用代码判断密码强度,并详细解读了评分标准。

一、题目

在这里插入图片描述
在这里插入图片描述

二、示例

在这里插入图片描述

三、代码

while True:
    try:
        str1 = input()
        result = 0
        # 密码长度
        if len(str1) <= 4:
            result += 5
        elif len(str1) >= 8:
            result += 25
        else:
            result += 10
        word_uper = 0
        word_lower = 0
        num = 0
        signal = 0
        # 遍历字符串
        for item in str1:
            if item.islower():
                word_lower += 1
            elif item.isupper():
                word_uper += 1
            elif item.isdigit():
                num += 1
            else:
                signal += 1
        # 判断字母情况
        if word_lower == 0 and word_uper == 0:
            result += 0
        elif word_lower > 0 and word_uper > 0:
            result += 20
        else:
            result += 10
        # 判断数字情况
        if num == 0:
            result += 0
        elif num == 1:
            result += 10
        else:
            result += 20
        # 判断符号情况
        if signal == 0:
            result += 0
        elif signal == 1:
            result += 10
        else:
            result += 25
        # 判断奖励情况
        if word_lower > 0 and word_uper > 0 and num > 0 and signal > 0:
            result += 5
        if (word_lower > 0 and num > 0 and signal > 0) or (word_uper > 0 and num > 0 and signal > 0):
            result += 3
        else:
            result += 2
        # 评分标准
        if result >= 90:
            print('VERY_SECURE')
        if 80 <= result < 90:
            print('SECURE')
        if 70 <= result < 80:
            print('VERY_STRONG')
        if 60 <= result < 70:
            print('STRONG')
        if 50 <= result < 60:
            print('AVERAGE')
        if 25 <= result < 50:
            print('WEAK')
        if 0 <= result < 25:
            print('VERY_WEAK')
    except:
        break

四、算法说明

  • 计算密码长度的得分;
  • 建立四个计数值:word_uperword_lowernumsignal,分别表示大写字母、小写字母、数字、符号四种字符的个数;
  • 遍历字符串,将对应的字符个数,统计出来;
  • 根据统计的计数值,判断奖励的情况;
  • 根据最后的得分值,判断密码强度。

胡萝卜

编辑时间:2022年1月21日20:06:12

我不知道将去向何方,但我已在路上!
时光匆匆,虽未曾谋面,却相遇于斯,实在是莫大的缘分,感谢您的到访 !
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

O_胡萝卜_O

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值